0

I'm working in CakePHP 3.2 and building a shopping cart.

I'm using Cookie component to store the products in the cart.

This is what I'm doing to add products to 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'));
        }

          $this->Cookie->write('Cart',
            ['id' => $p_id, 'quantity' => $p_quantity]);

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

          $this->Flash->success(__('Product added to cart'));
          return $this->redirect($this->referer());

      }
    }

How could I add multidimensional array in Cookie because Cart can have multiple products and each product carry multiple value. Also, How could I print in view of cart() method ?

This is how my cart() method is

public function cart()
    {
      $cart_products = $this->Cookie->read('Cart');

      $this->set('cart_products', $cart_products);
    }

and printing in view as

foreach($cart_products as $c_product):
  echo $c_product->id.' : '.$c_product->quantity;   // line 45
endforeach;

But this gives error as

Trying to get property of non-object [ROOT/plugins/ArgoSystems02/src/Template/Orders/cart.ctp, line 45]
Anuj TBE
  • 9,198
  • 27
  • 136
  • 285

2 Answers2

1

You write array to cookie:

$this->Cookie->write('Cart', ['id' => $p_id, 'quantity' => $p_quantity]);

I believe what You want is to store all products in cookie:

$cart = $this->Cookie->read('Cart') ? $this->Cookie->read('Cart') : [];
$cart[] = $product;
$this->Cookie->write('Cart', $cart)
Bogdan Kuštan
  • 5,427
  • 1
  • 21
  • 30
  • From your first line, everytime I add a new product, data of previously written item is replaced by new one. I want to add all products without replacing one. – Anuj TBE Jul 13 '16 at 07:05
  • It reads already added products or creates new array if cookie does not exists. – Bogdan Kuštan Jul 13 '16 at 07:10
  • How to update a particular array value in this cookie – Anuj TBE Jul 13 '16 at 08:37
  • @anujsharma9196 You get from the cookie and by using for loop check the id if exists then overwrite the quantity and also take the remaining to temp array finally overwrite the new array to cookie – Maths RkBala Jul 13 '16 at 09:23
  • @MathsRkBala can you answer it [Here](http://stackoverflow.com/questions/38348282/cakephp-3-delete-and-update-array-from-cookie) I have created a separate question for this – Anuj TBE Jul 13 '16 at 09:54
1

Try the following

Instead of Method

$this->Cookie->write('Cart',['id' => $p_id, 'quantity' => $p_quantity]);

Into

$cart = [];
if($this->Cookie->check('Cart')){
    $cart = $this->Cookie->read('Cart');
}
$cart[] = ['id' => $p_id, 'quantity' => $p_quantity];//multiple
$this->Cookie->write('Cart', $cart);

Instead of View

foreach($cart_products as $c_product):
  echo $c_product->id.' : '.$c_product->quantity;   // line 45
endforeach;

Into

foreach($cart_products as $c_product):
  echo $c_product['id'].' : '.$c_product['quantity'];   // line 45
endforeach;
Maths RkBala
  • 2,207
  • 3
  • 18
  • 21