问题
i need to add all the quantity of all the product in the cart that have a lbc_type = pouch.
example:
- add to cart apple(lbc_type = pouch) user inputted quantity 2
- add to cart orange(lbc_type = box) user inputted quantity 2
- add to cart grapes(lbc_type = pouch) user inputted quantity 3
so i should get all the product in cart that have lbc_type = pouch then sum up all the quantity which based on the example the overall quantity of lbc_type = pouch is equal to 5. Because apple have quantity 2 and grapes have quantity 3
NOTE: Each product in the product table have column name lbc_type which must contain either Pouch or Box
MY CODES:
foreach($this->cart->contents() as $item)
{
$name = $item['name']; //product name
echo "<br>";
$id = $item['id'];
print_r($id); //print product_id
echo " ";
print_r($name); //print product name
$data['product'] = $this->PaymentModel->getLBCType($name);
foreach ($data['product'] as $lbctype)
{
$getLBC = $lbctype->lbc_type;
if($getLBC == 'Pouch') //check product if lbc_type pouch
{
$qty = $item['qty']; //inputted quantity
echo " The Quantity of this Product is" .$qty;
}
}
}
echo "<br/>";
//$increment++;
die;
回答1:
Let me try :). First when you add item to cart, you can add any value you define. So use this possibility to store lbc_type:
$data = array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'lbc_type' => 'Pouch'
);
$this->cart->insert($data);
Then you just simple iterate cart contents:
$pouch_qty = 0;
foreach($this->cart->contents() as $item)
{
$name = $item['name']; //product name
echo "<br>";
$id = $item['id'];
print_r($id); //print product_id
echo " ";
print_r($name); //print product name
if ($item['lbc_type']=='Pouch') {
$pouch_qty += $item['qty'];
}
}
echo "<br/>";
echo " The Quantity of this Product is" .$pouch_qty;
die;
来源:https://stackoverflow.com/questions/38292657/adding-all-quantity-of-a-specific-lbc-type-in-cart