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

        if(!self::$removedProducts)
            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'];
                    unset($rawProducts[$pk]);
                    continue;
                }
            }
           
        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>
            <ul>
              {foreach from=$removed_products item='r_product'}
                <li>- {$r_product}</li>
              {/foreach}
            </ul>
            <p>{l s='And have therefore been removed from your cart' d='Shop.Theme.Checkout'}</p>
        </div>

      </div>
    </div>
  </div>
{/if}

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) {
	$('#removed-modal').modal('show');
}

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:

  • http://ttoine.net ttoine

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

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