JS Events get current value of cart

  • Posts: 44
  • Thank you received: 7
  • Hikashop Business
1 week 6 days ago #367582

Hi, how to using js events www.hikashop.com/support/documentation/6...tion.html#CartUpdate and for example cart.updated event get current cart value (total value of product added to cart)?

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

  • Posts: 83997
  • Thank you received: 13611
  • MODERATOR
1 week 6 days ago #367586

Hi,

There are 3 things:

1. First, you need to add your javascript code on your pages. How to add that code to your website depends on what you're trying to do. There are many ways to go about it.
For example, this page presents several of them:
www.infyways.com/add-custom-javascript-to-joomla/
I would rather recommend this:
www.joomlashack.com/blog/joomla/custom-js/

2. Now that you know how to add javascript to your Joomla website, you want to know how your javascript code can be called when the cart is updated.
The documentation about javascript events actually starts with explanations on how to do it:
www.hikashop.com/support/documentation/6...ocumentation.html#js
So, as explained there, you would want to have something like this in your javascript code:

if(window.Oby) {
	window.Oby.registerAjax(["cart.updated"],function(params){
		// ... your javascript code ...
	});
}

3. Then, you want to know how to get the total value of products in the cart.
As explained in the documentation you're pointing at, you have an array of product objects in params.resp.products
There, you have access to product_id which provides you with the id of the product and quantity which provides you with the quantity of the product in the cart.
You have two ways to go about it:
- you could create a plugin following :
webkul.com/blog/ajax-call-from-plugins-and-modules-in-joomla/
and in your javascript, use the JS code there to call your plugin to retrieve the information on the product price, or even the cart total amount.
- but I would recommend implementing instead the onGetCartProductsInfo event of the Cart API:
www.hikashop.com/support/documentation/6...nGetCartProductsInfo
With it, you'll be able to directly add extra data about the cart, like the total amount, in params.resp

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

  • Posts: 44
  • Thank you received: 7
  • Hikashop Business
1 week 3 days ago #367600

I'am adding js code directly in plugin that I'am developing in onAfterCartProductsLoad event. But how to add $ret with total cart value to js params.resp using onGetCartProductsInfo(&$cart, &$ret) event?

ok, I got it:

    public function onGetCartProductsInfo(&$cart, &$ret){

        $ret['total_price'] = $cart -> total ->prices[0] -> price_value_with_tax;
   
    }

Last edit: 1 week 3 days ago by Bprak.
The following user(s) said Thank You: nicolas

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

  • Posts: 44
  • Thank you received: 7
  • Hikashop Business
1 week 3 days ago #367604

This

window.Oby.registerAjax(["cart.updated","wishlist.updated"],function(params){


works only when cart is updated, but how to get total price in cart on product page load?

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

  • Posts: 83997
  • Thank you received: 13611
  • MODERATOR
1 week 3 days ago #367606

Hi,

You want to use the standard DOMContentLoaded event:
developer.mozilla.org/en-US/docs/Web/API...MContentLoaded_event

document.addEventListener("DOMContentLoaded", (event) => {
  // your javascript
});
And since you're probably displaying that javascript code from a PHP file, you can use HikaShop's functions to get the cart and output it.
For example:
<?php
$cartClass = hikashop_get('class.cart');
$cart = $cartClass->getFullCart();
if($cart && !empty($cart->products)) {
?>
<script>
document.addEventListener("DOMContentLoaded", (event) => {
   alert('<?php echo $cart->total->prices[0] ->price_value_with_tax; ?>');
});
</script>
<?php
}
?>

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

Time to create page: 0.064 seconds
Powered by Kunena Forum