GFX_dev Posted January 16, 2022 Share Posted January 16, 2022 Hello, Trying to wrap my head around on how to change the default Prestashop 1.7.8 behavior of placing an item (of which only 1 is available in the backoffice) in the cart, and it showing it no longer is in stock. It updates the quantity from 1 to 0, but, it has not actually been sold yet. The label however shows "Out of Stock" which is very confusing for customers. Why isn't there a variable which shows the _actual_ stock, which triggers this label? And have $product.quantity for the disabling of the Add to cart button? Any ideas if there is a variable accessible which shows the "quantity 1" in the backoffice, versus the *temporary* quantity=0 for the customer doing the shopping? I am thinking of: {if $product.realstock.quantity =< 0}show label out of stock{/if} instead of {if $product.quantity =<0}show label out of stock{/if} Thanks! Link to comment Share on other sites More sharing options...
GFX_dev Posted January 16, 2022 Author Share Posted January 16, 2022 Hmm I have found {$product.stock_quantity} to be working, but only in the Checkout environment (cart). This variable is not available for the product page itself. Any thoughts how to retrieve this variable? Link to comment Share on other sites More sharing options...
GFX_dev Posted February 3, 2022 Author Share Posted February 3, 2022 Any ideas? Link to comment Share on other sites More sharing options...
Waseya Posted February 8, 2022 Share Posted February 8, 2022 the only idea is to find where this variable comes from. Smarty variables are just the way to present the data fetched by php from the database. So you have to find the php function that did the job, the php file/class with it, re-assign it to your custom smarty tpl in php, and put it into an existing tpl with a hook. This is in case you don't want to write your own sql query from scratch and to search through 200+ Prestashop tables yourself for this data. Link to comment Share on other sites More sharing options...
GFX_dev Posted February 11, 2022 Author Share Posted February 11, 2022 (edited) Thanks, that's an excellent idea. I noticed the variable is working in the product cart, so I investigated and I THINK it is rendered by Cart.php Line 4101 /** * Check if product quantities in Cart are available. * * @param bool $returnProductOnFailure Return the first found product with not enough quantity * * @return bool|array If all products are in stock: true; if not: either false or an array * containing the first found product which is not in stock in the * requested amount */ //snip/// $product['stock_quantity'] = StockManager::getStockByCarrier( (int) $product['id_product'], (int) $product['id_product_attribute'], $delivery ); //snip// I am struggling to continue. How would I re-assign this PHP to be used on the product page, modules which are displaying products (e.g. new arrivals) and the product-overview (grid page)? For the later steps, I think you mean I should remove (comment-out) the $product.quantity label which renders the "Out of Stock" warning, and have it replaced with a tpl hook? In this custom tpl hook I will create a check with this pseudo-code? {if $product.stock_quantity = 0 && $product.stock = 0} //Truely out of stock //Original code with label product out of stock {elseif $product.stock_quantity = 1 && $product.stock = 0} // The product is in the customers cart; alert customer to check cart; <p>Please <a href="/cart">check cart</a></p> (or something like this) {/if} Thanks for the input! Edited February 11, 2022 by GFX_dev (see edit history) Link to comment Share on other sites More sharing options...
jacob_v_dam Posted February 14, 2022 Share Posted February 14, 2022 Probably this will help:https://github.com/PrestaShop/PrestaShop/blob/1.7.8.3/classes/Product.php#L4276 As you can see on line 3829 the cart quantity is extracted from the product quantity. If you overwrite this file and remove those lines i think you will get the required result. Link to comment Share on other sites More sharing options...
GFX_dev Posted February 20, 2022 Author Share Posted February 20, 2022 Thanks! You are absolutely right! I commented out: if (!empty($cart) && empty(Order::getByCartId($cart->id))) { $cartProduct = $cart->getProductQuantity($idProduct, $idProductAttribute, $idCustomization); if (!empty($cartProduct['deep_quantity'])) { $nbProductInCart = $cartProduct['deep_quantity']; } } Which indeed no longer shows "out of stock" when you hit the shopping cart button. But it is possible to click on add to cart again, which results in n=2 in the shopping cart, and then an error saying there is not enough items available when checking out. I think it would be a nicer solution if the label would change from "Out of stock" to "Already in cart". It should be possible as we have the product ID available in the cart? Thoughts? Link to comment Share on other sites More sharing options...
jacob_v_dam Posted February 20, 2022 Share Posted February 20, 2022 Perfect it is better to create an override. In case of an upgrade the file will be overwritten: https://devdocs.prestashop.com/1.7/modules/concepts/overrides/#class--controller-override Please not that you need to clear the cache when creating a new overwrite. About the other question, create a new topic or check this: Link to comment Share on other sites More sharing options...
GFX_dev Posted February 25, 2022 Author Share Posted February 25, 2022 Thanks for the suggestion, I see the option is for 1.6 and indeed, the code provided does not work in 1.7. I noticed getRealQuantity() is also a PHP function (line 7680). How can I call this function in my TPL? In that case I can undo the overwrite, and perform a product-> quantity VS getRealQuantity and show a alert if quanity = 0 (= in cart) vs getRealQuantity (= stock; n=1)! Link to comment Share on other sites More sharing options...
GFX_dev Posted March 20, 2022 Author Share Posted March 20, 2022 Any thoughts? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now