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:

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

To

[php]
$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’];
}
[/php]

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

[php]
‘removed_products’ => $removed,
‘cart_qties’ => (int)$this->context->cart->nbProducts(),
[/php]

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

[php language=”htmlscript=”]
<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>

[/php]

Need PrestaShop Modules? Have a look at my Prestashop Addons Store!

Looking for quality PrestaShop Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , | Posted under PrestaShop | RSS 2.0

Author Spotlight

Fabio Porta

Fabio has been involved in web development and design since 2005, when launched his first website at the age of 16. He’s now highly skilled in both client and server side development, along with design, and since August 2012 runs a successful website about PrestaShop tutorials and Prestashop Modules called Nemo’s Post Scriptum, at http://nemops.com

Leave a Reply

Your email address will not be published. Required fields are marked *