朋友们,我有这个方法来检查购物车中是否已经存在该产品。同样,他补充说,如果产品存在,则更改数量,但他也让产品再次发送。..$order = $this->Orders->newEntity(); if ( $this->request->is( 'post' ) ) { $order = $this->Orders->patchEntity( $order, $this->request->getData() ); $order->order = $this->Orders->Products->get( $order->product_id, ['contain' => ['Users']] ); $session = $this->request->getSession(); $cart = $session->read( 'cart' ); $counter = 0; foreach((array) $cart as $cartOne){ if($cartOne['product_id'] == $order->product_id){ $cartOne['quantity'] += 1; $counter++; break; } } $cart[] = $order; $session->write('cart', $cart);如果有人能分析一下我将不胜感激!我真的无法制作这个过滤器。
1 回答
繁星coding
TA贡献1797条经验 获得超4个赞
您break
跳出循环foreach
,但随后将新订单附加到购物车 ( $cart[] = $order
) 而不检查$counter
变量。您可能想首先检查一下:
if (!$counter) { $cart[] = $order; }
- 1 回答
- 0 关注
- 80 浏览
添加回答
举报
0/150
提交
取消