laravelshoppingcart
laravelshoppingcart copied to clipboard
How can we sum quantity in attributes?
I want to add quantities for attributes just like products. I have added an index of price in attributes and it is saving, but how can I sum the quantity as it does for product? When I add same product its sum the old quantity for product but not for their attributes.
Here is my code for adding item into cart.
public function addToCart(Request $request)
{
$product_id = $request->input('product_id');
$product = \App\Product::find($product_id);
$session_key = 'guest-2255';
// $new_cart = null;
$unique_id = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 9);
if (\Auth::user()) { // Check is user logged in
$session_key = \Auth::id();
} else {
$session_key = 'guest-2255';
}
Cart::session($session_key);
$attributes = array();
$cart_items = $request->except(['_token', 'product_id']);
$total_qty = 0;
foreach($cart_items as $product_model => $qty)
{
if($qty > 0)
{
$total_qty = $total_qty + $qty;
$productOption = \App\ProductOption::find($product_model);
$row = array();
$row['option_id'] = $productOption->id;
$row['name'] = $productOption->option_name;
$row['quantity'] = $qty;
$row['price'] = $product->mrp;
$attributes[] = $row;
}
}
Cart::add(array(
'id' => $product->id,
'product_id' => $product->id,
'quantity' => intval($total_qty),
'price' => floatval($product->mrp),
'name' => $product->name,
'attributes' => $attributes
));
return response()->json(['msg' => 'Product added to cart successfully!']);
}
I support the question! Please tell me such a situation, there is a product
{
"id": 508,
"name": "name product 1",
"price": 386,
"quantity": 1,
"attributes": {
"size": "44",
"color": "blue"
},
"conditions": []
}
and I add the same product but a different size, for example
{
"id": 508,
"name": "name product 1",
"price": 386,
"quantity": 1,
"attributes": {
"size": "50",
"color": "blue"
},
"conditions": []
}
I don't need him to update the current product in the basket and overwrite its size and increase the number, but what he needs to create in basket is another product of the same size but with a different size, as this is a completely different product.
How to do it?
@vplaton, instead of using the product id as identifier of the cart item, use a "combination".
Example:
- Product is with id 508, size 44, color blue, the
idof the cart item can be508_44_blue. - Product is with id 508, size 50, color blue, the
idof the cart item can be508_50_blue.
This way at the end you will have 2 cart items, depending on their configurations.
@haider885, just write the logic yourself to read the attributes value of the already-existing cart item and update the quantity there as appropriate.
I cannot quite understand the logic that you have added in your example. Can you please give some more details, or an explanation of your logic or expected end result?
As @o15a3d4l11s2 said, you need to adjust the id of the product, what I usually do is to create a row_id like this:
function row_id($product) {
$attributes = $product->attributes;
ksort($attributes); // This is important because we will hash the serialized content
return md5($product->id . '|' . serialize($attributes));
}
Then you can just \Cart::add(row_id($product), ... );
This was created as an example only to clarify what I am trying to say, each one must adjust it to their needs but it is quite straightforward.