检查产品是否存在于购物车中 - Cakephp 3
Check if the product exists in the shopping cart - Cakephp 3
朋友们,谁能分析一下,不胜感激
我根据下面的代码将商品添加到购物车。
public function add()
{
$order = $this->Orders->newEntity();
if ($this->request->is('post')) {
$order = $this->Orders->patchEntity($order, $this->request->getData());
$order->product = $this->Orders->Products->get($order->product_id, ['contain' => ['Users']]);
$session = $this->request->getSession();
$cart = $session->read('cart');
$cart[] = $order;
if (isset($cart[$order->product_id])) {
//product is already in the cart
} else {
$cart[$order->product_id] = $order;
}
$product = $this->Orders->Products->find('list', ['limit' => 200]);
$users = $this->Products->Users->find('list', ['limit' => 200]);
$this->set(compact('order', 'products', 'users'));
}
}
我可以将产品添加到普通购物车,但无法阻止多次添加同一产品。只需要一次。
感谢任何评论!
而不是这个
if (isset($cart[$order->product_id])) {
//product is already in the cart
} else {
$cart[$order->product_id] = $order;
}
应该是这样的(因为我没有你的完整代码)
// counter is to check if the product_id already exists
$counter = 0;
foreach($cart as $cartOne){
if($cartOne['product_id'] == $order->product_id){
$cartOne['quantity'] += 1;
$counter++;
break;
}
}
if($counter == 0){
$cart[$order->product_id] = $order;
}
$counter = 0;
完整代码在这里:
public function add(){
$order = $this->Orders->newEntity();
if ($this->request->is('post')) {
$order = $this->Orders->patchEntity($order, $this->request->getData());
$order->product = $this->Orders->Products->get($order->product_id, ['contain' => ['Users']]);
$session = $this->request->getSession();
$cart = $session->read('cart');
$cart[] = $order;
// counter is to check if the product_id already exists
$counter = 0;
foreach($cart as $cartOne){
if($cartOne['product_id'] == $order->product_id){
$cartOne['quantity'] += 1;
$counter++;
break;
}
}
if($counter == 0){
$cart[$order->product_id] = $order;
}
$counter = 0;
$product = $this->Orders->Products->find('list', ['limit' => 200]);
$users = $this->Products->Users->find('list', ['limit' => 200]);
$this->set(compact('order', 'products', 'users'));
}
}
朋友们,谁能分析一下,不胜感激
我根据下面的代码将商品添加到购物车。
public function add()
{
$order = $this->Orders->newEntity();
if ($this->request->is('post')) {
$order = $this->Orders->patchEntity($order, $this->request->getData());
$order->product = $this->Orders->Products->get($order->product_id, ['contain' => ['Users']]);
$session = $this->request->getSession();
$cart = $session->read('cart');
$cart[] = $order;
if (isset($cart[$order->product_id])) {
//product is already in the cart
} else {
$cart[$order->product_id] = $order;
}
$product = $this->Orders->Products->find('list', ['limit' => 200]);
$users = $this->Products->Users->find('list', ['limit' => 200]);
$this->set(compact('order', 'products', 'users'));
}
}
我可以将产品添加到普通购物车,但无法阻止多次添加同一产品。只需要一次。
感谢任何评论!
而不是这个
if (isset($cart[$order->product_id])) {
//product is already in the cart
} else {
$cart[$order->product_id] = $order;
}
应该是这样的(因为我没有你的完整代码)
// counter is to check if the product_id already exists
$counter = 0;
foreach($cart as $cartOne){
if($cartOne['product_id'] == $order->product_id){
$cartOne['quantity'] += 1;
$counter++;
break;
}
}
if($counter == 0){
$cart[$order->product_id] = $order;
}
$counter = 0;
完整代码在这里:
public function add(){
$order = $this->Orders->newEntity();
if ($this->request->is('post')) {
$order = $this->Orders->patchEntity($order, $this->request->getData());
$order->product = $this->Orders->Products->get($order->product_id, ['contain' => ['Users']]);
$session = $this->request->getSession();
$cart = $session->read('cart');
$cart[] = $order;
// counter is to check if the product_id already exists
$counter = 0;
foreach($cart as $cartOne){
if($cartOne['product_id'] == $order->product_id){
$cartOne['quantity'] += 1;
$counter++;
break;
}
}
if($counter == 0){
$cart[$order->product_id] = $order;
}
$counter = 0;
$product = $this->Orders->Products->find('list', ['limit' => 200]);
$users = $this->Products->Users->find('list', ['limit' => 200]);
$this->set(compact('order', 'products', 'users'));
}
}