Error messages using AJAX AddToCart

  • Posts: 141
  • Thank you received: 3
  • Hikashop Business
1 year 9 months ago #343162

-- HikaShop version -- : 4.6.1
-- Joomla version -- : 3.10.10
-- PHP version -- : 7.4.30

I have a HikaShop plugin written years ago to prevent Customers in a specific User group from adding a particular item to their cart if they don't have meet specific requirements, which has quietly worked for years (These customers are partners of the site's company and understand how they are restricted.). However, a new employee to whom this applies wasn't informed, so we learned that since the site now uses the AJAX add to cart process, the error message - which uses Joomla's "enqueueMessage" methods - no longer displays to the user until they move to some different page or physically reload.

The plugin still works the way it was written, and I see that I need to change onBeforeCartUpdate() to onBeforeCartSave(), but being AJAX, is there a way to deliver an error message to a user who fails to qualify for this AddToCart process?

Also, the AJAX interaction makes it very difficult to review the structure of the $element object argument. Is there full documentation of the $element object?

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

  • Posts: 81509
  • Thank you received: 13064
  • MODERATOR
1 year 9 months ago #343164

Hi,

For AJAX debugging, the simplest is to echo your debug and finish with "exit;". Then, before you do the AJAX action in your browser, open the browser console and make sure that the "network" tab is activated.
Then, do the action and in the network tab, you'll see the AJAX request being sent and if you open it, you can access the response. There, you'll get your debug.
This works for HikaShop's AJAX but it will work for any kind of AJAX process anywhere so it's a good thing to know.

To display the error, what you can do is this:

$cartClass = hikashop_get('class.cart');
$cartClass->addMessage($cart, array('msg'=>'My error message', 'type' => 'error'));
That way, when the system will display your message as a notification box on the page.

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

  • Posts: 141
  • Thank you received: 3
  • Hikashop Business
1 year 9 months ago #343169

Thank you very much. I will give this a shot soon.

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

  • Posts: 141
  • Thank you received: 3
  • Hikashop Business
1 year 9 months ago #343172

I appreciate the AJAX troubleshooting tips. They are working well for me. However, I don't think they are what will help me with this challenge.

Let's tackle one issue at a time.

First, I'm using the Hikashop plugin trigger onBeforeCartUpdate to block adding an item to the cart if my tests fail. I'm doing this by setting the method's argument &$do to false. Is that the correct approach, even after I swap this trigger call for the non-deprecated onBeforeCartSave method? (the onBeforeCartUpdate currently still seems to work.)

Second, this is all meant to be the result of using the Add To Cart button on a Product display page - the page layout displays a mini-cart module. The Add To Cart button runs the AJAX script in hikashop.js, and that script normally ends with a floating window that announces that the item was added to the cart. What I would prefer is to put a message which says adding the item failed either into that floater or into the mini-cart.

To test your suggested code, I commented out ALL of my code in the onBeforeCartUpdate method and added your code. I expected that would insert the test message into the floating success notice or the mini-cart when adding ANY item, since I'm no longer testing OR setting the $do value to false. The result was that the item was added to the cart normally, but I still get no test message. I've checked that I have not overridden the layout for the mini-cart. I'm not sure where to find the layout for the floating window.

This is no longer an urgent issue for me, so you can take your time.

Thanks.

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

  • Posts: 81509
  • Thank you received: 13064
  • MODERATOR
1 year 9 months ago #343177

Hi,

The event onBeforeCartUpdate shouldn't work with recent versions of HikaShop. That event is just not triggered anymore by HikaShop. You can indeed use onBeforeCartSave instead.
Another possibility you can use is to use the event onAfterProductCheckQuantities(&$products, &$cart, $options). This event is triggered specifically when products are added to the cart while onBeforeCartSave is called whenever the cart is saved, even when no product is being added to the cart. In onAfterProductCheckQuantities, you don't have a $do variable. Instead, you can set the quantity in $products to 0 to cancel the adding of a product to the cart (in some setup, it's possible to add several products to the cart at the same time, that's why it's an array of products).

If you don't set $do to false, then the message you set is ignored.
To really test how the message is displayed, you need to set $do to false.

Regarding where and how the message is displayed, you want to look into the settings of the HikaShop cart notification plugin in the joomla plugins manager. It has settings on how the add to cart is handled.

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

  • Posts: 141
  • Thank you received: 3
  • Hikashop Business
1 year 9 months ago #343201

Alright. I'll look into these options. Thanks again!

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

  • Posts: 141
  • Thank you received: 3
  • Hikashop Business
1 year 9 months ago #343204

I have switched the method to onBeforeCartSave, and confirmed that setting $do to fall does prevent adding to cart. It modifies the popup to indicate that the item was not added, but the custom message I sent using your suggested code does not appear.

(The onBeforeCartUpdate is deprecated and I will definitely stop using it, but since my error message is still making it out to the next loaded page via the enqueueMessage method, the Trigger Event for it must still have been thrown. Perhaps it isn't receiving its full complement of arguments any more, but it's definitely being called from somewhere.)

I'm going to fight with onBeforeCartSave right now, but I will ultimately check out the other Triggered Methods you discussed.

The Hikashop Cart notification Plugin tips have helped. I did not know about that one. I have switched the display to Popup, which is more functional for our flow, but the custom message is still not appearing.

Your tips for AJAX troubleshooting are becoming more helpful as I experiment with them. I am now able to return the structure of the $element object, which will be of great use to me.

I really appreciate all your assistance.

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

  • Posts: 141
  • Thank you received: 3
  • Hikashop Business
1 year 9 months ago #343206

Progress!
My custom message IS being handed back within the AJAX response, it's just not being displayed in the Popup.

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

  • Posts: 81509
  • Thank you received: 13064
  • MODERATOR
1 year 9 months ago #343205

Hi,

To check if onBeforeCartUpdate is called, you can add such code at the beginning:
var_dump(debug_backtrace());exit;
That will output the callstack if called so you'll know where it is called from.
I checked the whole code of HikaShop and I couldn't find onBeforeCartUpdate so I doubt it comes from HikaShop.
Chances are that the message you see is coming from onBeforeCartSave.
Note that undisplayed messages are stored in the session to be displayed later, when possible. So it might be this.
Now, without knowing the code of that plugin and how the shop is configured, it will be hard to say exactly what's going on.

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

Time to create page: 0.069 seconds
Powered by Kunena Forum