Jump to content

OOP PHP How to Find Total Quantity


Codin2012

Recommended Posts

I am trying to get the total quantity of products ordered and added to my shopping cart. I am trying to do this in OOP. I have a class called cart and I have a method called get_total_qty. So every time the cart gets updated and the quantity changes I need my total quantity to change also. Here is my class 

 

 

 class Cart {
        
        public function __construct()
        {
            
        }
 
 
 
 
 
 
Link to comment
Share on other sites

What is the best way using OOP PHP to find the total quantity of a shopping cart. The customer can have more than one item per page to add to the cart. So whenever the quantity of the cart changes I want the total to change. So I have a method in my cart class that will calculate the total quantity. Here is the method from my cart class.

 

    

public function get_total_qty($total_qty = '')
        {
            $total_qty = 0;
            
            if (isset($_SESSION['cart']))
            {
               foreach($_SESSION['cart'] as $qty)
               {
                  $total_qty = $total_qty + $qty;
               }
            }
            return $total_qty;
            echo $total_qty; die();
        }

And here is the code on my index page

$total_qty = new Cart();
			
$total_qty->get_total_qty($cart_itm["qty"]);

cart_itm["qty] is the variable used to receive the quantity when an item is added to the cart.

Link to comment
Share on other sites

your cart should have it's on $key['quantity] that way when a item is added or removed it's as simple as $key['quantity] += 1; || -= 1; That will save you from having to loop your cart every time a item is added or removed just to get the total items in your cart. Think of it this way... cart items are unique so they need an array() of item(s), but things like total items, total price, etc... are not unique, so they only need a single $key => $value;

Edited by printf
Link to comment
Share on other sites

the total quantity is a derived value. you should not maintain a value for it, but calculate it when needed.

 

your cart appears to be storing the quantity of each item as the value for each array entry (and if it's not, that is how you should be storing the quantity for each item.) you can just use array_sum($_SESSION['cart']) to get a total count of things in the cart. using count($_SESSION['cart']) would tell you how many different items are in the cart.

 

your class method doesn't use/need an input parameter. why are you defining and calling it with one?

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.