我正在CakePHP 3.2中工作并构建购物车。我使用Cookie
组件来将产品存储在购物车中。CakePHP 3:创建多维饼干
这是我在做什么将产品添加到购物车
public function addToCart()
{
$this->loadModel('Products');
if ($this->request->is('post')) {
$p_id = $this->request->data('product_id');
$p_quantity = $this->request->data('qnty');
$product = $this->Products->get($p_id);
if (!$product) {
throw new NotFoundException(__('Invalid Product'));
}
$this->Cookie->write('Cart',
['id' => $p_id, 'quantity' => $p_quantity]);
$itemsCount = count($this->Cookie->read('Cart'));
$this->Flash->success(__('Product added to cart'));
return $this->redirect($this->referer());
}
}
我怎么能在Cookie添加多维数组,因为车可以有多个产品,每个产品携带多个值。 另外,如何在cart()
的view
中打印?
这是我cart()
方法是如何
public function cart()
{
$cart_products = $this->Cookie->read('Cart');
$this->set('cart_products', $cart_products);
}
,并打印在视图
foreach($cart_products as $c_product):
echo $c_product->id.' : '.$c_product->quantity; // line 45
endforeach;
但是这给了错误的
Trying to get property of non-object [ROOT/plugins/ArgoSystems02/src/Template/Orders/cart.ctp, line 45]
使用echo $ c_product [ '身份证']“。 :'。$ c_product ['quantity'];在线45 –
这是正确的打印一个单一的cookie值。我想在cookie中存储一个数组并循环遍历它以打印所有'id'和'quantity'。此代码需要删除'foreach()'循环,并使用'$ cart_products ['id']打印' –
请检查我的答案,并让我知道谢谢:-) –