在购物车

时间:2016-07-10 13:57:34

标签: codeigniter shopping-cart

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

示例:

  • 添加到购物车apple(lbc_type = pouch)用户输入数量2
  • 添加到购物车橙色(lbc_type = box)用户输入数量2
  • 添加到购物车葡萄(lbc_type = pouch)用户输入的数量3

所以我应该把购物车中的所有产品都放到lbc_type = pouch然后总结所有基于例子的数量lbc_type = pouch的总量等于5.因为苹果有数量2而葡萄有数量3

注意:产品表中的每个产品都有列名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; 

1 个答案:

答案 0 :(得分:0)

让我试试:)。首先,当您将项目添加到购物车时,您可以添加您定义的任何值。所以使用这种可能性来存储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;