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

  • Posts: 116
  • Thank you received: 7
  • Hikashop Business
7 years 6 months ago #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.

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

  • Posts: 82868
  • Thank you received: 13377
  • MODERATOR
7 years 6 months ago #270364

Hi,

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

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

  • Posts: 200
  • Thank you received: 75
7 years 5 months ago #270389

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:

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, mabdelaziz

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

  • Posts: 116
  • Thank you received: 7
  • Hikashop Business
7 years 5 months ago #270411

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 or Create an account to join the conversation.

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
7 years 5 months ago #270390

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 & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

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

  • Posts: 116
  • Thank you received: 7
  • Hikashop Business
7 years 5 months ago #270445

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 or Create an account to join the conversation.

Time to create page: 0.075 seconds
Powered by Kunena Forum