【问题标题】:Sum of an elemnt in option section of codeigniter cartcodeigniter 购物车选项部分中元素的总和
【发布时间】:2014-08-19 09:09:48
【问题描述】:

您好,codeigniter 中的所有知识渊博的人,

我正在使用 codeigniter 购物车系统。

这是我的 codeigniter 购物车的结构

$data = array(
                'id'      => $id,
                'qty'     => $qty,
                'price'   => $price,
                'name'    => $name,
                'options' => array(
                                  'picture'=>$img,
                                  'item_slug'=>$slug,
                                  'item_color'=>$color,
                                  'item_size'=>$size,
                                  'unit_price'=>$price,
                                  'order_type'=>$order_type,
                                  'product_type'=>$pro_type,
                                  'unit_discount' => 0.00,
                                  'total_discount' => 0.00,
                                  'discount_type' => '',
                                )
                                );

现在我想得到购物车上的总折扣,我可以通过这样的 code-sn-p 来完成

$tot_discount = 0.00;
foreach($this->cart->contents() as $ci)
       {
          $tot_discount = floatval($tot_discount + $ci['options']['total_discount'];
       }

现在我的问题是,是否有任何内置的购物车功能可以获取 codeigniter 中选项部分的任何数字字段的总和?比如说 $this->cart->total() 给出购物车的总价格。

或者我需要使用上面的代码sn-p?

提前致谢

【问题讨论】:

    标签: codeigniter cart


    【解决方案1】:

    在使用 codeigniter 中的购物车库时,您需要为购物车设置一些修复参数,其中一些是可选的。

    固定参数

    'id'      => $id,
    'qty'     => $qty,
    'price'   => $price,
    'name'    => $name
    

    可选参数

    'options' => array();
    

    实际上 options 变量包含您指定的附加产品特定信息,它们可能会根据您的业务需求而变化。现在我们有一个通用的函数 total() 如下所述:

    $this->cart->total()
    

    如果您阅读方法定义,您会发现它使用“价格”参数来获取购物车总金额。现在,您已经在“options”数组中声明了“total_discount”,因此有一个自定义业务需求。因此,您可以利用“total()”方法定义来创建自己的函数,例如“totalDiscount”,因为它因产品而异。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      • 2011-11-20
      • 2011-01-25
      • 2013-01-11
      • 1970-01-01
      相关资源
      最近更新 更多