Skip to main content

Correct PHP Code for Displaying Cart Items

More
11 years 6 months ago #196626 by PigeonTech
-- 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:
<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.

More
11 years 6 months ago #196652 by Mohamed Thelji
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.

More
11 years 6 months ago #196780 by PigeonTech
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.

More
11 years 6 months ago #196791 by Mohamed Thelji
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.

More
11 years 6 months ago #196962 by PigeonTech
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.

More
11 years 6 months ago #196964 by PigeonTech

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

More
11 years 6 months ago #196979 by nicolas
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.201 seconds
Powered by Kunena Forum