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

#   Correct PHP Code for Displaying Cart Items

##

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

Less More

- Posts: 35
- Thank you received: 0
-
-

11 years 7 months ago [\#196626](#196626) by [PigeonTech](/forum/user/35279-pigeontech "View PigeonTech's Profile")  *[Correct PHP Code for Displaying Cart Items](/forum/32-content-creation/876049-correct-php-code-for-displaying-cart-items#196626 "-- HikaShop version -- : 2.3.5 -- Joomla version -- : 3.3.6 -- PHP version -- : 5.4  I would like to display the number of cart items in a div that shows a shopping basket graphic (see top right of my website)  In an earlier post I was told I could get the number of cart items using the code below. But this variable is showing double the actual number of items in the cart. In the side menu I have a minicart which shows the correct number so presumably I just need to use the variable for the minicart.  Please could you let me know what the variable is I need.  Thanks")* was created by *[PigeonTech](/forum/user/35279-pigeontech "View PigeonTech's Profile")*

-- HikaShop version -- : 2.3.5
-- Joomla version -- : 3.3.6
-- PHP version -- : 5.4

I would like to display the number of cart items in a div that shows a shopping basket graphic (see top right of my website)

In an earlier post I was told I could get the number of cart items using the code below. But this variable is showing double the actual number of items in the cart. In the side menu I have a minicart which shows the correct number so presumably I just need to use the variable for the minicart.

Please could you let me know what the variable is I need.

Thanks

Code:

&lt;span style="font-family: courier new, courier, monospace;"&gt; &lt;?php require_once('/home/dwrftp/diywetroom.pigeontech.co.uk/administrator/components/com_hikashop/helpers/helper.php'); $class = hikashop::get('class.cart'); $rows = $class-&gt;get(); $qty = 0; if(!empty($rows)){ foreach($rows as $k =&gt;$row){ $qty += $row-&gt;cart_product_quantity; } } echo $qty; ?&gt; &lt;/span&gt;

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

- **[Mohamed Thelji](/forum/user/6861-mohamed-thelji "View Mohamed Thelji's Profile")**
- [![Mohamed Thelji's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar6861.png)](/forum/user/6861-mohamed-thelji "View Mohamed Thelji's Profile")
- [ Offline](/forum/user/6861-mohamed-thelji)

Less More

- Posts: 12953
- Thank you received: 1778
-
-
-

11 years 7 months ago [\#196652](#196652) by [Mohamed Thelji](/forum/user/6861-mohamed-thelji "View Mohamed Thelji's Profile")  Replied by *[Mohamed Thelji](/forum/user/6861-mohamed-thelji "View Mohamed Thelji's Profile")* on topic *[Correct PHP Code for Displaying Cart Items](/forum/32-content-creation/876049-correct-php-code-for-displaying-cart-items#196652 "-- HikaShop version -- : 2.3.5 -- Joomla version -- : 3.3.6 -- PHP version -- : 5.4  I would like to display the number of cart items in a div that shows a shopping basket graphic (see top right of my website)  In an earlier post I was told I could get the number of cart items using the code below. But this variable is showing double the actual number of items in the cart. In the side menu I have a minicart which shows the correct number so presumably I just need to use the variable for the minicart.  Please could you let me know what the variable is I need.  Thanks")*

Hello,
Note that you can already do that without using custom code and just by using a simple cart module, you'll just have to set the "mini cart" option to "YES"

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

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

Less More

- Posts: 35
- Thank you received: 0
-
-

11 years 7 months ago [\#196780](#196780) by [PigeonTech](/forum/user/35279-pigeontech "View PigeonTech's Profile")  Replied by *[PigeonTech](/forum/user/35279-pigeontech "View PigeonTech's Profile")* on topic *[Correct PHP Code for Displaying Cart Items](/forum/32-content-creation/876049-correct-php-code-for-displaying-cart-items#196780 "-- HikaShop version -- : 2.3.5 -- Joomla version -- : 3.3.6 -- PHP version -- : 5.4  I would like to display the number of cart items in a div that shows a shopping basket graphic (see top right of my website)  In an earlier post I was told I could get the number of cart items using the code below. But this variable is showing double the actual number of items in the cart. In the side menu I have a minicart which shows the correct number so presumably I just need to use the variable for the minicart.  Please could you let me know what the variable is I need.  Thanks")*

Hi Mohamed, did you look at my site?
You will see the mini cart is being used in the side menu on the left.
At the top-right of all my pages is a graphic of a shopping basket. You will see, just to the left of this, is a small graphic in which I echo the variable $qty taken from my php code described in my previous post.

The problem is it is adding the product options to the total number of items. Yet the minicart total shows just the main products number.

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

- **[Mohamed Thelji](/forum/user/6861-mohamed-thelji "View Mohamed Thelji's Profile")**
- [![Mohamed Thelji's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar6861.png)](/forum/user/6861-mohamed-thelji "View Mohamed Thelji's Profile")
- [ Offline](/forum/user/6861-mohamed-thelji)

Less More

- Posts: 12953
- Thank you received: 1778
-
-
-

11 years 7 months ago [\#196791](#196791) by [Mohamed Thelji](/forum/user/6861-mohamed-thelji "View Mohamed Thelji's Profile")  Replied by *[Mohamed Thelji](/forum/user/6861-mohamed-thelji "View Mohamed Thelji's Profile")* on topic *[Correct PHP Code for Displaying Cart Items](/forum/32-content-creation/876049-correct-php-code-for-displaying-cart-items#196791 "-- HikaShop version -- : 2.3.5 -- Joomla version -- : 3.3.6 -- PHP version -- : 5.4  I would like to display the number of cart items in a div that shows a shopping basket graphic (see top right of my website)  In an earlier post I was told I could get the number of cart items using the code below. But this variable is showing double the actual number of items in the cart. In the side menu I have a minicart which shows the correct number so presumably I just need to use the variable for the minicart.  Please could you let me know what the variable is I need.  Thanks")*

I just tested it with one of your product and it was correctly adding the "Square Drain Grid" to your cart, did you fix your issue ?
If not, you should probably check again what you have on your "$rows" variable and check each product/option that are added on your cart.

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

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

Less More

- Posts: 35
- Thank you received: 0
-
-

11 years 7 months ago [\#196962](#196962) by [PigeonTech](/forum/user/35279-pigeontech "View PigeonTech's Profile")  Replied by *[PigeonTech](/forum/user/35279-pigeontech "View PigeonTech's Profile")* on topic *[Correct PHP Code for Displaying Cart Items](/forum/32-content-creation/876049-correct-php-code-for-displaying-cart-items#196962 "-- HikaShop version -- : 2.3.5 -- Joomla version -- : 3.3.6 -- PHP version -- : 5.4  I would like to display the number of cart items in a div that shows a shopping basket graphic (see top right of my website)  In an earlier post I was told I could get the number of cart items using the code below. But this variable is showing double the actual number of items in the cart. In the side menu I have a minicart which shows the correct number so presumably I just need to use the variable for the minicart.  Please could you let me know what the variable is I need.  Thanks")*

You chose one of the products that doesn't have any options. If you select the first product from 'Kits' i.e. 'Maxuus Wetroom Kit' you will see it adds 3 to the number in my shopping basket graphic but the number in the mini cart increases by 1.

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

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

Less More

- Posts: 35
- Thank you received: 0
-
-

11 years 7 months ago [\#196964](#196964) by [PigeonTech](/forum/user/35279-pigeontech "View PigeonTech's Profile")  Replied by *[PigeonTech](/forum/user/35279-pigeontech "View PigeonTech's Profile")* on topic *[Correct PHP Code for Displaying Cart Items](/forum/32-content-creation/876049-correct-php-code-for-displaying-cart-items#196964 "-- HikaShop version -- : 2.3.5 -- Joomla version -- : 3.3.6 -- PHP version -- : 5.4  I would like to display the number of cart items in a div that shows a shopping basket graphic (see top right of my website)  In an earlier post I was told I could get the number of cart items using the code below. But this variable is showing double the actual number of items in the cart. In the side menu I have a minicart which shows the correct number so presumably I just need to use the variable for the minicart.  Please could you let me know what the variable is I need.  Thanks")*

The php code was found in an earlier thread here:

[ www.hikashop.com/forum/4-how-to/12498-mi...l?limitstart=0#13328](http://www.hikashop.com/forum/4-how-to/12498-mini-cart-module-modification.html?limitstart=0#13328)

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

11 years 7 months ago [\#196979](#196979) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Correct PHP Code for Displaying Cart Items](/forum/32-content-creation/876049-correct-php-code-for-displaying-cart-items#196979 "-- HikaShop version -- : 2.3.5 -- Joomla version -- : 3.3.6 -- PHP version -- : 5.4  I would like to display the number of cart items in a div that shows a shopping basket graphic (see top right of my website)  In an earlier post I was told I could get the number of cart items using the code below. But this variable is showing double the actual number of items in the cart. In the side menu I have a minicart which shows the correct number so presumably I just need to use the variable for the minicart.  Please could you let me know what the variable is I need.  Thanks")*

Hi,

That's normal. Options are also products so with such code it will also count the options as products. And the mini cart will do too if you turn off the "group options with products" option of the hikashop configuration.
So the solution in your case, so that it takes into account the options properly, is to add the code:
if($row-&gt;cart_product_option_parent_id) continue;
before the line:
$qty += $row-&gt;cart_product_quantity;
of your code.

The following user(s) said Thank You: [PigeonTech](/forum/user/35279-pigeontech "View PigeonTech's Profile")

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

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