CakePHP 3:从cookie中删除和更新数组

时间:2016-07-13 09:53:19

标签: cakephp cookies cakephp-3.2

我使用CakePHP 3.2编写购物车应用程序。

我使用Cookie将商品添加到购物车。

现在我想更新并删除购物车中的值。因此,如果用户点击具有不同add to cart值的同一产品quantity,则会删除现有记录,并将新商品添加到购物车。

这是我的addToCart()方法。

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

$product->quantity = $p_quantity;

if (!$product) {
  throw new NotFoundException(__('Invalid Product'));
}

  $cart = $this->Cookie->read('Cart') ? $this->Cookie->read('Cart') : [];

  $itemInCart = false;
  $itemUpdated = false;
  if ($cart != null) {
    foreach($cart as $cart_item):
      if ($cart_item['id'] == $p_id) {
        if ($cart_item['quantity'] != $p_quantity) {
          $this->Cookie->delete('Cart.'.$cart_item);    // line 148
          $cart[] = $product;
          $this->Cookie->write('Cart', $cart);
          $itemsCount = count($this->Cookie->read('Cart'));
          $this->Flash->success('Product updated in cart');
          return $this->redirect($this->referer);
        }
        $itemInCart = true;
      }
    endforeach;
  }

  if (!$itemInCart) {
    $cart[] = $product;
    $this->Cookie->write('Cart', $cart);

    $itemsCount = count($this->Cookie->read('Cart'));

    if ($itemUpdated) {
      $this->Flash->success(__('Product updated in cart'));
    } else {
      $this->Flash->success(__('Product added to cart'));
    }

    return $this->redirect($this->referer());
  } else {
    $this->Flash->success(__('Product is already in cart'));
    return $this->redirect($this->referer());
  }

  }
}

但这是错误的

Notice (8): Array to string conversion [APP/Controller/OrdersController.php, line 148]

如何更新购物车中的数量值。

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

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'));
        }

        $product->quantity = $p_quantity;

        $cart = $this->Cookie->read('Cart') ? $this->Cookie->read('Cart') : [];

        $itemInCart = false;
        $new_cart = [];
        if ($cart != null) {
            foreach($cart as $cart_item):               
                if ($cart_item['id'] == $p_id) {                    
                    if($p_quantity == 0){
                        //Removed the item from cart and set itemInCart to true
                        $itemInCart = true;
                    }else{
                        //update the quantity of item
                        $new_cart[] = $product;
                        $itemInCart = true;
                    }                   
                }else{
                    $new_cart[] = $cart_item;
                }
            endforeach;
        }

        if ($itemInCart) {      
            $this->Cookie->write('Cart', $new_cart);
            $this->Flash->success(__('Product updated in cart'));
        } else {
            $cart[] = $product;
            $this->Cookie->write('Cart', $cart);
            $this->Flash->success(__('Product added to cart'));
        }
        return $this->redirect($this->referer);
    }
}