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

#   Count items in shopping cart from a custom module?

##

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

Less More

- Posts: 29
- Thank you received: 1
-
-

10 years 6 months ago [\#239492](#239492) by [bshomer](/forum/user/42675-bshomer "View bshomer's Profile")  *[Count items in shopping cart from a custom module?](/forum/32-content-creation/884386-count-items-in-shopping-cart-from-a-custom-module#239492 "Hi,  Searched for this a lot and couldn't find anything...  I'm developing a custom module for embedding in my H.S. pages.  How can I obtain within that module the count of items in my shopping cart? (PHP code that can be embedded in the module html)  Thanks.")* was created by *[bshomer](/forum/user/42675-bshomer "View bshomer's Profile")*

Hi,

Searched for this a lot and couldn't find anything...

I'm developing a custom module for embedding in my H.S. pages.

How can I obtain within that module the count of items in my shopping cart?
(PHP code that can be embedded in the module html)

Thanks.

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

- **[Xavier](/forum/user/4105-xavier "View Xavier's Profile")**
- [![Xavier's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar4105.png)](/forum/user/4105-xavier "View Xavier's Profile")
- [ Offline](/forum/user/4105-xavier)

Less More

- Posts: 13201
- Thank you received: 2322
-
-
-
- [](http://www.kidiweb.fr)
-

10 years 6 months ago [\#239515](#239515) by [Xavier](/forum/user/4105-xavier "View Xavier's Profile")  Replied by *[Xavier](/forum/user/4105-xavier "View Xavier's Profile")* on topic *[Count items in shopping cart from a custom module?](/forum/32-content-creation/884386-count-items-in-shopping-cart-from-a-custom-module#239515 "Hi,  Searched for this a lot and couldn't find anything...  I'm developing a custom module for embedding in my H.S. pages.  How can I obtain within that module the count of items in my shopping cart? (PHP code that can be embedded in the module html)  Thanks.")*

Hi,

To get the content of the cart, you can use that kind of code: Code:

$cartClass = hikashop_get('class.cart'); $fullCart = $cartClass-&gt;loadFullCart(); $nbProducts = 0; foreach($fullCart-&gt;products as $product){ $nbProducts += $product-&gt;cart_product_quantity; }

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

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

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

Less More

- Posts: 29
- Thank you received: 1
-
-

10 years 6 months ago [\#239575](#239575) by [bshomer](/forum/user/42675-bshomer "View bshomer's Profile")  Replied by *[bshomer](/forum/user/42675-bshomer "View bshomer's Profile")* on topic *[Count items in shopping cart from a custom module?](/forum/32-content-creation/884386-count-items-in-shopping-cart-from-a-custom-module#239575 "Hi,  Searched for this a lot and couldn't find anything...  I'm developing a custom module for embedding in my H.S. pages.  How can I obtain within that module the count of items in my shopping cart? (PHP code that can be embedded in the module html)  Thanks.")*

Cool!
Thanks very much Xavier.
I now have a cool icon for empty and full shopping bag with the number of selected items on top.

[ ![&#91;img](https://benefrat.org/images/tmp/shoppingBagEmpty.jpg) ](https://benefrat.org/images/tmp/shoppingBagEmpty.jpg "&#91;img")

[ ![&#91;img](https://benefrat.org/images/tmp/shoppingBagFull.jpg) ](https://benefrat.org/images/tmp/shoppingBagFull.jpg "&#91;img")

It uses the modals extension from Regular Labs to display a modal window of the shopping cart.
I still have two issues.

Here's the PHP code of the module:
Code:

&lt;?php $cartClass = hikashop_get('class.cart'); $fullCart = $cartClass-&gt;loadFullCart(); $nbProducts = 0; foreach($fullCart-&gt;products as $product){ $nbProducts += $product-&gt;cart_product_quantity; } if ($nbProducts == 0){ echo "{modal /index.php?options=com_content&amp;view=article&amp;id=2}" . "&lt;div class='emptyCartIcon'&gt;&lt;/div&gt;{/modal}"; } else { echo "{modal /index.php?options=com_content&amp;view=article&amp;id=2}" . "&lt;div class='fullCartIcon'&gt;&lt;h6&gt;" . $nbProducts . "&lt;/h6&gt;&lt;/div&gt;{/modal}"; } ?&gt;

Here's the css (should anyone wish to implement it)
Code:

.emptyCartIcon{ margin-right: 2em; margin-left: 2em; width:40px; height:50px; background: url(/images/shoppingBagEmpty.png); } .fullCartIcon{ margin-right: 2em; margin-left: 2em; position: relative; width:40px; height:50px; background: url(/images/shoppingBagFull.png); } .fullCartIcon h6{ display: inline-block; position: absolute; bottom: 1px; right: 1px; background-color: purple; color: white; font-size: 0.7em; width: 0.7em; font-weight: bold; text-align: center; -moz-border-radius:0.25em; -webkit-border-radius:0.25em; border-radius:0.25em; }

My Issues:

---

1. The icon is updated only upon page reload. This doesn't always happen.
Is there some sort of a listener I can hook to (Ajax or other), so the icon will receive an update when the cart content is changed?

2. When loading "regular" Joomla pages, not related to Hikashop, the code freezes everything and the page will not display.
I guess it is a scope issue. When a H.S. page loads, PHP gets the scope of H.S. With regular pages this doesn't happen.
Any advice on how to overcome this???

By the way, I think such an icon at the top of the shop's page should be an integral part of Hikashop.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMyLWNvbnRlbnQtY3JlYXRpb24vODg0Mzg2LWNvdW50LWl0ZW1zLWluLXNob3BwaW5nLWNhcnQtZnJvbS1hLWN1c3RvbS1tb2R1bGU/dG1wbD1tYXJrZG93bg==) 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: 14325
-
-

10 years 6 months ago [\#239579](#239579) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Count items in shopping cart from a custom module?](/forum/32-content-creation/884386-count-items-in-shopping-cart-from-a-custom-module#239579 "Hi,  Searched for this a lot and couldn't find anything...  I'm developing a custom module for embedding in my H.S. pages.  How can I obtain within that module the count of items in my shopping cart? (PHP code that can be embedded in the module html)  Thanks.")*

Hi,

1. We don't have yet a event system for the cart system (we're working on one for HikaShop 3.0). So for now, you would have to edit the file "quantity" of the view "product" via the menu Display&gt;Views and add a call to a javascript function of yours for the onclick of the add to cart button. You can add it to the $this-&gt;ajax variable.

2. You need to include the main HikaShop helper before your PHP code: Code:

if(!defined('DS')) define('DS', DIRECTORY_SEPARATOR); if(!include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')) return true;

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

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

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

Less More

- Posts: 29
- Thank you received: 1
-
-

10 years 6 months ago [\#239748](#239748) by [bshomer](/forum/user/42675-bshomer "View bshomer's Profile")  Replied by *[bshomer](/forum/user/42675-bshomer "View bshomer's Profile")* on topic *[Count items in shopping cart from a custom module?](/forum/32-content-creation/884386-count-items-in-shopping-cart-from-a-custom-module#239748 "Hi,  Searched for this a lot and couldn't find anything...  I'm developing a custom module for embedding in my H.S. pages.  How can I obtain within that module the count of items in my shopping cart? (PHP code that can be embedded in the module html)  Thanks.")*

Thanks!!!
The HikaShop helper code did the job alright! Works great!

I didn't quite understand where exactly can I place my Javascript call. Where is the onclick call of Add To Cart in quantity.php?

I think I will simply invoke a document reload with the onclick. (Any better idea?)
I would really appreciate a more detailed explanation as I'm really new to Hikashop.

THANKS!

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMyLWNvbnRlbnQtY3JlYXRpb24vODg0Mzg2LWNvdW50LWl0ZW1zLWluLXNob3BwaW5nLWNhcnQtZnJvbS1hLWN1c3RvbS1tb2R1bGU/dG1wbD1tYXJrZG93bg==) 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: 14325
-
-

10 years 6 months ago [\#239753](#239753) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Count items in shopping cart from a custom module?](/forum/32-content-creation/884386-count-items-in-shopping-cart-from-a-custom-module#239753 "Hi,  Searched for this a lot and couldn't find anything...  I'm developing a custom module for embedding in my H.S. pages.  How can I obtain within that module the count of items in my shopping cart? (PHP code that can be embedded in the module html)  Thanks.")*

Hi,

After the line: Code:

$this-&gt;ajax = 'return hikashopModifyQuantity(\''.$this-&gt;row-&gt;product_id.'\',field,1,0,\'cart\','.$module_id.')';

You can add such code: Code:

$this-&gt;ajax = 'myjavascriptfunction();'.$this-&gt;ajax;

in the product/quantity.php view file.
The $this-&gt;ajax variable is added automatically to the onclick of the add to cart button.

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

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