Codeigniter添加折扣项目

时间:2013-08-23 10:57:32

标签: php codeigniter shopping-cart

您好我在Codeigniter购物车运行良好。我试图寻找折扣/优惠券处理但不能。

这是我的购物车代码:

public function addToCart($id){

    $this->load->model('products_model');

    $products = $this->products_model->getItemDetail($id);

    foreach ($products as $product) {
        $insert = array(
            'id' => $product->id,
            'name' => $product->name,
            'qty' => '1',
            'price' => $product->item_eur,
            'image' => $product->image,
            'options' => array(
                'info' => $product->cart_description,
                'qty_description' => $product->qty_description
                )
            );
        $this->cart->insert($insert);
    }

    $last = end($this->cart->contents());
    echo $last['rowid'];
}

我想过从数据库中读取优惠券代码并在购物车中插入折扣作为项目:

public function addCoupon(){
    $this->load->model('products_model');
    $res = $this->products_model->getCoupon($_POST['value']);
    if($res){
        foreach($res as $result){
            $coupon = array(
                'id' => $result->id,
                'name' => $result->name,
                'qty' => '1',
                'price' => $result->discount,
                'options' => array(
                    'info' => 'coupon',
                    'qty_description' => ''
                    )
                );
            $this->cart->insert($coupon);
        }
        echo '1';
    }else{
        $this->lang->load('products');
        echo $this->lang->line('cart_notok');
    }
}

数据库

[name:type] (example)
id:int (1)
name: varchar ('Discount of 20€')
code: varchar ('AFD1234')
discount: decimal (-20)

[编辑]

模特:

public function getCoupon($code){

    $results = $this->db->get_where('coupons', array('code' => $code));
    return $results->result();

}

该商品已添加到购物车,但如果'price' => $result->discount设置为-20或百分比设置为+ 20€而不是-20€。

有什么办法吗?

0 个答案:

没有答案