Jump to content

Check if product is already in the cart


alexshimov
 Share

Recommended Posts

Hi everyone!

 

I have a lot of questions, ill start with a first one here.

 

So I have virtual products only in my store, what i need is to check if a product is already added to the cart, because i want customer to be able only to add one of each product. And if product is already in the cart, than do nothing basically. This is the first question.

 

And then the second one that comes from the first one: I can delete all the lines of code that display quantity on the website in the template, but how can I just disable this quantity, so that only one of each product can be added?

 

Thank you!

Share this post


Link to post
Share on other sites

Sounds like you need a little bit of customization to occur

 

1) You would likely want to override the CartController and add your logic to check quantity.  The function processChangeProductInCart looks like this correct place.  The Cart class has a function called containsProduct, so you could invoke this function and then cancel the add/update if the product is already in the cart.

 

2) You would want to revise the template files to remove or hide the quantity fields so it defaults to 1 quantity.  You would also want to override CartController again for this, so it would ignore the quantity and always use 1.  If you properly remove everything from template, then this is just added protection

  • Like 1

Share this post


Link to post
Share on other sites

Sounds like you need a little bit of customization to occur

 

1) You would likely want to override the CartController and add your logic to check quantity.  The function processChangeProductInCart looks like this correct place.  The Cart class has a function called containsProduct, so you could invoke this function and then cancel the add/update if the product is already in the cart.

 

2) You would want to revise the template files to remove or hide the quantity fields so it defaults to 1 quantity.  You would also want to override CartController again for this, so it would ignore the quantity and always use 1.  If you properly remove everything from template, then this is just added protection

 

Ok. Thank you. Ill try it n will come back with what i get after customization.

Share this post


Link to post
Share on other sites

  • 3 months later...

Sounds like you need a little bit of customization to occur

 

1) You would likely want to override the CartController and add your logic to check quantity.  The function processChangeProductInCart looks like this correct place.  The Cart class has a function called containsProduct, so you could invoke this function and then cancel the add/update if the product is already in the cart.

 

2) You would want to revise the template files to remove or hide the quantity fields so it defaults to 1 quantity.  You would also want to override CartController again for this, so it would ignore the quantity and always use 1.  If you properly remove everything from template, then this is just added protection

 

 

With purpuse of learning, can how explain how it should be accomplished? Thanks

Share this post


Link to post
Share on other sites

 

Try it with

{if ($cart->containsProduct($product.id_product, $product.id_product_attribute))}
Already in cart
{/if}

 

Hi Ventura,

 

This works!

Can you explain me why?

 

1. Where is defined the variable $Cart?

2. Where is defined the function containsProduct()? I checked the post: http://www.prestashop.com/forums/topic/342788-show-that-product-is-already-in-cart-on-the-product-list/

 

Thanks again : )

 

 

EDIT: Removed 2.

Edited by ayin (see edit history)

Share this post


Link to post
Share on other sites

  • 1 year later...

Hi Folks,

 

I have found this post, and probably you can help me with my issue:

 

I want to hide a message that is given in my cart, when a selected product is in the cart.

 

The above snippet seems to be quite near to my needs, but I dont now how to customize it correctly?

 

The target is to have something like

 

 

 

{if ($cart->containsProduct($product.id != 12)) }
This is the message!
{/if}

 

But this one does not work. 

 

Thx for any help

Share this post


Link to post
Share on other sites

  • 6 months later...

Works on 1.6.1 :

 

in product.tpl

<!-- check if you have this product in cart -->
{$idproductincart = Tools::getValue('id_product')}
{$idproductatrincart = Tools::getValue('id_product_attribute')}
{if ($cart->containsProduct({$idproductincart}, {$idproductatrincart}))}
<div class="alert alert-info">{l s='Already in cart'}</div>
{/if}
<!-- / check if you have this product in cart -->

in product-list.tpl

<!-- check if you have this product in cart -->
{if ($cart->containsProduct($product.id_product, $product.id_product_attribute))}<div class="alert alert-info">{l s='Already in cart'}</div>{/if}
<!-- / check if you have this product in cart -->
  • Like 1

Share this post


Link to post
Share on other sites

  • 8 months later...
  • 2 years later...
until version 1.6 I solved the problem in a very simple way.
in the product.tpl file in THEMES, around line 311 I say this way:
 
{if !$PS_CATALOG_MODE}
                        {if ($product->is_virtual)==1}
                        <p class="alert alert-warning" align="center">{l s='Virtual product. just add 1 item to Cart.'}</p>
                        {else}
                        <p id="quantity_wanted_p"{if (!$allow_oosp && $product->quantity <= 0) || !$product->available_for_order || $PS_CATALOG_MODE} style="display: none;"{/if}>
                            <label>{l s='Quantity:'}</label>
                            <input type="text" name="qty" id="quantity_wanted" class="text" value="{if isset($quantityBackup)}{$quantityBackup|intval}{else}{if $product->minimal_quantity > 1}{$product->minimal_quantity}{else}1{/if}{/if}" />
                            <a href="#" data-field-qty="qty" class="btn btn-default button-minus product_quantity_down">
                                <span><i class="icon-minus"></i></span>
                            </a>
                            <a href="#" data-field-qty="qty" class="btn btn-default button-plus product_quantity_up ">
                                <span><i class="icon-plus"></i></span>
                            </a>
                            <span class="clearfix"></span>
                        </p>
                        {/if}
                        {/if}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More