Correct PHP Code for Displaying Cart Items

  • Posts: 35
  • Thank you received: 0
9 years 7 months ago #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

<span style="font-family: courier new, courier, monospace;">

<?php

require_once('/home/dwrftp/diywetroom.pigeontech.co.uk/administrator/components/com_hikashop/helpers/helper.php'); 
$class = hikashop::get('class.cart');
$rows = $class->get();
$qty = 0;
if(!empty($rows)){
foreach($rows as $k =>$row){
$qty += $row->cart_product_quantity;
}
} 
echo $qty; 
?>
</span>

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

  • Posts: 12953
  • Thank you received: 1778
9 years 7 months ago #196652

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

  • Posts: 35
  • Thank you received: 0
9 years 7 months ago #196780

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

  • Posts: 12953
  • Thank you received: 1778
9 years 7 months ago #196791

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

  • Posts: 35
  • Thank you received: 0
9 years 7 months ago #196962

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

  • Posts: 35
  • Thank you received: 0
9 years 7 months ago #196964

The php code was found in an earlier thread here:

www.hikashop.com/forum/4-how-to/12498-mi...l?limitstart=0#13328

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
9 years 7 months ago #196979

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->cart_product_option_parent_id) continue;
before the line:
$qty += $row->cart_product_quantity;
of your code.

The following user(s) said Thank You: PigeonTech

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

Time to create page: 0.079 seconds
Powered by Kunena Forum