장바구니 응용 프로그램 작성을 위해 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]
으로 오류를주고있다.
무엇 라인 (148)에인가? – claudios
예를 들면 : 이미 product_id (1) 및 수량 (3)이 있습니다. 이제 동일한 product_id로 addToCart() 함수를 호출했습니다. 이제 수량 또는 (oldquantity + 현재 수량)을 덮어 쓰려고합니다. 알려주세요 –
@claudios 코드에서 148 행을 표시했습니다. –