Codeigniter cart can't insert data

纵然是瞬间 提交于 2019-12-11 14:56:12

问题


I'm trying to add some data to a codeigniter (HMVC codeigniter) chopping cart and display it, i'm using this method in the main cart controller:

function add_to_cart(){
    $this->load->library('cart');

    // Get data
    $userID = $this->input->post('userID');
    $eventID = $this->input->post('eventID');
    $tickedID = $this->input->post('tickedID');

    // Get ticket data
    $this->load->module('ticket');
    $ticket_query = $this->ticket->get_where($tickedID);

    //echo $this->session->all_userdata();

    foreach($ticket_query->result() as $ticket_data){
        $ticketPrice = $ticket_data->price;
        $ticketCategory = $ticket_data->category;
    }

    //echo 'tickedID: '.$tickedID.' price: '.$ticketPrice.' category: '.$ticketCategory;

    // Add item to cart
    $data_items = array(
           'id'      => $tickedID,
           'qty'     => 1,
           'price'   => $ticketPrice,
           'category'    => $ticketCategory,
           'options' => array()
        );

    $this->cart->insert($data_items);

    $cart = $this->cart->contents();
    echo '<pre>';
    echo print_r($cart);
    echo '</pre>';




}

Basically i'm getting the userID, eventID and tickedID variables from the session, then I run a query to get the ticked with the specific id. I run through the results of the query and get the $thicketPrice and $ticketCategory variables from it. Then I attempt to set the variables in $data_items to insert in the cart itself. FInally I attempt to echo the contents of the care and all I get is an empty array.

The session, database and cart libraries are all autoloaded and the sessions are using the database, they have the ci_sessions table. THe sessions also have an ecrypted key, what is wrong?


回答1:


Some attention for successful cart insert:

  • 'price' > 0
  • 'name' (or similar field) better not in unicode



回答2:


You need a name index as it's mandatory.

  • id - Each product in your store must have a unique identifier. Typically this will be an "sku" or other such identifier.
  • qty - The quantity being purchased.
  • price - The price of the item.
  • name - The name of the item.
  • options - Any additional attributes that are needed to identify the product. These must be passed via an array.

Important: The first four array indexes above (id, qty, price, and name) are required. If you omit any of them the data will not be saved to the cart. The fifth index (options) is optional. It is intended to be used in cases where your product has options associated with it. Use an array for options, as shown above.

From http://ellislab.com/codeigniter/user-guide/libraries/cart.html

So, something like this then:

$data_items = array(
       'id'      => $tickedID,
       'qty'     => 1,
       'price'   => $ticketPrice,
       'name'    => $someName,
       'options' => array('category'=>$ticketCategory)
    );


来源:https://stackoverflow.com/questions/18614190/codeigniter-cart-cant-insert-data

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