Automatically remove Out of Stock products from the Prestashop Cart

In this video tutorial we will see how to automatically remove out of stock products from the Prestashop cart, and display a message about it as well.

Watch the screencast

Steps Breakdown

File: modules/blockcart/blockcart.php

Around line 64, change:

		$nbTotalProducts = 0;
		foreach ($products as $pk => $product)
		{
			$nbTotalProducts += (int)$product['cart_quantity'];
		}

To

		$nbTotalProducts = 0;
		$removed = array();
		foreach ($products as $pk => $product)
		{
			if($product['quantity_available'] <= 0 && !Product::isAvailableWhenOutOfStock($product['out_of_stock']))
			{
				$this->context->cart->deleteProduct($product['id_product'],$product['id_product_attribute'], $product['id_customization'], $product['id_address_delivery']);
				$removed[] = $product['name'];
				unset($product[$pk]);
				continue;
			}
			$nbTotalProducts += (int)$product['cart_quantity'];
		}

Around line 133, add the following to the Smarty assign:

			'removed_products' => $removed,
			'cart_qties' => (int)$this->context->cart->nbProducts(),

File: themes/default-bootstrap/modules/blockcart/blockcart.tpl
Add the following at the very beginning

<script>
{if $removed_products}
	var removed_products = new Array();
	{foreach from=$removed_products item=prd}
		removed_products.push('{$prd}');
	{/foreach}
	var products_string = removed_products.join(', ');
	alert("{l s='The following products have been removed from your cart as they are out of stock:' mod='blockcart'}" + products_string);
{/if}
</script>

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

  • Awadhesh Prajapati

    Hi Nemo,

    I want to delete out of stock or disabled products from our database, Please suggest me mysql query or something best way.

    Thanks
    Awadhesh Prajapati

  • Dorothea Steinle

    Hi! How is this done in Pestashop 1.7? :) Would be very happy to add this! Thanks!

    • Juliano

      Hi, I tested in 1.6.1 and it worked, you can try….

  • Marco van Wijnbergen

    Hi Nemo,

    Is there a way to only get a better message when the product that is added is out of stock? We are a small shop and can’t have a lot of stock. But we order almost weekly products. So what I want is a message that is telling that the added product is not on stock with the ‘message available later’ when available for order & if there only a few products on stock telling them how much in stock and how much to order. Do I have to edit the blockcart.tpl for that? And do you have a idea how to?

Store Top Sales

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