【问题标题】:Check if product already exists in the cart - Cakephp 3检查产品是否已存在于购物车中 - Cakephp 3
【发布时间】:2020-08-05 20:07:22
【问题描述】:

朋友们,我有这个方法可以检查产品是否已经存在于购物车中。这样做也是如此,如果产品存在,他添加更改数量,但他也让产品再次发送。

.
.
$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);

如果有人可以分析,我将不胜感激!我真的无法制作这个过滤器。

【问题讨论】:

    标签: php cakephp cakephp-3.0


    【解决方案1】:

    您的 break 退出了 foreach 循环,但随后您将新订单附加到购物车 ($cart[] = $order) 而不检查 $counter 变量。您大概想先检查一下:

    if (!$counter) {
        $cart[] = $order;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 2019-03-17
      • 2017-05-06
      • 1970-01-01
      • 2011-04-09
      • 1970-01-01
      相关资源
      最近更新 更多