【问题标题】:CakePHP 3 : delete and update array from cookieCakePHP 3:从 cookie 中删除和更新数组
【发布时间】:2016-07-13 09:53:18
【问题描述】:

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

我正在使用 cookie 将商品添加到购物车。

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

这是我的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]

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

【问题讨论】:

  • 第 148 行是什么?
  • 举个例子:我已经有了product_id(1)和quantity(3)。现在您使用相同的 product_id 调用了 addToCart() 函数,现在您想要覆盖数量或(旧数量 + 当前数量)。告诉我
  • @claudios 我已经在代码中标记了第 148 行。
  • @MathsRkBala 我想覆盖现有数量。
  • @anujsharma9196 请使用我的代码并告诉我。谢谢:-)

标签: cakephp cookies cakephp-3.2


【解决方案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);
    }
}

【讨论】:

  • 谢谢。它工作正常。但正如您在编辑//Remove the item from cart 中提到的那样。如何从购物车中删除商品。我知道delete 会这样做,但是如何删除特定的数组。在我的问题中,我尝试$this->Cookie->delete('Cart.'.$cart_item),但这给出了错误
  • 欢迎。您可以尝试发送产品 id 并且数量为零。然后它将从购物车中删除。因为在 $new_cart 中,我没有添加特定的项目。
  • 如果您删除整个购物车,您将使用“$this->Cookie->delete('Cart')”。为什么你有错误的意思是,“'Cart.'.$cart_item”它作为变量,而且我们不使用变量,我们将只使用“Cart”
  • 删除整个购物车将删除购物车中的所有商品。我只想删除一个特定的项目。将数量设置为 0 并传递给 addToCart() 听起来不错。我会尝试一下,让你知道它是否有效。谢谢。
  • @anujsharma9196 当然,让我知道。欢迎:-)
猜你喜欢
  • 2019-06-05
  • 1970-01-01
  • 2011-11-05
  • 2011-08-05
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多