Unable to add to Cart Class

纵饮孤独 提交于 2019-12-11 18:37:04

问题


I've just started playing around with the shopping cart class for CodeIgniter (version 2.1.4) and I've been following tutorials that help explain it. But for some reason I am unable to successfully add a a simple array to the cart.

Here is my implementation of the cart class:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cart extends CI_Controller {

public function __construct() {
    parent:: __construct();
}

public function index() {
    $nav["navigation"] = $this->getCategory->getCategories();

    $this->load->view("header");
    $this->load->view("scripts"); 
    $this->load->view("nav", $nav);
    $this->load->view("cart");
    $this->load->view("footer");
}

function add() {
    $data = array(
        "id"        => "42",
        "name"      => "pants",
        "quantity"  => 1,
        "price"     => 19.99,
        "options"   => array("size" => "medium")
    );

    $this->cart->insert($data);
    var_dump($this->cart->contents()); //This should output the array!

}

function show() {

    $cart = $this->cart->contents();
    echo "<pre>" . print_r($cart) . "</pre>"; //Nothing here either!

}

function update() {

    $this->cart->update($data);
    redirect(base_url()."cart");

}

function total() {

    echo $this->cart->total();

}

function remove() {

    $this->cart->update($data);  

}

function destroy() {

    $this->cart->destroy();

}

}

But if I go to the add function the var_dump just displays "array(0) { }". Same result if I navigate to the show function.

Here is my autoload config showing that I have autoloaded the cart library has been loaded:

$autoload['libraries'] = array("database", "session", "cart");

$autoload['helper'] = array("html", "url", "form");

I know it's something really simple and obvious I'm missing, but right now I'm just left baffled. Any suggestions?


回答1:


Your array key for quantity is named improperly. In the data your are inserting the quantity must be named as qty for the data to be saved to the cart.

"qty"  => 1,


来源:https://stackoverflow.com/questions/19056107/unable-to-add-to-cart-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!