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

#   Clear cart before a certain product is added

##

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

Less More

- Posts: 13
- Thank you received: 2
-
-

12 years 2 months ago [\#167912](#167912) by [erhartm](/forum/user/33354-erhartm "View erhartm's Profile")  *[Clear cart before a certain product is added](/forum/31-development/870446-clear-cart-before-a-certain-product-is-added#167912 "-- HikaShop version -- : current -- Joomla version -- : current 3.3 -- PHP version -- : current  hi!  I would like to create a plugin that does something when a product is added to the cart:  - It should check whether the product is part of a certain category. If this is the case, the plugin should empty the cart and then add the product to the cart, making it the only product in the cart  - If a certain product is added to the cart, it should change the shop's currency  Right now, I am working on the first part, but I do not even manage to make the cart get cleared for every product added. What I tried is this:  It clears the cart, but the product is not added afterwards. Can somebody please guide me on what I am doing wrong here? Additionally, I would greatly appreciate if you could tell me how to check if a certain product is part of a certain category.  Thank you very much &amp; all the best,  Lukas")* was created by *[erhartm](/forum/user/33354-erhartm "View erhartm's Profile")*

-- HikaShop version -- : current
-- Joomla version -- : current 3.3
-- PHP version -- : current

hi!

I would like to create a plugin that does something when a product is added to the cart:

- It should check whether the product is part of a certain category. If this is the case, the plugin should empty the cart and then add the product to the cart, making it the only product in the cart

- If a certain product is added to the cart, it should change the shop's currency

Right now, I am working on the first part, but I do not even manage to make the cart get cleared for every product added. What I tried is this:
Code:

function onBeforeCartUpdate(&amp;$cartClass,&amp;$cart,$product_id,$quantity,$add,$type,$resetCartWhenUpdate,$force,&amp;$do) { if (($type=='product') &amp;&amp; ($add=1)) $cartClass-&gt;resetCart(); $do=true; return true; }

It clears the cart, but the product is not added afterwards. Can somebody please guide me on what I am doing wrong here? Additionally, I would greatly appreciate if you could tell me how to check if a certain product is part of a certain category.

Thank you very much &amp; all the best,

Lukas

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

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

Less More

- Posts: 86290
- Thank you received: 14268
-
-

12 years 2 months ago [\#167948](#167948) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Clear cart before a certain product is added](/forum/31-development/870446-clear-cart-before-a-certain-product-is-added#167948 "-- HikaShop version -- : current -- Joomla version -- : current 3.3 -- PHP version -- : current  hi!  I would like to create a plugin that does something when a product is added to the cart:  - It should check whether the product is part of a certain category. If this is the case, the plugin should empty the cart and then add the product to the cart, making it the only product in the cart  - If a certain product is added to the cart, it should change the shop's currency  Right now, I am working on the first part, but I do not even manage to make the cart get cleared for every product added. What I tried is this:  It clears the cart, but the product is not added afterwards. Can somebody please guide me on what I am doing wrong here? Additionally, I would greatly appreciate if you could tell me how to check if a certain product is part of a certain category.  Thank you very much &amp; all the best,  Lukas")*

Hi,

If you reset the cart in the onBeforeCartUpdate function, you need to re-init it or the system won't be able to add the products to the cart after that function.
So adding that line in your code after the reset will probably help:
$cart = $cartClass-&gt;initCart();

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

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

Less More

- Posts: 13
- Thank you received: 2
-
-

12 years 2 months ago [\#168060](#168060) by [erhartm](/forum/user/33354-erhartm "View erhartm's Profile")  Replied by *[erhartm](/forum/user/33354-erhartm "View erhartm's Profile")* on topic *[Clear cart before a certain product is added](/forum/31-development/870446-clear-cart-before-a-certain-product-is-added#168060 "-- HikaShop version -- : current -- Joomla version -- : current 3.3 -- PHP version -- : current  hi!  I would like to create a plugin that does something when a product is added to the cart:  - It should check whether the product is part of a certain category. If this is the case, the plugin should empty the cart and then add the product to the cart, making it the only product in the cart  - If a certain product is added to the cart, it should change the shop's currency  Right now, I am working on the first part, but I do not even manage to make the cart get cleared for every product added. What I tried is this:  It clears the cart, but the product is not added afterwards. Can somebody please guide me on what I am doing wrong here? Additionally, I would greatly appreciate if you could tell me how to check if a certain product is part of a certain category.  Thank you very much &amp; all the best,  Lukas")*

Hi!

Thank you, it kinda works! Only problem is, if I add the same product twice, it just says "The cart is empty" - do you have any idea what could be the problem here?

Cheers!

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

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

Less More

- Posts: 86290
- Thank you received: 14268
-
-

12 years 2 months ago [\#168082](#168082) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Clear cart before a certain product is added](/forum/31-development/870446-clear-cart-before-a-certain-product-is-added#168082 "-- HikaShop version -- : current -- Joomla version -- : current 3.3 -- PHP version -- : current  hi!  I would like to create a plugin that does something when a product is added to the cart:  - It should check whether the product is part of a certain category. If this is the case, the plugin should empty the cart and then add the product to the cart, making it the only product in the cart  - If a certain product is added to the cart, it should change the shop's currency  Right now, I am working on the first part, but I do not even manage to make the cart get cleared for every product added. What I tried is this:  It clears the cart, but the product is not added afterwards. Can somebody please guide me on what I am doing wrong here? Additionally, I would greatly appreciate if you could tell me how to check if a certain product is part of a certain category.  Thank you very much &amp; all the best,  Lukas")*

Hi,

I don't know. That would require adding traces in the file administrator/components/com_hikashop/classes/cart and especially in the updateEntry function to see what data is passing by which is different from the first time to the second time in order to understand what is missing.

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

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

Less More

- Posts: 13
- Thank you received: 2
-
-

12 years 2 months ago [\#168322](#168322) by [erhartm](/forum/user/33354-erhartm "View erhartm's Profile")  Replied by *[erhartm](/forum/user/33354-erhartm "View erhartm's Profile")* on topic *[Clear cart before a certain product is added](/forum/31-development/870446-clear-cart-before-a-certain-product-is-added#168322 "-- HikaShop version -- : current -- Joomla version -- : current 3.3 -- PHP version -- : current  hi!  I would like to create a plugin that does something when a product is added to the cart:  - It should check whether the product is part of a certain category. If this is the case, the plugin should empty the cart and then add the product to the cart, making it the only product in the cart  - If a certain product is added to the cart, it should change the shop's currency  Right now, I am working on the first part, but I do not even manage to make the cart get cleared for every product added. What I tried is this:  It clears the cart, but the product is not added afterwards. Can somebody please guide me on what I am doing wrong here? Additionally, I would greatly appreciate if you could tell me how to check if a certain product is part of a certain category.  Thank you very much &amp; all the best,  Lukas")*

I am kinda stuck, do you have any other ideas how it could be possible to clean the cart in the onBeforeCartUpdate function?

thanks!!

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50Lzg3MDQ0Ni1jbGVhci1jYXJ0LWJlZm9yZS1hLWNlcnRhaW4tcHJvZHVjdC1pcy1hZGRlZD90bXBsPW1hcmtkb3du) 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)
-

12 years 2 months ago [\#168325](#168325) by [Xavier](/forum/user/4105-xavier "View Xavier's Profile")  Replied by *[Xavier](/forum/user/4105-xavier "View Xavier's Profile")* on topic *[Clear cart before a certain product is added](/forum/31-development/870446-clear-cart-before-a-certain-product-is-added#168325 "-- HikaShop version -- : current -- Joomla version -- : current 3.3 -- PHP version -- : current  hi!  I would like to create a plugin that does something when a product is added to the cart:  - It should check whether the product is part of a certain category. If this is the case, the plugin should empty the cart and then add the product to the cart, making it the only product in the cart  - If a certain product is added to the cart, it should change the shop's currency  Right now, I am working on the first part, but I do not even manage to make the cart get cleared for every product added. What I tried is this:  It clears the cart, but the product is not added afterwards. Can somebody please guide me on what I am doing wrong here? Additionally, I would greatly appreciate if you could tell me how to check if a certain product is part of a certain category.  Thank you very much &amp; all the best,  Lukas")*

Hi,

If it's working fine but when you add the same item, why not simply add a check in order to get the cart content, check the product to add, and if it's the same just set $do to false.

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

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

Less More

- Posts: 13
- Thank you received: 2
-
-

12 years 1 month ago [\#169135](#169135) by [erhartm](/forum/user/33354-erhartm "View erhartm's Profile")  Replied by *[erhartm](/forum/user/33354-erhartm "View erhartm's Profile")* on topic *[Clear cart before a certain product is added](/forum/31-development/870446-clear-cart-before-a-certain-product-is-added#169135 "-- HikaShop version -- : current -- Joomla version -- : current 3.3 -- PHP version -- : current  hi!  I would like to create a plugin that does something when a product is added to the cart:  - It should check whether the product is part of a certain category. If this is the case, the plugin should empty the cart and then add the product to the cart, making it the only product in the cart  - If a certain product is added to the cart, it should change the shop's currency  Right now, I am working on the first part, but I do not even manage to make the cart get cleared for every product added. What I tried is this:  It clears the cart, but the product is not added afterwards. Can somebody please guide me on what I am doing wrong here? Additionally, I would greatly appreciate if you could tell me how to check if a certain product is part of a certain category.  Thank you very much &amp; all the best,  Lukas")*

thanks, very good idea, that makes it work ALMOST all the time.

sometimes, and I have no idea when (no pattern recognizable), it just says "the cart is empty".

is there any other way to delete all items from the cart without calling resetCart()?

thanks once again!

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

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

Less More

- Posts: 86290
- Thank you received: 14268
-
-

12 years 1 month ago [\#169144](#169144) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Clear cart before a certain product is added](/forum/31-development/870446-clear-cart-before-a-certain-product-is-added#169144 "-- HikaShop version -- : current -- Joomla version -- : current 3.3 -- PHP version -- : current  hi!  I would like to create a plugin that does something when a product is added to the cart:  - It should check whether the product is part of a certain category. If this is the case, the plugin should empty the cart and then add the product to the cart, making it the only product in the cart  - If a certain product is added to the cart, it should change the shop's currency  Right now, I am working on the first part, but I do not even manage to make the cart get cleared for every product added. What I tried is this:  It clears the cart, but the product is not added afterwards. Can somebody please guide me on what I am doing wrong here? Additionally, I would greatly appreciate if you could tell me how to check if a certain product is part of a certain category.  Thank you very much &amp; all the best,  Lukas")*

Yes. You can delete the reference to the cart in the table hikashop_cart and hikashop_cart_product with SQL queries.

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

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

Less More

- Posts: 13
- Thank you received: 2
-
-

12 years 1 month ago [\#169485](#169485) by [erhartm](/forum/user/33354-erhartm "View erhartm's Profile")  Replied by *[erhartm](/forum/user/33354-erhartm "View erhartm's Profile")* on topic *[Clear cart before a certain product is added](/forum/31-development/870446-clear-cart-before-a-certain-product-is-added#169485 "-- HikaShop version -- : current -- Joomla version -- : current 3.3 -- PHP version -- : current  hi!  I would like to create a plugin that does something when a product is added to the cart:  - It should check whether the product is part of a certain category. If this is the case, the plugin should empty the cart and then add the product to the cart, making it the only product in the cart  - If a certain product is added to the cart, it should change the shop's currency  Right now, I am working on the first part, but I do not even manage to make the cart get cleared for every product added. What I tried is this:  It clears the cart, but the product is not added afterwards. Can somebody please guide me on what I am doing wrong here? Additionally, I would greatly appreciate if you could tell me how to check if a certain product is part of a certain category.  Thank you very much &amp; all the best,  Lukas")*

just for others if you have a similar problem - I did it this way (it also changes the currency if certain products are added):

function onBeforeCartUpdate(&amp;$cartClass,&amp;$cart,$product_id,$quantity,$add,$type,$resetCartWhenUpdate,$force,&amp;$do)
{
$app=&amp;JFactory::getApplication();
$pointProducts=array(4,5,6,7);
if (in_array($product_id,$pointProducts)) {
$app-&gt;setUserState( 'com_hikashop.currency_id',1);
} else
{
$app-&gt;setUserState( 'com_hikashop.currency_id',182);
}
$full = $cartClass-&gt;loadFullCart(true,true,true);
$rows = $full-&gt;products;
$first=array_shift(array_values($rows));
if ($first-&gt;product_id==$product_id)
{
$do=false;
return false;
} else
{
$cartClass-&gt;resetCart(false);
$cart=$cartClass-&gt;initCart();
$do=true;
return true;
}
}

The following user(s) said Thank You: [nicolas](/forum/user/63-nicolas "View nicolas's Profile")

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50Lzg3MDQ0Ni1jbGVhci1jYXJ0LWJlZm9yZS1hLWNlcnRhaW4tcHJvZHVjdC1pcy1hZGRlZD90bXBsPW1hcmtkb3du) 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.393 seconds

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

[](#top)
