CakePHP 3:从cookie中删除和更新数组 [英] CakePHP 3 : delete and update array from cookie

查看:206
本文介绍了CakePHP 3:从cookie中删除和更新数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using CakePHP 3.2 for writing a shopping cart application.

我使用cookie将项目添加到购物车。

I'm using cookie to add items to the cart.

现在我想更新和删除购物车中的值。所以如果用户点击相同的产品添加到具有不同的数量值的购物车,现有记录将被删除,新的将被添加到购物车。

Now I want to update and delete value from cart. so that if user clicks on the same product add to cart with a different quantity value the existing record will be deleted and new will be added 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());
  }

  }
}

(8):数组到字符串的转换[APP / Controller / OrdersController.php,第148行] $($)

But this is giving error as

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

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

How could I update the quantity value in the cart.

推荐答案

尝试以下操作:

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

这篇关于CakePHP 3:从cookie中删除和更新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆