Codeigniter - 为什么添加到购物车不起作用?

Codeigniter - Why adding to cart didn't work?

我在尝试将购物车添加到购物车时遇到问题。购物车无法进入购物车。

这是我的控制器:

public function buy($product_id)
    {
        $product = $this->m_produk->find($product_id);
        $data = array(
                        'id'      => $product->id,
                        'qty'     => 1,
                        'nama'   => $product->nama,
                        'harga'    => $product->harga
                        );

        $this->cart->insert($data);
        redirect('produk/index');   
        } 

这是我要添加购物车的型号:

public function find($id){
    $hasil = $this->db->where('id',$id)
                      ->limit(1)
                      ->get('produk');
    if ($hasil->num_rows() > 0) {
        return $hasil->row();
    }else {
        return array();
    }
}

这是我的观点:

<?php

foreach ($produk as $product) {

?>
<div class="thumbnail">
<img src="<?php echo base_url(''.$product->gambar);?>" width="200" height="200" class="img-rounded center-block" alt=""/>
<div class="caption">
<h4 class="text-center"><?php echo $product->nama?></h4>
<h4 class="text-center">RP.<?php echo $product->harga?>,00</h4>
<!-- <a href="#"  class="link-class btn btn-primary center-block" role="button">add to cart</a> -->
<?=anchor('produk/buy/'.$product->id,'add to cart' , [
    'class' => 'btn btn-primary' , 'role' => 'button'
]) ?>
</div>
</div>
<?php } ?>
</div>

PS:当我print_r我的内容时,购物车只是显示:array()这意味着当我添加购物车时,购物车无法进入购物车。

谁能解决这个问题?

谢谢。

根据您参考的文档: https://codeigniter.com/userguide3/libraries/cart.html

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.

请传递所有必需的参数。 您有 name 拼写错误或遗漏,price 遗漏