在codeigniter购物车会话中更新价格金额

时间:2014-07-11 06:28:24

标签: codeigniter cart

我使用下面的代码来更新CodeIgniter的购物车会话中的价格金额。

$vars = array('rowid'   => $_POST['rowid'],'price' => $amount);
var_dump($this->cart->update($vars));

但结果未反映出来。请指导我错在哪里。

3 个答案:

答案 0 :(得分:2)

我知道问题被问到已经有一段时间了,但我遇到了同样的问题,并认为这可能会对将来的其他人有所帮助。

默认情况下,CodeIgniter购物车不允许您更新价格,也不允许更新任何不是数量的元素。有两种方法可以解决这个问题。

第一个是复制内容并更新副本,然后删除原始内容并再次插入。这可能有点乱:

$data = $delete = $this->cart->contents()[0];
$data['price'] = $new_price;
$delete['qty'] = 0;
$this->cart->update($delete);
$this->cart->insert($data);

第二种方法是使用以下方法更新购物车类:http://blog.thecodingbox.me/codeigniter-cart-class-extended-to-update-price/并按原样更新购物车。我不得不说我从未尝试过这个。

答案 1 :(得分:0)

来自https://bitbucket.org/nelson.wells/extended-codeigniter-cart/src/b9d1601be11a7044d54243ffe9470ce4f9f42728/application/libraries/MY_Cart.php?at=default&fileviewer=file-view-default

的Nelson Wells的解决方案

只需使用此购物车库扩展核心购物车库,并使用方法cart->update_all($data)代替cart->update($data)(更新仍然有效),使用CI 2.2对其进行测试。

它将更新购物车会话中商品中存储的任何商品(即价格,数量,名称,价格,自定义字段)。

位置:

./application/libraries/MY_Cart.php

文件内容

<?php

/* 
 * Class: Extended Cart 
 * Purpose:  Add methods to update any item stored in an item in the 
 *           cart session (i.e. price, qty, name, price, custom fields).
 * Limitations: No support for updating array data in items (i.e. the 
 *              options array, any custom data stored as an array)
 * Author: Nelson Wells
 * License: The CodeIgniter license.  Read it.
 */

/**** Use: ****
 * 
 * $data = array(
 *     'rowid' => 'dhedu3k3mhdu2n',
 *     'qty' => 10,
 *     'name' => 'hello'
 * );
 * 
 * $this->cart->update_all($data);
 * 
 * OR
 * 
 * $data = array(
 *      array(
 *          'rowid' => 'dlk2jkduvk2d',
 *          'name' => 'world'
 *      ),
 *      array(
 *          'rowid' => 'dklg3h211kd',
 *          'price' => 25.50
 *      )
 * );
 * 
 * $this->cart->update_all($data);
 * 
 * 
 */

class MY_Cart extends CI_Cart
{
    function __construct()
    {
        parent::__construct();
    }

    function update_all($items = array())
    {
        // Was any cart data passed?
        if ( ! is_array($items) OR count($items) == 0)
        {
            return false;
        }

        // You can either update a single product using a one-dimensional array, 
        // or multiple products using a multi-dimensional one.  The way we
        // determine the array type is by looking for a required array key named "rowid".
        // If it's not found we assume it's a multi-dimensional array   
        if (isset($items['rowid']))
        {   
            $this->_update_item($items);
        }
        else
        {
            foreach($items as $item)
            {
                $this->_update_item($item);
            }
        }

        $this->_save_cart();
    }

    /*
     * Function: _update_item
     * Param: Array with a rowid and information about the item to be updated
     *          such as qty, name, price, custom fields. 
     */
    function _update_item($item)
    {
        foreach($item as $key => $value)
        {
            //don't allow them to change the rowid
            if($key == 'rowid')
            {
                continue;
            }

            //do some processing if qty is 
            //updated since it has strict requirements
            if($key == "qty")
            {                   
                // Prep the quantity
                $item['qty'] = preg_replace('/([^0-9])/i', '', $item['qty']);

                // Is the quantity a number?
                if ( ! is_numeric($item['qty']))
                {
                    continue;
                }

                // Is the new quantity different than what is already saved in the cart?
                // If it's the same there's nothing to do
                if ($this->_cart_contents[$item['rowid']]['qty'] == $item['qty'])
                {
                    continue;
                }

                // Is the quantity zero?  If so we will remove the item from the cart.
                // If the quantity is greater than zero we are updating
                if ($item['qty'] == 0)
                {
                    unset($this->_cart_contents[$item['rowid']]);
                    continue;
                }
            }

            $this->_cart_contents[$item['rowid']][$key] = $value;
        }
    }
}

答案 2 :(得分:0)

我尝试使用此代码,并使用之前的评论引用它。

$cart_info =  $_POST['cart'] ;
$this->cart->destroy();
foreach( $cart_info as $id => $cart){   

   $data = array(
            'rowid'   => $cart['rowid'],
            'id'      => $cart['id'],
            'name'    => $cart['name'],
            'price'   => $cart['price'],
            'qty'     => $cart['qty'],
            'subtotal' => $cart['subtotal']                 
        );
        $this->cart->insert($data);
    }
    redirect('orden_compra');