bumbummen99 / LaravelShoppingcart

A simple shopping cart implementation for Laravel
MIT License
501 stars 228 forks source link

updating options replace all existing options #177

Closed talha2k closed 1 year ago

talha2k commented 1 year ago

Cart::update($rowId, ['options' => ['new_option' => 'XXX']]);

using above line replaces all existing options. is there any way to add a new option to existing rowId?

bumbummen99 commented 1 year ago

Out of the blue i would say: Get the CartItem with the rowId and then merge its existing options

$cartItem =$cart->get($rowId)
$cart->update($rowId, array_merge($cartItem->options, [
    ...
]))
talha2k commented 1 year ago

Thank you! I achieved by doing following:

$cart_item = Cart::get($rowId); $new_option = $cart_item->options->merge(['new_variable' => $new_variable_value]); Cart::update($rowId, ['options' => $new_option]);