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>