Catch remove coupon

  • Posts: 19
  • Thank you received: 0
  • Hikamarket Multivendor Hikashop Business
5 months 2 weeks ago #334906

-- HikaShop version -- : 4.4.3
-- Joomla version -- : 3.9.27
-- PHP version -- : 7.3.28
-- Browser(s) name and version -- : Chrome 90.0.4430.212

Hello,
i have used onAfterCartProductsLoad() to apply custom coupon based on data from third party software, the problem is that i cannot catch when coupon is removed manually from the frontpage (click on trash icon near coupon name).

What i need is to know that onAfterCartProductsLoad() is called because of a remove coupon action request, in order to avoid re-adding the same coupon.

Thank you.

Please Log in or Create an account to join the conversation.

  • Posts: 74655
  • Thank you received: 11522
  • MODERATOR
5 months 2 weeks ago #334907

Hi,

When a coupon is removed, it goes through the events onBeforeCartSave(&$cart, &$do) and onAfterCartSave(&$cart)
So you can implement one of these events in a plugin and check if $cart->cart_coupon is set and if set, if it is empty. If it is set and empty, it means that a coupon is being removed (or that there was simply no coupon already in the cart).
If it's not set, then it means that a cart is created or updated and that if there is already a coupon in the cart it's not being changed so you can ignore the call to the event.
If it's set with a non empty value, it means that a coupon code is being added to the cart.

Please Log in or Create an account to join the conversation.

  • Posts: 19
  • Thank you received: 0
  • Hikamarket Multivendor Hikashop Business
5 months 2 weeks ago #334924

Hi,
sorry but no onBeforeCartSave nor onAfterCartSave is called when i remove a coupon.
and i think is correct because coupon is per order not per cart.

is there another way to catch when a coupon is removed?

Thank you

Please Log in or Create an account to join the conversation.

  • Posts: 74655
  • Thank you received: 11522
  • MODERATOR
5 months 2 weeks ago #334931

Hi,

I'm not sure what you mean.
What the order has to do with removing a coupon from the cart ?
Or are you removing the coupon from an order in the backend ?
Or are you removing the coupon from an order in the frontend of HikaMarket as a super admin ?
In your first message, you talk about onAfterCartProductsLoad. This event is not called when you add/remove coupons from orders. It's only called when a cart is loaded. So I assumed that you were talking about removing a coupon from the cart.
And in that case, I can assure you that onBeforeCartSave nor onAfterCartSave are called.

When you do any action in the "coupon" view of your checkout, the validate function of the file administrator/components/com_hikashop/helpers/checkout/coupon.php is called.
There, if a coupon is added the function addCoupon is called, and if a coupon is removed the function removeCoupon is called.
Both are in the file administrator/components/com_hikashop/classes/cart.php and both functions finish by calling the "save" function of the same class. And the save function of administrator/components/com_hikashop/classes/cart.php will call onBeforeCartSave nor onAfterCartSave.

So I have to disagree with you. What I proposed is the solution. There are other ways to do it, like developing a system plugin and catching the correct coupon removal request based on the parameters in $_REQUEST, but it's much more convoluted than using the events I proposed.

So this leads me to believe that the problem is in how you tested whether onBeforeCartSave nor onAfterCartSave were called or not when a coupon is being removed from the cart.

Please Log in or Create an account to join the conversation.

  • Posts: 19
  • Thank you received: 0
  • Hikamarket Multivendor Hikashop Business
5 months 2 weeks ago #334943

i try to explain better whats append:

on onAfterCartProductsLoad() i create a coupon (only if certains conditions are satisfied) and add to cart this way:

$couponClass = hikashop_get('class.discount');
$couponClass->load('mycoupon'); // this already exist in the backend
$element->cart_coupon = $free_couponcode;

then hikashop call onBeforeCouponCheck() where i set the correct price this way:
$coupon->discount_flat_amount = $correctVal;

if i delete coupon from checkout page (click on trash icon) onAfterCartProductsLoad() is called, but not the onBeforeCartSave or onAfterCartSave

so i tried to put coupon generation (code above) in onAfterCartSave() that is a better place because this way i get the coupon generated only when the cart is created or effectively modified and not all times the cart is loaded and i can catch coupon removed as you explain to me in your prev post

but unfortunaly in onAfterCartSave() the same code (the one that check conditions and eventually generate and add a coupon) never gets the trigger onBeforeCouponCheck() be called! don't understand why

Please Log in or Create an account to join the conversation.

  • Posts: 74655
  • Thank you received: 11522
  • MODERATOR
5 months 2 weeks ago #334949

Hi,

I would recommend you to do it simplier.
Just create a coupon with no restrictions and the "auto load in cart if possible" setting activated.
That way, by default, the coupon will always be added to all the carts.
Then, in the onBeforeCouponCheck( &$coupon, &$total, &$zones, &$products, &$display_error, &$error_message, &$do ) trigger, you can check if the conditions are met.
If yes, then you can set the $coupon->discount_flat_amount variable.
If no, you can set $do to false.
That way the coupon is auto added and auto removed and the user doesn't have to do anything.

Regardless, with the method you talked about before, I maintain that onBeforeCartSave or onAfterCartSave are called when you delete coupon from checkout page (click on trash icon).
It's not possible is doesn't, as I explained already in my previous message:

When you do any action in the "coupon" view of your checkout, the validate function of the file administrator/components/com_hikashop/helpers/checkout/coupon.php is called.
There, if a coupon is added the function addCoupon is called, and if a coupon is removed the function removeCoupon is called.
Both are in the file administrator/components/com_hikashop/classes/cart.php and both functions finish by calling the "save" function of the same class. And the save function of administrator/components/com_hikashop/classes/cart.php will call onBeforeCartSave nor onAfterCartSave.

If you think it doesn't, it's probably the way you test if it does or not which is wrong.
For example, if you set a value in a variable somewhere during onAfterCartSave and then you expect to be able to read it in onAfterCartProductsLoad, that won't happen as onAfterCartSave is called during the AJAX request to remove the coupon, while onAfterCartProductsLoad will be called when refreshing the HTML of the different areas of the checkout.
So since these are done with different AJAX requests they won't share the variables data.
Now I don't know what you did you check if onAfterCartSave was called when a coupon is removed. I can only tell you that it is called and the problem is probably in the way you're testing that.
To make sure, you can do it like that:
function onAfterCartSave(&$cart) {
 if(isset($cart->cart_coupon)) hikashop_writeToLog('coupon is now: '.$cart->cart_coupon);
}

Please Log in or Create an account to join the conversation.

Time to create page: 0.067 seconds
Powered by Kunena Forum