0

我正在使用购物车类CI,里面我有小计。有没有办法计算所有小计并将它们显示在视图中?

喜欢$total=$allsubtotal;

谢谢

4

1 回答 1

3

根据Codeigniter Cart 类文档

$this->cart->total();

显示购物车中的总金额。

如果您好奇,这是内部计算的方式:

/**
 * Cart Total
 *
 * @access  public
 * @return  integer
 */
function total()
{
    return $this->_cart_contents['cart_total'];
}

这是设置的位置:

/* snippet from function _save_cart */

// Lets add up the individual prices and set the cart sub-total
$total = 0;
$items = 0;
foreach ($this->_cart_contents as $key => $val)
{
    // We make sure the array contains the proper indexes
    if ( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty']))
    {
        continue;
    }

    $total += ($val['price'] * $val['qty']);
    $items += $val['qty'];

    // Set the subtotal
    $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']);
}

// Set the cart total and total items.
$this->_cart_contents['total_items'] = $items;
$this->_cart_contents['cart_total'] = $total;

我不确定为什么totals 返回值被记录为整数,应该是浮点数/双精度数。

于 2013-08-11T21:24:03.450 回答