- Posts: 13
- Thank you received: 2
Clear cart before a certain product is added
12 years 1 month ago #167912
by erhartm
Clear cart before a certain product is added was created by erhartm
-- 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 & all the best,
Lukas
-- 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(&$cartClass,&$cart,$product_id,$quantity,$add,$type,$resetCartWhenUpdate,$force,&$do)
{
if (($type=='product') && ($add=1)) $cartClass->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 & all the best,
Lukas
Please Log in or Create an account to join the conversation.
12 years 1 month ago #167948
by nicolas
Replied by nicolas on topic Clear cart before a certain product is added
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->initCart();
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->initCart();
Please Log in or Create an account to join the conversation.
12 years 1 month ago #168060
by erhartm
Replied by erhartm on topic Clear cart before a certain product is added
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!
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 or Create an account to join the conversation.
12 years 1 month ago #168082
by nicolas
Replied by nicolas on topic Clear cart before a certain product is added
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.
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 or Create an account to join the conversation.
12 years 1 month ago #168322
by erhartm
Replied by erhartm on topic Clear cart before a certain product is added
I am kinda stuck, do you have any other ideas how it could be possible to clean the cart in the onBeforeCartUpdate function?
thanks!!
thanks!!
Please Log in or Create an account to join the conversation.
12 years 1 month ago #168325
by Xavier
Replied by Xavier on topic Clear cart before a certain product is added
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.
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 or Create an account to join the conversation.
12 years 1 month ago #169135
by erhartm
Replied by erhartm on topic Clear cart before a certain product is added
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!
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 or Create an account to join the conversation.
12 years 1 month ago #169144
by nicolas
Replied by nicolas on topic Clear cart before a certain product is added
Yes. You can delete the reference to the cart in the table hikashop_cart and hikashop_cart_product with SQL queries.
Please Log in or Create an account to join the conversation.
12 years 4 weeks ago #169485
by erhartm
Replied by erhartm on topic Clear cart before a certain product is added
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(&$cartClass,&$cart,$product_id,$quantity,$add,$type,$resetCartWhenUpdate,$force,&$do)
{
$app=&JFactory::getApplication();
$pointProducts=array(4,5,6,7);
if (in_array($product_id,$pointProducts)) {
$app->setUserState( 'com_hikashop.currency_id',1);
} else
{
$app->setUserState( 'com_hikashop.currency_id',182);
}
$full = $cartClass->loadFullCart(true,true,true);
$rows = $full->products;
$first=array_shift(array_values($rows));
if ($first->product_id==$product_id)
{
$do=false;
return false;
} else
{
$cartClass->resetCart(false);
$cart=$cartClass->initCart();
$do=true;
return true;
}
}
function onBeforeCartUpdate(&$cartClass,&$cart,$product_id,$quantity,$add,$type,$resetCartWhenUpdate,$force,&$do)
{
$app=&JFactory::getApplication();
$pointProducts=array(4,5,6,7);
if (in_array($product_id,$pointProducts)) {
$app->setUserState( 'com_hikashop.currency_id',1);
} else
{
$app->setUserState( 'com_hikashop.currency_id',182);
}
$full = $cartClass->loadFullCart(true,true,true);
$rows = $full->products;
$first=array_shift(array_values($rows));
if ($first->product_id==$product_id)
{
$do=false;
return false;
} else
{
$cartClass->resetCart(false);
$cart=$cartClass->initCart();
$do=true;
return true;
}
}
The following user(s) said Thank You: nicolas
Please Log in or Create an account to join the conversation.
Time to create page: 0.243 seconds