Removing out of stock products from the cart in PrestaShop 1.7

PrestaShop 1.7 does not remove out of stock products from shopping carts, creating major troubles when keeping low stock. Let’s see how to improve our customer’s experience with a simple modification!

Watch the screencast

Text Version

If you have lots of users it might happen sometimes that a product goes out of stock while it’s in someone’s cart. For example two users add the last unit, one proceeds to the checkout and the other doesn’t. What happens is when the second customer places the order, the product will not be available anymore, and it will cause confusion if not irritation to that person, especially if you cannot restock that specific item.

The Cart Presenter in PrestaShop 1.7

The first file we need to edit is the cartPresenter, which is responsible of displaying cart information across the store.

Open up src/Adapter/Cart/cartpresenter.php and add the following around line 51, in the class properties:

private static $removedProducts = [];

Then locate the present method, and right after the following statement:

        if ($shouldSeparateGifts) {
            $rawProducts = $cart->getProductsWithSeparatedGifts();
        } else {
            $rawProducts = $cart->getProducts(true);

Add this:

        // start nemo

            foreach ($rawProducts as $pk => $product)
            // we want to remove the product in case it's not available
                if($product['quantity_available'] <= 0 && !Product::isAvailableWhenOutOfStock($product['out_of_stock']))
                    Context::getContext()->cart->deleteProduct($product['id_product'],$product['id_product_attribute'], $product['id_customization'], $product['id_address_delivery']);
                    self::$removedProducts[] = $product['name'];
        Context::getContext()->smarty->assign(['removed_products' => self::$removedProducts]);
        \Media::addJsDef(['removed_products' => self::$removedProducts]);

And that’s all for php! Before continuing let’s make sure the product actually gets removed, so let’s test it and see if they really get removed!

The shopping cart template in PrestaShop 1.7

Let’s open up themes/classic/modules/ps_shoppingcart/ps_shoppingcart.tpl

{if isset($removed_products) && $removed_products}
  <div id="removed-modal" class="modal">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-body">
          <p>{l s='These products are out of stock' d='Shop.Theme.Checkout'}</p>
              {foreach from=$removed_products item='r_product'}
                <li>- {$r_product}</li>
            <p>{l s='And have therefore been removed from your cart' d='Shop.Theme.Checkout'}</p>


And the very last thing is actually triggering the modal when we need, so let’s add some javascript to themes/classic/assets/js/custom.js

if(removed_products.length > 0) {

And that’s it! Now this modal doesn’t look so fancy, but no worries since in one of the next videos we will go over creating and styling custom popups in prestashop 1.7, so stay tuned!

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

  • Degardin Felix

    Hi there, any solution for prestashop. 1.7.6 and 1.7.7? And if the product is not out of stock but just the quantity is inferior to the quantity in cart, how to only remove the missing quantity?
    If anyone can help thx in advance.

  • Christoph Schlager

    Hi everybody, the code from the CartPresenter.php file looks completely different for PS 1.7.5. How to get this done for PS 1.7.5? Appreciate your time answering me.

  • Jérémie JUNG


    Thank you so much …! After many days of research, I find your Holy Grail topic!

    On my Prestashop, it fixed the first problem (The Cart Presenter in PrestaShop 1.7) but not the second (The shopping cart template in PrestaShop 1.7).

    One idea ?

    Best regards,

  • ttoine

    This specific issue has been fixed in…
    What version are you using for your article / demo ?

Store Top Sales

You like the tuts and want to say "thank you"? Well, you can always feel free to donate: