0

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

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.

This is my addToCart() method.

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

  }
}

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.

Anuj TBE
  • 9,198
  • 27
  • 136
  • 285

1 Answers1

1

Try the following:

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);
    }
}
Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
Maths RkBala
  • 2,207
  • 3
  • 18
  • 21
  • Thanks. It is working fine. But as mentioned in your edit `//Remove the item from cart`. How to remove item from cart. I know `delete` will do this but how to delete particular array. In my question, I have tried to `$this->Cookie->delete('Cart.'.$cart_item)` but this is giving error – Anuj TBE Jul 13 '16 at 10:30
  • 1
    Welcome. You can try with send the product id and quantity is zero. Then it will be removed from the cart. Because in $new_cart, I didn't add the particular item. – Maths RkBala Jul 13 '16 at 10:36
  • If you delete the whole cart you will use "$this->Cookie->delete('Cart')". Why you have error means, "'Cart.'.$cart_item" it takes as variable and more over we dont use the variable, we will use only "Cart" – Maths RkBala Jul 13 '16 at 10:38
  • Deleting whole cart will remove all items from cart. I want to delete only a particular item. It sounds good to set quantity to 0 and pass to `addToCart()`. I will try it and let you know whether it worked or not. Thank You. – Anuj TBE Jul 13 '16 at 10:47
  • @anujsharma9196 Sure, let me know. Welcome :-) – Maths RkBala Jul 13 '16 at 10:48
  • This is not deleting item from cart instead refreshes same page with all items as it is – Anuj TBE Jul 14 '16 at 06:01
  • You are sending the product_id and quantity as 0 – Maths RkBala Jul 14 '16 at 06:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117285/discussion-between-anujsharma9196-and-maths-rkbala). – Anuj TBE Jul 14 '16 at 07:43
  • but this is not deleting the record. Instead add a new record with all quantity and price as 0. And also previous record still exists there in cookie. – Anuj TBE Jul 20 '16 at 05:46
  • solved it. Nedd to add `$itemInCart = true` in `if($p_quantity == 0)` – Anuj TBE Jul 20 '16 at 08:15
  • Yes. I missed.. Thanks – Maths RkBala Jul 20 '16 at 08:25