Custom backend coupon

  • Posts: 50
  • Thank you received: 1
  • Hikamarket Multivendor Hikashop Business
1 year 6 months ago #351471

-- HikaShop version -- : 4.7.2
-- Joomla version -- : 3.10.11
-- PHP version -- : 7.4.3
-- Browser(s) name and version -- : Chrome 90.0.4430.212

Hi,
I wish I could add custom backend coupons to the checkout page.

In this case the customer text a code in the hikashop coupon field of the checkout page, we intercept the event, call our custom backend (java app) which decides if the customer deserves one or more discounts, these discounts are generated only at the software level ( no hikashop_discount table or other), and added to the cart "on the fly", also taking advantage of the use of the multicoupon plugin with the possibility of adding an array of coupons.

Is there a way to achieve this?

For example, if we can intercept the coupon field event with the coupon code entered by the customer as parameter, check if there are available coupons on our custom backend and eventually add those coupons directly to the $cart->additional (as multicoupon plugn do), could it work?


Thanks

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
1 year 6 months ago #351477

Hi,

It's possible yes.
The multicoupon plugin lets HikaShop validate the coupon and add it to the cart, and only then takes the coupon from the cart, remove it and add it as an "additional fee" to the cart.
HikaShop has an event onBeforeCouponLoad(&$coupon, &$do) which you can implement to retrieve the text entered in the coupon input field (in $coupon). You can set $do to false to cancel the coupon adding and then add your custom code to connect to your java app, and then add your "additional fee" to the cart with a similar code to what is in the _addCouponAsAdditional function of the multicoupon plugin.
You don't even need the multicoupon plugin.

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

  • Posts: 50
  • Thank you received: 1
  • Hikamarket Multivendor Hikashop Business
1 year 6 months ago #351519

Hi,
thank you very much
How can i retrive current $cart object to set ->additional from inside event onBeforeCouponLoad(&$coupon, &$do)?

Thanks

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
1 year 6 months ago #351520

Hi,
You can do like this:

$cartClass = hikashop_get('class.cart');
$cart = $cartClass->getFullCart();
www.hikashop.com/support/documentation/6...umentation.html#code

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

  • Posts: 50
  • Thank you received: 1
  • Hikamarket Multivendor Hikashop Business
1 year 6 months ago #351548

Hi,
thank you.

Strange behavior, if I try with a coupon code that does not exist, I correctly receive the onBeforeCouponLoad event, text "invalid coupon" is written on the screen. If i try again with the same coupon code, i don't get the onBeforeCouponLoad event anymore, until i delete the cart from hikashop backend.

Is possibile to always receive onBeforeCouponLoad event in any situation?

Thanks

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
1 year 6 months ago #351551

Hi,

I don't see why that would be the case. At least, there is no mechanism in HikaShop to not trigger again the event.
Could it be a caching issue on your browser / server ?

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

  • Posts: 50
  • Thank you received: 1
  • Hikamarket Multivendor Hikashop Business
1 year 6 months ago #351597

Hi,
it happends because i set $do = false.
After that onBeforeCouponLoad never fires again for the same coupon code, until i completely remove customer cart from Hikashop backend

Thanks

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

  • Posts: 50
  • Thank you received: 1
  • Hikamarket Multivendor Hikashop Business
1 year 6 months ago #351772

Hi,
I implemented the event as you described, but unfortunately I get an "invalid coupon" error message and the total is not updated with the additional fee.

if I refresh the page (F5) the coupon (additional fee) appears correctly

Steps to reproduce the problem:

* url: test.cartsoftware.cloud:24080
* user: This email address is being protected from spambots. You need JavaScript enabled to view it.
* pwd: cv56py

The suitable product is already in the cart.
Add coupon code: 0072ca4f-6adf-4ffd-a718-d71454adaeea

1 - the coupon does not appear as an additional fee and the message "invalid coupon" appear on page

2 - simple page refresh (F5) makes the additional fee comes out correctly

ADMINISTRATOR AREA
* url: test.cartsoftware.cloud:24080/administrator
* user: hikashop
* pwd: hikashop

The extplorer plugin is already installed.

We use our custom plugin "HikaShop PortaleBM custom discount plugin" to customize coupon behaviour.
Your multicoupon plugin in not used anymore (uninstalled).

If you want to do more test (we disable coupon remove)
- remove the product from cart
- go on home page
- select blu card numbered 970624438
- on next page click on PLUS button at the row with "7 Days Z12"


Thank you

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

  • Posts: 50
  • Thank you received: 1
  • Hikamarket Multivendor Hikashop Business
1 year 6 months ago #351774

7 Days Z12 = 7 Giorni Z12 (frontend is in italian language)

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
1 year 6 months ago #351775

Hi,

In onBeforeCouponLoad, you need to return a coupon object so that HikaShop can add the coupon to the cart
Once that's done, in onAfterCartProductsLoad, you can get the coupon from $cart->cart_coupon, empty it, and add it as an additional.

You get a "coupon valid" error because of the $do set to false without returning a coupon object.

The following user(s) said Thank You: matmosis

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

  • Posts: 50
  • Thank you received: 1
  • Hikamarket Multivendor Hikashop Business
1 year 5 months ago #352390

Hello,
in onBeforeCouponLoad(&$coupon, &$do) i set $do = false; and return $coupon;

Despite this, onAfterCouponCheck is triggered and hikashop still do the check routine on coupon, that does not exist on hikashop, in fact we get several notice and warnign (see one of those at the end of this)

if I have not misunderstood, $do = false avoid load of the coupon, and I also thought the check was avoided, is'n it?

Is there a way to avoid coupon check?
Coupon does not exist on hikashop, is managed by our custom backend
Those several warnig and notice with stacktrace are annoing and make log analysis more difficult

Thank you

Stacktrace (one example):

[15-Jun-2023 14:25:16 Europe/Berlin] PHP Notice: Trying to get property 'discount_start' of non-object in /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/administrator/components/com_hikashop/classes/discount.php on line 242
[15-Jun-2023 14:25:16 Europe/Berlin] PHP Stack trace:
[15-Jun-2023 14:25:16 Europe/Berlin] PHP 1. {main}() /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/index.php:0
[15-Jun-2023 14:25:16 Europe/Berlin] PHP 2. Joomla\CMS\Application\SiteApplication->execute() /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/index.php:49
[15-Jun-2023 14:25:16 Europe/Berlin] PHP 3. Joomla\CMS\Application\SiteApplication->doExecute() /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/libraries/src/Application/CMSApplication.php:225
[15-Jun-2023 14:25:16 Europe/Berlin] PHP 4. Joomla\CMS\Application\SiteApplication->dispatch() /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/libraries/src/Application/SiteApplication.php:233
[15-Jun-2023 14:25:16 Europe/Berlin] PHP 5. Joomla\CMS\Component\ComponentHelper::renderComponent() /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/libraries/src/Application/SiteApplication.php:194
[15-Jun-2023 14:25:16 Europe/Berlin] PHP 6. Joomla\CMS\Component\ComponentHelper::executeComponent() /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/libraries/src/Component/ComponentHelper.php:377
[15-Jun-2023 14:25:16 Europe/Berlin] PHP 7. require_once() /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/libraries/src/Component/ComponentHelper.php:402
[15-Jun-2023 14:25:16 Europe/Berlin] PHP 8. checkoutController->execute() /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/components/com_hikashop/hikashop.php:88
[15-Jun-2023 14:25:16 Europe/Berlin] PHP 9. checkoutController->execute() /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/administrator/components/com_hikashop/helpers/helper.php:2437
[15-Jun-2023 14:25:16 Europe/Berlin] PHP 10. checkoutController->submitblock() /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/libraries/src/MVC/Controller/BaseController.php:702
[15-Jun-2023 14:25:16 Europe/Berlin] PHP 11. hikashopCheckoutCouponHelper->validate() /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/components/com_hikashop/controllers/checkout.php:314
[15-Jun-2023 14:25:16 Europe/Berlin] PHP 12. hikashopCartClass->addCoupon() /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/administrator/components/com_hikashop/helpers/checkout/coupon.php:41
[15-Jun-2023 14:25:16 Europe/Berlin] PHP 13. hikashopDiscountClass->loadAndCheck() /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/administrator/components/com_hikashop/classes/cart.php:2551
[15-Jun-2023 14:25:16 Europe/Berlin] PHP 14. hikashopDiscountClass->check() /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/administrator/components/com_hikashop/classes/discount.php:179
[15-Jun-2023 14:25:16 Europe/Berlin] PHP 15. hikashopDiscountClass->couponChecks() /home/administrator/workspaceneonpdt/bs.joomla.revamp2021/administrator/components/com_hikashop/classes/discount.php:194

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
1 year 5 months ago #352400

Hi,

You missunderstood me.
When you set $do to false in onBeforeCouponLoad HikaShop will use the return of your function as the coupon object.
So the object you return needs to have the same attributes (same name of attribute). The values can all be empty though.
You get these notices / warnings because the object you provide doesn't contain the normal attributes HikaShop is expecting.

The fact that onAfterCouponCheck is called is normal and not a problem in your case. Just provide empty attributes in your coupon object and the checks done on the coupon will go through unimpeeded.
The checks on the coupon are based on what you provide, not what is in the database.
So for example, if in $coupon you have:
$coupon->discount_access = 'xxx';
The checks will check that the current user is in the group xxx
But if you have instead:
$coupon->discount_access = 'all';
the checks will skip the user group verification.
And if you don't provide discount_access in $coupon, then HikaShop will output a warning because this should normally never happen.

The following user(s) said Thank You: matmosis

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

Time to create page: 0.082 seconds
Powered by Kunena Forum