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

#   JS Events get current value of cart

##

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

Less More

- Posts: 45
- Thank you received: 7
-
-

1 year 4 months ago [\#367582](#367582) by [Bprak](/forum/user/34235-bprak "View Bprak's Profile")  *[JS Events get current value of cart](/forum/31-development/909507-js-events-get-current-value-of-cart#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)?")* was created by *[Bprak](/forum/user/34235-bprak "View Bprak's Profile")*

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

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

Less More

- Posts: 86290
- Thank you received: 14268
-
-

1 year 4 months ago [\#367586](#367586) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[JS Events get current value of cart](/forum/31-development/909507-js-events-get-current-value-of-cart#367586 "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)?")*

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/](https://www.infyways.com/add-custom-javascript-to-joomla/)
I would rather recommend this:
[ www.joomlashack.com/blog/joomla/custom-js/](https://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](https://www.hikashop.com/support/documentation/62-hikashop-developer-documentation.html#js)
So, as explained there, you would want to have something like this in your javascript code: 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/](https://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](https://www.hikashop.com/support/documentation/62-hikashop-developer-documentation.html#onGetCartProductsInfo)
With it, you'll be able to directly add extra data about the cart, like the total amount, in params.resp

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

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

Less More

- Posts: 45
- Thank you received: 7
-
-

1 year 4 months ago -  1 year 4 months ago [\#367600](#367600) by [Bprak](/forum/user/34235-bprak "View Bprak's Profile")  Replied by *[Bprak](/forum/user/34235-bprak "View Bprak's Profile")* on topic *[JS Events get current value of cart](/forum/31-development/909507-js-events-get-current-value-of-cart#367600 "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)?")*

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(&amp;$cart, &amp;$ret) event?

ok, I got it:
Code:

public function onGetCartProductsInfo(&amp;$cart, &amp;$ret){ $ret['total_price'] = $cart -&gt; total -&gt;prices[0] -&gt; price_value_with_tax; }

Last edit: 1 year 4 months ago by [Bprak](/forum/user/34235-bprak "View Bprak's Profile").

The following user(s) said Thank You: [nicolas](/forum/user/63-nicolas "View nicolas's Profile")

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

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

Less More

- Posts: 45
- Thank you received: 7
-
-

1 year 4 months ago [\#367604](#367604) by [Bprak](/forum/user/34235-bprak "View Bprak's Profile")  Replied by *[Bprak](/forum/user/34235-bprak "View Bprak's Profile")* on topic *[JS Events get current value of cart](/forum/31-development/909507-js-events-get-current-value-of-cart#367604 "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)?")*

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

Less More

- Posts: 86290
- Thank you received: 14268
-
-

1 year 4 months ago [\#367606](#367606) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[JS Events get current value of cart](/forum/31-development/909507-js-events-get-current-value-of-cart#367606 "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)?")*

Hi,

You want to use the standard DOMContentLoaded event:
[ developer.mozilla.org/en-US/docs/Web/API...MContentLoaded\_event](https://developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoaded_event)Code:

document.addEventListener("DOMContentLoaded", (event) =&gt; { // 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: Code:

&lt;?php $cartClass = hikashop_get('class.cart'); $cart = $cartClass-&gt;getFullCart(); if($cart &amp;&amp; !empty($cart-&gt;products)) { ?&gt; &lt;script&gt; document.addEventListener("DOMContentLoaded", (event) =&gt; { alert('&lt;?php echo $cart-&gt;total-&gt;prices[0] -&gt;price_value_with_tax; ?&gt;'); }); &lt;/script&gt; &lt;?php } ?&gt;

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

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

[](#top)
