2016-07-13 1 views
0

장바구니 응용 프로그램 작성을 위해 CakePHP 3.2를 사용하고 있습니다.CakePHP 3 : 쿠키에서 배열 삭제 및 업데이트

나는 장바구니에 항목을 추가하기 위해 쿠키를 사용하고 있습니다.

이제 카트에서 값을 업데이트하고 삭제하고 싶습니다. 사용자가 동일한 제품 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] 

으로 오류를주고있다.

+0

무엇 라인 (148)에인가? – claudios

+0

예를 들면 : 이미 product_id (1) 및 수량 (3)이 있습니다. 이제 동일한 product_id로 addToCart() 함수를 호출했습니다. 이제 수량 또는 (oldquantity + 현재 수량)을 덮어 쓰려고합니다. 알려주세요 –

+0

@claudios 코드에서 148 행을 표시했습니다. –

답변

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

감사. 잘 작동합니다. 그러나 편집에서 언급 한대로'// 아이템을 카트에서 제거하십시오. ' 장바구니에서 항목을 제거하는 방법. 나는'delete'가 이것을 할 것이지만 특정 배열을 삭제하는 방법을 알고 있습니다. 내 질문에, 나는'$ this-> Cookie-> delete ('Cart.'. $ cart_item)'시도했지만 오류가 –

+1

오신 것을 환영합니다. 상품 ID를 보내고 수량을 0으로 사용해 볼 수 있습니다. 그런 다음 카트에서 제거됩니다. $ new_cart에서 특정 항목을 추가하지 않았기 때문에. –

+0

전체 카트를 삭제하면 "$ this-> Cookie-> delete ('Cart')"를 사용하게됩니다. 왜 당신은 오류를 의미합니다 ", '장바구니.', '장바구니. $ cart_item'은 변수로 사용되며 변수를 사용하지 않으면"장바구니 "만 사용합니다 –