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!