[Skip to main content](#vessel-content)

#   Conditional Delete From Cart

##

- **[jonblk](/forum/user/9766-jonblk "View jonblk's Profile")**
- [![jonblk's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/9766-jonblk "View jonblk's Profile") Topic Author
- [ Offline](/forum/user/9766-jonblk)

Less More

- Posts: 25
- Thank you received: 0
-
-

14 years 3 weeks ago [\#70835](#70835) by [jonblk](/forum/user/9766-jonblk "View jonblk's Profile")  *[Conditional Delete From Cart](/forum/4-how-to/70835-conditional-delete-from-cart#70835 "Good Day,  Is there a way to set up HikaShop so that if I add a new product to the cart it deletes any products which were in the cart previously and just adds the new product being added?  Thank You")* was created by *[jonblk](/forum/user/9766-jonblk "View jonblk's Profile")*

Good Day,

Is there a way to set up HikaShop so that if I add a new product to the cart it deletes any products which were in the cart previously and just adds the new product being added?

Thank You

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzQtaG93LXRvLzcwODM1LWNvbmRpdGlvbmFsLWRlbGV0ZS1mcm9tLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[Xavier](/forum/user/4105-xavier "View Xavier's Profile")**
- [![Xavier's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar4105.png)](/forum/user/4105-xavier "View Xavier's Profile")
- [ Offline](/forum/user/4105-xavier)

Less More

- Posts: 13201
- Thank you received: 2322
-
-
-
- [](http://www.kidiweb.fr)
-

14 years 3 weeks ago [\#70872](#70872) by [Xavier](/forum/user/4105-xavier "View Xavier's Profile")  Replied by *[Xavier](/forum/user/4105-xavier "View Xavier's Profile")* on topic *[Re: Conditional Delete From Cart](/forum/4-how-to/70835-conditional-delete-from-cart#70872 "Good Day,  Is there a way to set up HikaShop so that if I add a new product to the cart it deletes any products which were in the cart previously and just adds the new product being added?  Thank You")*

Hi,

There is actually no way to do this. But if you edit the "product / show_default" view and add an action in a onClick on the add to cart button, and this action is to reset the cart_id in session, then I think that the product will be added in a new cart, so no other products inside.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzQtaG93LXRvLzcwODM1LWNvbmRpdGlvbmFsLWRlbGV0ZS1mcm9tLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[jonblk](/forum/user/9766-jonblk "View jonblk's Profile")**
- [![jonblk's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/9766-jonblk "View jonblk's Profile") Topic Author
- [ Offline](/forum/user/9766-jonblk)

Less More

- Posts: 25
- Thank you received: 0
-
-

14 years 3 weeks ago [\#70873](#70873) by [jonblk](/forum/user/9766-jonblk "View jonblk's Profile")  Replied by *[jonblk](/forum/user/9766-jonblk "View jonblk's Profile")* on topic *[Re: Conditional Delete From Cart](/forum/4-how-to/70835-conditional-delete-from-cart#70873 "Good Day,  Is there a way to set up HikaShop so that if I add a new product to the cart it deletes any products which were in the cart previously and just adds the new product being added?  Thank You")*

Hi again,

I am actually quite new to HikaShop and I have rather limited knowledge of php and joomla!.

I am willing to try your suggested method if you can tell me how to do it step by step.

thank you in advance.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzQtaG93LXRvLzcwODM1LWNvbmRpdGlvbmFsLWRlbGV0ZS1mcm9tLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[Xavier](/forum/user/4105-xavier "View Xavier's Profile")**
- [![Xavier's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar4105.png)](/forum/user/4105-xavier "View Xavier's Profile")
- [ Offline](/forum/user/4105-xavier)

Less More

- Posts: 13201
- Thank you received: 2322
-
-
-
- [](http://www.kidiweb.fr)
-

14 years 3 weeks ago [\#70934](#70934) by [Xavier](/forum/user/4105-xavier "View Xavier's Profile")  Replied by *[Xavier](/forum/user/4105-xavier "View Xavier's Profile")* on topic *[Re: Conditional Delete From Cart](/forum/4-how-to/70835-conditional-delete-from-cart#70934 "Good Day,  Is there a way to set up HikaShop so that if I add a new product to the cart it deletes any products which were in the cart previously and just adds the new product being added?  Thank You")*

If you want to always clear the cart when you add a new product, then I think that it can be done in the file: "yourSite\component\com_hikashop\controllers\product.php".

And in the function "updatecart()" add the line: Code:

$app-&gt;getUserState(HIKASHOP_COMPONENT.'.'.$cart_type_id,0);

under the line: Code:

$cart_type_id = $cart_type.'_id';

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzQtaG93LXRvLzcwODM1LWNvbmRpdGlvbmFsLWRlbGV0ZS1mcm9tLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[jonblk](/forum/user/9766-jonblk "View jonblk's Profile")**
- [![jonblk's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/9766-jonblk "View jonblk's Profile") Topic Author
- [ Offline](/forum/user/9766-jonblk)

Less More

- Posts: 25
- Thank you received: 0
-
-

14 years 3 weeks ago [\#70993](#70993) by [jonblk](/forum/user/9766-jonblk "View jonblk's Profile")  Replied by *[jonblk](/forum/user/9766-jonblk "View jonblk's Profile")* on topic *[Re: Conditional Delete From Cart](/forum/4-how-to/70835-conditional-delete-from-cart#70993 "Good Day,  Is there a way to set up HikaShop so that if I add a new product to the cart it deletes any products which were in the cart previously and just adds the new product being added?  Thank You")*

hi again,

I tried your suggestion but unfortunately it does not work.

Do you have any other suggestions?

Thank You again

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzQtaG93LXRvLzcwODM1LWNvbmRpdGlvbmFsLWRlbGV0ZS1mcm9tLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[Xavier](/forum/user/4105-xavier "View Xavier's Profile")**
- [![Xavier's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar4105.png)](/forum/user/4105-xavier "View Xavier's Profile")
- [ Offline](/forum/user/4105-xavier)

Less More

- Posts: 13201
- Thank you received: 2322
-
-
-
- [](http://www.kidiweb.fr)
-

14 years 3 weeks ago [\#71378](#71378) by [Xavier](/forum/user/4105-xavier "View Xavier's Profile")  Replied by *[Xavier](/forum/user/4105-xavier "View Xavier's Profile")* on topic *[Re: Conditional Delete From Cart](/forum/4-how-to/70835-conditional-delete-from-cart#71378 "Good Day,  Is there a way to set up HikaShop so that if I add a new product to the cart it deletes any products which were in the cart previously and just adds the new product being added?  Thank You")*

Hi,

Sorry, it's not "getUsetState" but "setUserState"

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzQtaG93LXRvLzcwODM1LWNvbmRpdGlvbmFsLWRlbGV0ZS1mcm9tLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[jonblk](/forum/user/9766-jonblk "View jonblk's Profile")**
- [![jonblk's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/9766-jonblk "View jonblk's Profile") Topic Author
- [ Offline](/forum/user/9766-jonblk)

Less More

- Posts: 25
- Thank you received: 0
-
-

14 years 3 weeks ago [\#71691](#71691) by [jonblk](/forum/user/9766-jonblk "View jonblk's Profile")  Replied by *[jonblk](/forum/user/9766-jonblk "View jonblk's Profile")* on topic *[Re: Conditional Delete From Cart](/forum/4-how-to/70835-conditional-delete-from-cart#71691 "Good Day,  Is there a way to set up HikaShop so that if I add a new product to the cart it deletes any products which were in the cart previously and just adds the new product being added?  Thank You")*

hi again,

Thank you for replying again, however your suggestion did not work.
I do not understand the code fully but I think you are close to a solution.
I know the logic should read as follows:
when 'add to cart' is selected, if there are any items in the cart clean the cart, and add the new item.

Do you have any other suggestions.

Thank You again

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzQtaG93LXRvLzcwODM1LWNvbmRpdGlvbmFsLWRlbGV0ZS1mcm9tLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[Xavier](/forum/user/4105-xavier "View Xavier's Profile")**
- [![Xavier's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar4105.png)](/forum/user/4105-xavier "View Xavier's Profile")
- [ Offline](/forum/user/4105-xavier)

Less More

- Posts: 13201
- Thank you received: 2322
-
-
-
- [](http://www.kidiweb.fr)
-

14 years 3 weeks ago [\#71787](#71787) by [Xavier](/forum/user/4105-xavier "View Xavier's Profile")  Replied by *[Xavier](/forum/user/4105-xavier "View Xavier's Profile")* on topic *[Re: Conditional Delete From Cart](/forum/4-how-to/70835-conditional-delete-from-cart#71787 "Good Day,  Is there a way to set up HikaShop so that if I add a new product to the cart it deletes any products which were in the cart previously and just adds the new product being added?  Thank You")*

Hi,

I tried the solution on my end, and it's working fine.
Can you check in the file "administrator/components/com_hikashop/classes/cart.php" in the function "loadCart()" if the following line is commented. Code:

if($app-&gt;getUserState(HIKASHOP_COMPONENT.'.'.$this-&gt;cart_type.'_new', '0') == '0'){

If it's commented, uncomment it (don't forget to uncomment the "}" closing the if condition).

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzQtaG93LXRvLzcwODM1LWNvbmRpdGlvbmFsLWRlbGV0ZS1mcm9tLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[jonblk](/forum/user/9766-jonblk "View jonblk's Profile")**
- [![jonblk's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/9766-jonblk "View jonblk's Profile") Topic Author
- [ Offline](/forum/user/9766-jonblk)

Less More

- Posts: 25
- Thank you received: 0
-
-

14 years 3 weeks ago [\#71819](#71819) by [jonblk](/forum/user/9766-jonblk "View jonblk's Profile")  Replied by *[jonblk](/forum/user/9766-jonblk "View jonblk's Profile")* on topic *[Re: Conditional Delete From Cart](/forum/4-how-to/70835-conditional-delete-from-cart#71819 "Good Day,  Is there a way to set up HikaShop so that if I add a new product to the cart it deletes any products which were in the cart previously and just adds the new product being added?  Thank You")*

Hi again

I went to "mysite/administrator/components/com_hikashop/classes/cart.php" and I do not see the line:-

if($app-&gt;getUserState(HIKASHOP_COMPONENT.'.'.$this-&gt;cart_type.'_new', '0') == '0'){

The closest thing I see to this line is:-

if($app-&gt;getUserState(HIKASHOP_COMPONENT.'.'.$cart_type.'_id','0') == $cart_id){

I then looked for the "loadCart()" function and it does NOT exist in my 'cart.php' file.

Do you think it would help if you upload your product.php and cart.php files so I can try them?

Also, I realise there are other cart.php files in the 'com_hikashop\controllers', '\com_hikashop\views\product\tmpl' and 'com_hikashop\views\checkout\tmpl' directories. Do these also need to be modified?

Thank You again.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzQtaG93LXRvLzcwODM1LWNvbmRpdGlvbmFsLWRlbGV0ZS1mcm9tLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[Xavier](/forum/user/4105-xavier "View Xavier's Profile")**
- [![Xavier's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar4105.png)](/forum/user/4105-xavier "View Xavier's Profile")
- [ Offline](/forum/user/4105-xavier)

Less More

- Posts: 13201
- Thank you received: 2322
-
-
-
- [](http://www.kidiweb.fr)
-

14 years 2 weeks ago [\#71830](#71830) by [Xavier](/forum/user/4105-xavier "View Xavier's Profile")  Replied by *[Xavier](/forum/user/4105-xavier "View Xavier's Profile")* on topic *[Re: Conditional Delete From Cart](/forum/4-how-to/70835-conditional-delete-from-cart#71830 "Good Day,  Is there a way to set up HikaShop so that if I add a new product to the cart it deletes any products which were in the cart previously and just adds the new product being added?  Thank You")*

Can you try to replace your function "loadCart()" by: Code:

function loadCart($cart_id=0,$reset=false){ static $carts = array(); if($reset){ $carts = array(); $this-&gt;cart_id = 0; $this-&gt;cart = null; return true; } $this-&gt;filters = array(); $app = JFactory::getApplication(); if(empty($cart_id) &#124;&#124; $cart_id == 0){ $this-&gt;cart_id = $app-&gt;getUserState( HIKASHOP_COMPONENT.'.'.$this-&gt;cart_type.'_id', 0, 'int' ); } else { $this-&gt;cart_id=$cart_id; } if(!isset($this-&gt;cart) &#124;&#124; is_null($this-&gt;cart)) $this-&gt;cart = new stdClass(); $this-&gt;cart-&gt;cart_id = $this-&gt;cart_id; if(!empty($this-&gt;cart_id)){ $this-&gt;filters[]='a.cart_id = '.(int)$this-&gt;cart_id; }else{ $user = JFactory::getUser(); if(!empty($user-&gt;id)){ $this-&gt;filters[]='a.user_id = '.(int)$user-&gt;id; } $session = JFactory::getSession(); if($session-&gt;getId()){ $this-&gt;filters[]='a.session_id = '.$this-&gt;database-&gt;Quote($session-&gt;getId()); } } $filter = "(".implode(' OR ',$this-&gt;filters).")"; if($this-&gt;cart_type){ $typeFilter='a.cart_type = '.$this-&gt;database-&gt;Quote($this-&gt;cart_type); $filter .= " AND ".$typeFilter; } if(!empty($carts[$filter])){ $this-&gt;cart =&amp; $carts[$filter]; }else{ // if new cart/wishlist, don't search for an existing entry if($app-&gt;getUserState(HIKASHOP_COMPONENT.'.'.$this-&gt;cart_type.'_new', '0') == '0'){ //search for an existing cart if no cart entries were found if(!empty($filter)){ $query='SELECT a.* FROM '.hikashop_table('cart').' AS a WHERE '.$filter.' ORDER BY a.cart_modified DESC LIMIT 1'; $this-&gt;database-&gt;setQuery($query); $this-&gt;cart = $this-&gt;database-&gt;loadObject(); }else{ $this-&gt;cart = null; } $carts[$filter] =&amp; $this-&gt;cart; } } }

If it still doesn't work, thanks to wait for the next release of HikaShop which will be there before the end of the month.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzQtaG93LXRvLzcwODM1LWNvbmRpdGlvbmFsLWRlbGV0ZS1mcm9tLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

Board Categories HikaShop Install &amp; Update Product &amp; Category Display Content Creation Sales and Orders Orders management Reports &amp; Statistics Customers Checkout Payment Methods Shipping Methods Import &amp; Export (CSV, XLS…) Specific Features Affilation Taxes Wishlist Vote Carousel &amp; Slider Mass Actions Filters &amp; Search Discount &amp; Coupon Feature Request Integrations Showcase General Talk about HikaShop Development HikaShop 3.0 Beta Translation Commercial Jobs HikaShop Components HikaMarket Market: Feature requests Market: How to? Market: Bug Report HikaSerial General talk about HikaSerial Serial: Bug Report Serial: How to? Support en Français de HikaSerial (French Support) HikaSubscription HikaAuction Auction: Feature requests Auction: How to? Auction: Bug report Hikashop around the world Support en Français (French Support) Russian FAQ. Курс выживания с Hikashop. Частые вопросы и о Расширения Hikashop Наши сайты на Hikashop 日本語版サポート(Japanese Support) Thai German Archives Bug Report How to?

[](/forum/topics/feed/mode-topics?format=feed&type=rss)

Time to create page: 0.067 seconds

[Powered by](/forum/credits) [Kunena Forum](https://www.kunena.org)
