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

#   Is there a Javascript or Ajax function to access cart contents?

##

- **[stratacorps](/forum/user/33546-stratacorps "View stratacorps's Profile")**
- [![stratacorps's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/33546-stratacorps "View stratacorps's Profile") Topic Author
- [ Offline](/forum/user/33546-stratacorps)

Less More

- Posts: 116
- Thank you received: 7
-

9 years 6 months ago [\#270357](#270357) by [stratacorps](/forum/user/33546-stratacorps "View stratacorps's Profile")  *[Is there a Javascript or Ajax function to access cart contents?](/forum/27-checkout/890404-is-there-a-javascript-or-ajax-function-to-access-cart-contents#270357 "-- HikaShop version -- : 3.1.0 -- Joomla version -- : 3.7.2 -- PHP version -- : 7.0  We wanted to hide menus and/or modules based on the content of the shopping cart. A simple approach was to just check the number of items in the cart to see if it were empty and then trigger a jQuery or JS dom to change the object to hidden or visible, etc.  I know we can do this with PHP based on your development docs, but I was hoping I could just write a JS routine and put that into the template via a custom module, etc. to automatically hide or show an item based on the cart status.")* was created by *[stratacorps](/forum/user/33546-stratacorps "View stratacorps's Profile")*

-- HikaShop version -- : 3.1.0
-- Joomla version -- : 3.7.2
-- PHP version -- : 7.0

We wanted to hide menus and/or modules based on the content of the shopping cart. A simple approach was to just check the number of items in the cart to see if it were empty and then trigger a jQuery or JS dom to change the object to hidden or visible, etc.

I know we can do this with PHP based on your development docs, but I was hoping I could just write a JS routine and put that into the template via a custom module, etc. to automatically hide or show an item based on the cart status.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI3LWNoZWNrb3V0Lzg5MDQwNC1pcy10aGVyZS1hLWphdmFzY3JpcHQtb3ItYWpheC1mdW5jdGlvbi10by1hY2Nlc3MtY2FydC1jb250ZW50cz90bXBsPW1hcmtkb3du) 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: 86587
- Thank you received: 14326
-
-

9 years 6 months ago [\#270364](#270364) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Is there a Javascript or Ajax function to access cart contents?](/forum/27-checkout/890404-is-there-a-javascript-or-ajax-function-to-access-cart-contents#270364 "-- HikaShop version -- : 3.1.0 -- Joomla version -- : 3.7.2 -- PHP version -- : 7.0  We wanted to hide menus and/or modules based on the content of the shopping cart. A simple approach was to just check the number of items in the cart to see if it were empty and then trigger a jQuery or JS dom to change the object to hidden or visible, etc.  I know we can do this with PHP based on your development docs, but I was hoping I could just write a JS routine and put that into the template via a custom module, etc. to automatically hide or show an item based on the cart status.")*

Hi,

There is no javascript you can use to do that.
That will require PHP as far as I can see.

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

- **[GW](/forum/user/33851-gw "View GW's Profile")**
- [![GW's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/33851-gw "View GW's Profile")
- [ Offline](/forum/user/33851-gw)

Less More

- Posts: 200
- Thank you received: 75
-
-

9 years 6 months ago [\#270389](#270389) by [GW](/forum/user/33851-gw "View GW's Profile")  Replied by *[GW](/forum/user/33851-gw "View GW's Profile")* on topic *[Is there a Javascript or Ajax function to access cart contents?](/forum/27-checkout/890404-is-there-a-javascript-or-ajax-function-to-access-cart-contents#270389 "-- HikaShop version -- : 3.1.0 -- Joomla version -- : 3.7.2 -- PHP version -- : 7.0  We wanted to hide menus and/or modules based on the content of the shopping cart. A simple approach was to just check the number of items in the cart to see if it were empty and then trigger a jQuery or JS dom to change the object to hidden or visible, etc.  I know we can do this with PHP based on your development docs, but I was hoping I could just write a JS routine and put that into the template via a custom module, etc. to automatically hide or show an item based on the cart status.")*

Hi,

PHP would be more suitable, Javascript will never be 100% reliable/foolproof, but if that's no problem it's not impossible to do with JS.
If you have a cart module on all your pages for example, you can easily write a few lines of JS code to check if there are products in this cart module or not. There's also a trigger event which you can readily use for when the user adds or removes a product from the cart:

Code:

window.Oby.registerAjax(["cart.updated","wishlist.updated"],function(params){ /* params.type : 'cart' or 'wishlist' params.id : the cart id. If NaN, it has failed. params.el : the dom element where the customer clicked to add the product into the cart params.product_id : the product id params.resp : object ret : the cart id, if 0, it has failed product_name : the name of the product quantity : the quantity of the product image : the url of the product image (50x50) */ });

With this you should be able to achieve what you want. Note that there can be a delay before the Ajax call is finished and the products are updated in the cart module which you have to take into account.

Hope that helps!

The following user(s) said Thank You: [stratacorps](/forum/user/33546-stratacorps "View stratacorps's Profile"), [mabdelaziz](/forum/user/62648-mabdelaziz "View mabdelaziz's Profile")

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

- **[stratacorps](/forum/user/33546-stratacorps "View stratacorps's Profile")**
- [![stratacorps's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/33546-stratacorps "View stratacorps's Profile") Topic Author
- [ Offline](/forum/user/33546-stratacorps)

Less More

- Posts: 116
- Thank you received: 7
-

9 years 6 months ago [\#270411](#270411) by [stratacorps](/forum/user/33546-stratacorps "View stratacorps's Profile")  Replied by *[stratacorps](/forum/user/33546-stratacorps "View stratacorps's Profile")* on topic *[Is there a Javascript or Ajax function to access cart contents?](/forum/27-checkout/890404-is-there-a-javascript-or-ajax-function-to-access-cart-contents#270411 "-- HikaShop version -- : 3.1.0 -- Joomla version -- : 3.7.2 -- PHP version -- : 7.0  We wanted to hide menus and/or modules based on the content of the shopping cart. A simple approach was to just check the number of items in the cart to see if it were empty and then trigger a jQuery or JS dom to change the object to hidden or visible, etc.  I know we can do this with PHP based on your development docs, but I was hoping I could just write a JS routine and put that into the template via a custom module, etc. to automatically hide or show an item based on the cart status.")*

Shows you want I didn't know. I was afraid that I would replace the existing ajax functionality...I have read some of the examples using window.Oby, but didn't realize that my function would just be appended to the existing ones!

Thanks!! I realize PHP would be best, but if the module or menu is already on the page, JS is the only way to change it without a page refresh. Its as simple now for me to just change a style selector and append a ".hide" class to an Id to hide/un-hide what I need.

thanks again!

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

- **[Jerome](/forum/user/80-jerome "View Jerome's Profile")**
- [![Jerome's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar80.png)](/forum/user/80-jerome "View Jerome's Profile")
- [ Offline](/forum/user/80-jerome)
- HikaShop core developer

Less More

- Posts: 26402
- Thank you received: 4081
-
- [](http://obsi.dev)
-

9 years 6 months ago [\#270390](#270390) by [Jerome](/forum/user/80-jerome "View Jerome's Profile")  Replied by *[Jerome](/forum/user/80-jerome "View Jerome's Profile")* on topic *[Is there a Javascript or Ajax function to access cart contents?](/forum/27-checkout/890404-is-there-a-javascript-or-ajax-function-to-access-cart-contents#270390 "-- HikaShop version -- : 3.1.0 -- Joomla version -- : 3.7.2 -- PHP version -- : 7.0  We wanted to hide menus and/or modules based on the content of the shopping cart. A simple approach was to just check the number of items in the cart to see if it were empty and then trigger a jQuery or JS dom to change the object to hidden or visible, etc.  I know we can do this with PHP based on your development docs, but I was hoping I could just write a JS routine and put that into the template via a custom module, etc. to automatically hide or show an item based on the cart status.")*

Hello,

That Javascript event is only when the cart is dynamically updated in the page ; it does not help for the page loading.
For that page loading, only PHP code allow to access to the cart content.

Regards,

---

Jerome - Obsidev.com
HikaMarket &amp; HikaSerial developer / HikaShop core dev team.

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

- **[stratacorps](/forum/user/33546-stratacorps "View stratacorps's Profile")**
- [![stratacorps's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/33546-stratacorps "View stratacorps's Profile") Topic Author
- [ Offline](/forum/user/33546-stratacorps)

Less More

- Posts: 116
- Thank you received: 7
-

9 years 6 months ago [\#270445](#270445) by [stratacorps](/forum/user/33546-stratacorps "View stratacorps's Profile")  Replied by *[stratacorps](/forum/user/33546-stratacorps "View stratacorps's Profile")* on topic *[Is there a Javascript or Ajax function to access cart contents?](/forum/27-checkout/890404-is-there-a-javascript-or-ajax-function-to-access-cart-contents#270445 "-- HikaShop version -- : 3.1.0 -- Joomla version -- : 3.7.2 -- PHP version -- : 7.0  We wanted to hide menus and/or modules based on the content of the shopping cart. A simple approach was to just check the number of items in the cart to see if it were empty and then trigger a jQuery or JS dom to change the object to hidden or visible, etc.  I know we can do this with PHP based on your development docs, but I was hoping I could just write a JS routine and put that into the template via a custom module, etc. to automatically hide or show an item based on the cart status.")*

Thanks. I've got a check in PHP with the page with the custom module (script) loads--that way if the user has returned and still has items in the cart, etc. before the ajax update is triggered for a new visit; and then the javascript call added to recheck any time that routine is triggered.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI3LWNoZWNrb3V0Lzg5MDQwNC1pcy10aGVyZS1hLWphdmFzY3JpcHQtb3ItYWpheC1mdW5jdGlvbi10by1hY2Nlc3MtY2FydC1jb250ZW50cz90bXBsPW1hcmtkb3du) 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.057 seconds

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