添加购物车中特定 lbc_type 的所有数量

Adding all quantity of a specific lbc_type in cart

我需要添加购物车中带有 lbc_type = 小袋的所有产品的全部数量。

示例:

所以我应该得到购物车中有 lbc_type = 小袋的所有产品然后总结所有数量,根据示例 lbc_type = 小袋的总数量等于 5。因为苹果有数量 2 而葡萄有数量 3

注意:产品 table 中的每个产品都有列名称 lbc_type,其中必须包含 Pouch 或 Box

我的代码:

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; 

让我试试:)。首先,当您将商品添加到购物车时,您可以添加您定义的任何值。所以利用这种可能性来存储lbc_type:

$data = array(
        'id'      => 'sku_123ABC',
        'qty'     => 1,
        'price'   => 39.95,
        'name'    => 'T-Shirt',
        'lbc_type' => 'Pouch'
);

$this->cart->insert($data);

然后您只需简单地迭代购物车内容:

$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;