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

#   Error messages using AJAX AddToCart

##

- **[icomex](/forum/user/4967-icomex "View icomex's Profile")**
- [![icomex's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar4967.png)](/forum/user/4967-icomex "View icomex's Profile") Topic Author
- [ Offline](/forum/user/4967-icomex)

Less More

- Posts: 149
- Thank you received: 6
-
-

4 years 3 months ago [\#343162](#343162) by [icomex](/forum/user/4967-icomex "View icomex's Profile")  *[Error messages using AJAX AddToCart](/forum/31-development/904723-error-messages-using-ajax-addtocart#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 &quot;enqueueMessage&quot; 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?")* was created by *[icomex](/forum/user/4967-icomex "View icomex's Profile")*

-- 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](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50LzkwNDcyMy1lcnJvci1tZXNzYWdlcy11c2luZy1hamF4LWFkZHRvY2FydD90bXBsPW1hcmtkb3du) 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?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86320
- Thank you received: 14279
-
-

4 years 3 months ago [\#343164](#343164) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Error messages using AJAX AddToCart](/forum/31-development/904723-error-messages-using-ajax-addtocart#343164 "-- 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 &quot;enqueueMessage&quot; 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?")*

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: Code:

$cartClass = hikashop_get('class.cart'); $cartClass-&gt;addMessage($cart, array('msg'=&gt;'My error message', 'type' =&gt; 'error'));

That way, when the system will display your message as a notification box on the page.

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

- **[icomex](/forum/user/4967-icomex "View icomex's Profile")**
- [![icomex's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar4967.png)](/forum/user/4967-icomex "View icomex's Profile") Topic Author
- [ Offline](/forum/user/4967-icomex)

Less More

- Posts: 149
- Thank you received: 6
-
-

4 years 3 months ago [\#343169](#343169) by [icomex](/forum/user/4967-icomex "View icomex's Profile")  Replied by *[icomex](/forum/user/4967-icomex "View icomex's Profile")* on topic *[Error messages using AJAX AddToCart](/forum/31-development/904723-error-messages-using-ajax-addtocart#343169 "-- 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 &quot;enqueueMessage&quot; 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?")*

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

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

- **[icomex](/forum/user/4967-icomex "View icomex's Profile")**
- [![icomex's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar4967.png)](/forum/user/4967-icomex "View icomex's Profile") Topic Author
- [ Offline](/forum/user/4967-icomex)

Less More

- Posts: 149
- Thank you received: 6
-
-

4 years 3 months ago [\#343172](#343172) by [icomex](/forum/user/4967-icomex "View icomex's Profile")  Replied by *[icomex](/forum/user/4967-icomex "View icomex's Profile")* on topic *[Error messages using AJAX AddToCart](/forum/31-development/904723-error-messages-using-ajax-addtocart#343172 "-- 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 &quot;enqueueMessage&quot; 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?")*

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 **&amp;$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](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50LzkwNDcyMy1lcnJvci1tZXNzYWdlcy11c2luZy1hamF4LWFkZHRvY2FydD90bXBsPW1hcmtkb3du) 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?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86320
- Thank you received: 14279
-
-

4 years 3 months ago [\#343177](#343177) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Error messages using AJAX AddToCart](/forum/31-development/904723-error-messages-using-ajax-addtocart#343177 "-- 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 &quot;enqueueMessage&quot; 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?")*

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(&amp;$products, &amp;$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](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50LzkwNDcyMy1lcnJvci1tZXNzYWdlcy11c2luZy1hamF4LWFkZHRvY2FydD90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[icomex](/forum/user/4967-icomex "View icomex's Profile")**
- [![icomex's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar4967.png)](/forum/user/4967-icomex "View icomex's Profile") Topic Author
- [ Offline](/forum/user/4967-icomex)

Less More

- Posts: 149
- Thank you received: 6
-
-

4 years 3 months ago [\#343201](#343201) by [icomex](/forum/user/4967-icomex "View icomex's Profile")  Replied by *[icomex](/forum/user/4967-icomex "View icomex's Profile")* on topic *[Error messages using AJAX AddToCart](/forum/31-development/904723-error-messages-using-ajax-addtocart#343201 "-- 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 &quot;enqueueMessage&quot; 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?")*

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

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

- **[icomex](/forum/user/4967-icomex "View icomex's Profile")**
- [![icomex's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar4967.png)](/forum/user/4967-icomex "View icomex's Profile") Topic Author
- [ Offline](/forum/user/4967-icomex)

Less More

- Posts: 149
- Thank you received: 6
-
-

4 years 3 months ago [\#343204](#343204) by [icomex](/forum/user/4967-icomex "View icomex's Profile")  Replied by *[icomex](/forum/user/4967-icomex "View icomex's Profile")* on topic *[Error messages using AJAX AddToCart](/forum/31-development/904723-error-messages-using-ajax-addtocart#343204 "-- 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 &quot;enqueueMessage&quot; 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?")*

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](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50LzkwNDcyMy1lcnJvci1tZXNzYWdlcy11c2luZy1hamF4LWFkZHRvY2FydD90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[icomex](/forum/user/4967-icomex "View icomex's Profile")**
- [![icomex's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar4967.png)](/forum/user/4967-icomex "View icomex's Profile") Topic Author
- [ Offline](/forum/user/4967-icomex)

Less More

- Posts: 149
- Thank you received: 6
-
-

4 years 3 months ago [\#343206](#343206) by [icomex](/forum/user/4967-icomex "View icomex's Profile")  Replied by *[icomex](/forum/user/4967-icomex "View icomex's Profile")* on topic *[Error messages using AJAX AddToCart](/forum/31-development/904723-error-messages-using-ajax-addtocart#343206 "-- 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 &quot;enqueueMessage&quot; 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?")*

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

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50LzkwNDcyMy1lcnJvci1tZXNzYWdlcy11c2luZy1hamF4LWFkZHRvY2FydD90bXBsPW1hcmtkb3du) 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?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86320
- Thank you received: 14279
-
-

4 years 3 months ago [\#343205](#343205) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Error messages using AJAX AddToCart](/forum/31-development/904723-error-messages-using-ajax-addtocart#343205 "-- 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 &quot;enqueueMessage&quot; 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?")*

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](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50LzkwNDcyMy1lcnJvci1tZXNzYWdlcy11c2luZy1hamF4LWFkZHRvY2FydD90bXBsPW1hcmtkb3du) 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.145 seconds

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

[](#top)
