Product is already in cart information

In this guide i will show you the way of how to add information "you have X quantity of this product in cart" notification that will appear above the "add to cart" button on product pages. Whole guide will be based on controller modification and on theme file: product.tpl modification. If your shop does not use combinations (products are without attributes) you will be able to achieve this without touching the controllers. If your shop has combinations - it will be necessary to alter cart class a little. So, the most of you uses controllers - so I decided to show you the way of how to achieve this with combinations. This method will work for everyone, so each of you can give it a try :) Let's go!

you have 2 quantity of this product in cart

 

Modification of cart controller

So, as I already said in the introduction - we will have to alter the cart controller. By default it has function to check if product is in cart with function public function containsProduct. But unfortunately in this case it will work only for product without attributes (some technical aspects of this function - in context of feature that we want to implement - do not allow to check if product is in cart). Because of this we will create a copy of this function: public function containsProductNoAttribute with little modifications. 

 

Open classes/Cart.php file. If you know how to create overrides in PrestaShop I strongly suggest to use overrides. Anyway, it depends on your needs. I only show the way of how to create such feature. So, as I said - open cart.php file and at the end of it, before closing bracket } paste this function:

    public function containsProductNoAttribute($id_product, $id_customization = 0, $id_address_delivery = 0)
    {

        $sql = 'SELECT cp.`quantity` FROM `'._DB_PREFIX_.'cart_product` cp';

        if ($id_customization) {
            $sql .= '
				LEFT JOIN `'._DB_PREFIX_.'customization` c ON (
					c.`id_product` = cp.`id_product`
					AND c.`id_product_attribute` = cp.`id_product_attribute`
				)';
        }

        $sql .= '
			WHERE cp.`id_product` = '.(int)$id_product.'
			AND cp.`id_cart` = '.(int)$this->id;
        if (Configuration::get('PS_ALLOW_MULTISHIPPING') && $this->isMultiAddressDelivery()) {
            $sql .= ' AND cp.`id_address_delivery` = '.(int)$id_address_delivery;
        }

        if ($id_customization) {
            $sql .= ' AND c.`id_customization` = '.(int)$id_customization;
        }

        return Db::getInstance()->getRow($sql);
    }

Save changes to the file and go to next step of tutorial related to modification of product.tpl theme file.

 

Modification of product page

Open file located in your theme directory: product.tpl. Path to file looks like /themes/your-theme/product.tpl. We want to put the notification above the add to cart button. In this case, search in file for this code:

<div class="box-cart-bottom">
<div{if (!$allow_oosp && $product->quantity <= 0) || !$product->available_for_order || (isset($restricted_country_mode) && $restricted_country_mode) || $PS_CATALOG_MODE} class="unvisible"{/if}>
<p id="add_to_cart" class="buttons_bottom_block no-print">
<button type="submit" name="Submit" class="exclusive">
<span>{if $content_only && (isset($product->customization_required) && $product->customization_required)}{l s='Customize'}{else}{l s='Add to cart'}{/if}</span>
</button>
</p>
</div>
{if isset($HOOK_PRODUCT_ACTIONS) && $HOOK_PRODUCT_ACTIONS}{$HOOK_PRODUCT_ACTIONS}{/if}
</div>

change it to:

<div class="box-cart-bottom">
<div{if (!$allow_oosp && $product->quantity <= 0) || !$product->available_for_order || (isset($restricted_country_mode) && $restricted_country_mode) || $PS_CATALOG_MODE} class="unvisible"{/if}>
{if Context::getContext()->cart->ContainsProductNoAttribute(Tools::getValue('id_product'))}
<div class="alert alert-info">{l s='You\'ve got %s quantity of this product in cart' sprintf=Context::getContext()->cart->ContainsProductNoAttribute(Tools::getValue('id_product'))}</div>
{/if}
<p id="add_to_cart" class="buttons_bottom_block no-print">
<button type="submit" name="Submit" class="exclusive">
<span>{if $content_only && (isset($product->customization_required) && $product->customization_required)}{l s='Customize'}{else}{l s='Add to cart'}{/if}</span>
</button>
</p>
</div>
{if isset($HOOK_PRODUCT_ACTIONS) && $HOOK_PRODUCT_ACTIONS}{$HOOK_PRODUCT_ACTIONS}{/if}
</div>

and now you can save changes to this file. Now you can try the feature. Just add product to cart and try to refresh product page that you added to cart. You should see the notification that you've got this product in cart now. If not - try to clear shop cache because it is probably a matter of caching

author milos myszczuk
Article by Milosz Myszczuk PrestaShop expert, official PrestaShop community moderator. PHP developer, specialist in relative and spatial databases management, GIS Analyst, CEO & founder of VEKIA interactive agency. Read more about VEKIA company
If you like my articles and want much more valuable tips, feel free to send me donation
1.4 version 1.4.11 1.6 404 addon admin advertise ahref ajax alpha animation api app application authentication back office backup badge banner basics block bootstrap button cache carrier cart catalog category certificate changelog chat class clear client clip cms code colors columns comments configuration contact container content controller cookie counter country coupon css csv currency customer dashboard database debug default delete delivery desktop developer device disable discount displayNav displayTop download dynamic editor effect empty encrypt engine error exchange exclude export facebook faceshop fade fancoupon fancybox fanpage fatal feature feed field file fix fixed font footer free friendly url front ftp full gallery generate gift global godaddy google google+ gray grid groupon header help hide highlight homefeatured homepage hook hosting hover howto htaccess html html5 ID image import include input instagram installation integration iPhone issue javascript jquery kgb knowhow languages law left likebox link list livingsocial loading log login logo loyality mail mailing maintenance manufacturer marketing marquee mcrypt menu meta mobile modification module movie moving multilanguage multiupload must have mysql news newsletter notification number open graph order override page password performance PHP phpmyadmin picture pinterest plugin popup post prestashop prestashop 1.0 prestashop 1.1 prestashop 1.2 prestashop 1.3 prestashop 1.4 prestashop 1.5 price rules problem product profile promotion proslider purifier quantity query quick tip random rates register reinsurance release reporting reset responsive restore results ribbon rich text right sales search security seo service shadow share shipping shop shopmania slider smarty social networks SQL SSL statistics stock store style subcategory superuser support switcher tab tablet tag tax template text theme tinyMCE tips and tricks tpl tracking translations tree trends trigger tumblr tutorial twitter update upgrade upload variables video visits voucher vulnerability web2print wide widget width window wishlist wysiwyg youtube zip zopim