Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't update attributes #163

Open
aniket-singh opened this issue Jan 8, 2019 · 3 comments
Open

Can't update attributes #163

aniket-singh opened this issue Jan 8, 2019 · 3 comments

Comments

@aniket-singh
Copy link

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
@Tisseur2Toile
Copy link

Tisseur2Toile commented May 22, 2019

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

@joserick
Copy link

Thanks @Tisseur2Toile

But it would be better as I stated @aniket-s for make it more intuitive.

joserick added a commit to joserick/laravelshoppingcart that referenced this issue Oct 12, 2020
Be able to update a value within attributes without deleting others values. darryldecode#163 darryldecode#224
@websupplements
Copy link

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants