Codin2012 Posted October 26, 2015 Share Posted October 26, 2015 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() { } Quote Link to comment Share on other sites More sharing options...
scootstah Posted October 26, 2015 Share Posted October 26, 2015 The code you posted gives us nothing. You have told us what you want to do, but have not told us what the problem is. Presumably you would get a "total quantity" by querying your database. Quote Link to comment Share on other sites More sharing options...
Codin2012 Posted October 26, 2015 Author Share Posted October 26, 2015 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. Quote Link to comment Share on other sites More sharing options...
printf Posted October 26, 2015 Share Posted October 26, 2015 (edited) 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 October 26, 2015 by printf Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 27, 2015 Share Posted October 27, 2015 (edited) 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 October 27, 2015 by mac_gyver Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.