I can get the total number of items in the cart by using:
$shipping_num_boxes = $_SESSION['cart']->count_contents();
How can i get the number of items of a specific product (master category #2)?
Thanks in advance for your help
I can get the total number of items in the cart by using:
$shipping_num_boxes = $_SESSION['cart']->count_contents();
How can i get the number of items of a specific product (master category #2)?
Thanks in advance for your help
You could use:
Note: depending on where in the code you are, you may need before that a line for:Code:$chk_the_quantity = $_SESSION['cart']->get_quantity(your_products_id);
Code:global $cart;
Linda McGrath
If you have to think ... you haven't been zenned ...
Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!
Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today: v1.5.5]
Officially PayPal-Certified! Just click here
Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!
Thanks a million Ajeh.
That works perfectly for a specific product.
Is there a way i can get the quantity of all the products in a specific category that are in the cart?
I have a category (id #2) it has sub categories and in the subcategories there are products listed. I want to get the total of all the products (items) in the cart that fall under all the sub categories of the main category (id #2).
Thanks a million in advance.
You cannot pull from the parent category all Products in the subcategories without a lot of customization ... I forget where I have code for something like that writte ... but you can try to hunt for it ...
Meanwhile, you could get the products from each of the subcategories using the master_categories_id for the Products ... the master_categories_id is the immediate categories_id that the Product belongs to ...
You would need to setup something like:
Now $chk_cats would hold the counts ...Code:// bof: get all of Category 2 Product quantity from master_categories_id 10, 12, 15 global $cart; $chk_cats = 0; $chk_cats += $_SESSION['cart']->in_cart_check('master_categories_id','10'); $chk_cats += $_SESSION['cart']->in_cart_check('master_categories_id','12'); $chk_cats += $_SESSION['cart']->in_cart_check('master_categories_id','15'); // eof: get all of Category 2 Product quantity from master_categories_id 10, 12, 15
NOTE: to get that you should put the code above the line:
so it only runs once for that shipping module rather than for each shipping method of that shipping module ...Code:for ($i=0; $i<$size; $i++) {
Linda McGrath
If you have to think ... you haven't been zenned ...
Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!
Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today: v1.5.5]
Officially PayPal-Certified! Just click here
Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!
Ajeh, Thank you so so much for your help. It works!
I am sharing below the modification i did with the help of Ajeh and a post by another member. Hopefully, it will help someone else with similar need.
The main challenge:
I have two main categories: category 1, which has sub categories and products under each sub; these products are pre-packaged but differ in weight since there is more than one item type, therefore when someone orders say 3 of these items we ship them as 3 boxes. Category 2, on the other hand, has sub categories and products under each sub, but these products we package (up to 12 items per box), therefore when someone orders, say 10 items that ships as one box, if someone order 24 items that ships as two boxes, and so on.
The original addon UPS module uses the "Max Weight You Will Ship" that you key in under Admin > Configuration >Shipping/Packaging. In our case that yeilds the correct total weight but an inaccurate number of boxes being shipped.
To remedy that the code below offers a decent solution:-
1- Reads how many items in the shopping cart (ALL ITEMS)
2- Reads how many items of Main Category 2 are in the cart (CAT 2 ITEMS)
3- Packages CAT 2 ITEMS (up to 12 each in a box) - (BOXES of PACKAGED ITEMS)
4- Calculates ALL ITEMS - CAT 2 ITEMS + BOXES of PACKAGED ITEMS
5- Finally shows the correct number of boxes we are shipping with averaged weight; thereby giving us a very realistic shipping cost based on UPS rates.
The final Code I used:
function quote($method = '') {
global $_POST, $order, $cart, $shipping_weight, $shipping_num_boxes;
// Correction from Forum Threads and Ajeh to Fix box count and averages weight
// check number of products we package (main cat id#2 - all items in sub category 12)
$chk_cats = 0;
// the line below gets the total number of items (under master cat id 12)
$chk_cats += $_SESSION['cart']->in_cart_check('master_categories_id','12');
// the line below places up to 12 items in a box
$cnt_cats = ceil($chk_cats / 12);
// calculate total weight
$total_weight = $shipping_weight * $shipping_num_boxes;
// get the total number of items in the cart
$shipping_num_boxes = $_SESSION['cart']->count_contents();
// take away cat 2 items and add the number of boxes of packaged items
$shipping_num_boxes = $shipping_num_boxes - $chk_cats + $cnt_cats;
// average the weight
$shipping_weight = $total_weight / $shipping_num_boxes;
// ... continue with the original UPS Module Code
if ( (zen_not_null($method)) && (isset($this->types[$method])) ) {
$prod = $method;
// BOF: UPS USPS
WARNING: BE VERY, VERY CAREFUL ...
You are changing variables that if you are trying to use other shipping modules that use the same variables will be altered do to changes like this in the shipping module ...
Linda McGrath
If you have to think ... you haven't been zenned ...
Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!
Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today: v1.5.5]
Officially PayPal-Certified! Just click here
Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!
Ajeh,
Is there a way to confine the changes to a specific shipping module? In my case all i use is UPS so it works but i don't want others to be mislead or confused.
Once again thanks for your help and others in the forum.
In your case this would not be an issue as you only use UPS and how you change those variables is your own issue ...
But if someone was to need to use multiple shipping modules such as USPS and UPS ... then it is better to get those variables but then if they need to be changed, change new variables such as:
use something line:Code:// calculate total weight $total_weight = $shipping_weight * $shipping_num_boxes; // get the total number of items in the cart $shipping_num_boxes = $_SESSION['cart']->count_contents(); // take away cat 2 items and add the number of boxes of packaged items $shipping_num_boxes = $shipping_num_boxes - $chk_cats + $cnt_cats;
Notice how you can use a variable that is globally shared by other files, just do not change their values in your code and use your own variables ...Code:// calculate total weight $my_total_weight = $shipping_weight * $shipping_num_boxes; // get the total number of items in the cart $my_shipping_num_boxes = $_SESSION['cart']->count_contents(); // take away cat 2 items and add the number of boxes of packaged items $my_shipping_num_boxes = $my_shipping_num_boxes - $chk_cats + $cnt_cats;
Linda McGrath
If you have to think ... you haven't been zenned ...
Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!
Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today: v1.5.5]
Officially PayPal-Certified! Just click here
Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!
Bookmarks