Jump to content

Singular products -> add to cart -> out of stock label?


Recommended Posts

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

  • 3 weeks later...

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

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 by GFX_dev (see edit history)
Link to comment
Share on other sites

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

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

  • 4 weeks later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...