- Posts: 35
- Thank you received: 0
Correct PHP Code for Displaying Cart Items
- PigeonTech
-
Topic Author
- Offline
Less
More
11 years 6 months ago #196626
by PigeonTech
Correct PHP Code for Displaying Cart Items was created 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
-- 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.
Less
More
- Posts: 12953
- Thank you received: 1778
11 years 6 months ago #196652
by Mohamed Thelji
Replied by Mohamed Thelji on topic Correct PHP Code for Displaying Cart Items
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"
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.
- PigeonTech
-
Topic Author
- Offline
Less
More
- Posts: 35
- Thank you received: 0
11 years 6 months ago #196780
by PigeonTech
Replied by PigeonTech on topic Correct PHP Code for Displaying Cart Items
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.
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.
Less
More
- Posts: 12953
- Thank you received: 1778
11 years 6 months ago #196791
by Mohamed Thelji
Replied by Mohamed Thelji on topic Correct PHP Code for Displaying Cart Items
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.
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.
- PigeonTech
-
Topic Author
- Offline
Less
More
- Posts: 35
- Thank you received: 0
11 years 6 months ago #196962
by PigeonTech
Replied by PigeonTech on topic Correct PHP Code for Displaying Cart Items
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.
- PigeonTech
-
Topic Author
- Offline
Less
More
- Posts: 35
- Thank you received: 0
11 years 6 months ago #196964
by PigeonTech
Replied by PigeonTech on topic Correct PHP Code for Displaying Cart Items
The php code was found in an earlier thread here:
www.hikashop.com/forum/4-how-to/12498-mi...l?limitstart=0#13328
www.hikashop.com/forum/4-how-to/12498-mi...l?limitstart=0#13328
Please Log in or Create an account to join the conversation.
11 years 6 months ago #196979
by nicolas
Replied by nicolas on topic Correct PHP Code for Displaying Cart Items
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.
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