laravelshoppingcart
laravelshoppingcart copied to clipboard
Can't update attributes
Hi, i have been trying to update the attributes but it does not work. When i add a product to cart the response is as shown below
{
"35":
{"id":35,
"name":"Boys Red & Navy Regular Fit Checked Casual Shirt",
"price":719.1,
"quantity":5,
"attributes": {"image":"1546780953_K4Xg0shHTMntvjIonYIKbiJ5HUnHQydGBK1M8GQWDIab6jH88v.jpg",
"size":"5Y",
"discount":10,
"slug":"boys-red-navy-regular-fit-checked-casual-shirt-Red",
"color":"Red",
"og_price":"799",
"max_qty":20}
,"conditions":[]
}
}
However when i try to update the attribute size to something else using the code below
Cart::update($request->id, array(
'attributes' => [
'size'=> $request->val,
]
));
After updating the attribute the cart data gets corrupted, as shown below
{"35":
{"id":35,
"name":"Boys Red & Navy Regular Fit Checked Casual Shirt",
"price":719.1,
"quantity":5,
"attributes":
{"size":"6Y"},
"conditions":[]
}
}
All the other attributes get removed automatically
Hello @aniket-s ,
I had a similar issue, what I did is setup all the potential attribues with the Cart::add
function like this :
\Cart::add( 1,
'product',
20,
2,
[
'att1' => 'value',
'att2' => '',
'att3' => '',
'att4' => 0
]
);
Then, to update a specific attribute I did this :
$cart = \Cart::get(1);
$cart->attributes['att4'] = 2 ;
dd(\Cart::get(1));
You'll see that the attribute is then updated. I needed this to store specific informations when the user is actually connected. Don't know if it's the best way but it solved my problem. Hope this helps, cheers
Thanks @Tisseur2Toile
But it would be better as I stated @aniket-s for make it more intuitive.
Above commit does not work. Need to update the code changes of the commit as mentioned below to make it work. An array is not merged directly with a collection.
$collection = new ItemAttributeCollection ($value);
$item[$key] = $item[$key]->merge($collection);