vanilophp / cart

Cart Module for Vanilo (or any Laravel app)
https://vanilo.io
MIT License
50 stars 17 forks source link

Additional parameters passed in addItem are not being saved #4

Closed danishjamil closed 6 years ago

danishjamil commented 6 years ago

Environment: Laravel 5.5.39 Standalone: vanila/cart: 0.2

Code:

$product = \App\Models\Banner::find(1); \Cart::addItem($product, 1, ['width' => 123, 'height' => 12]);

There is no column in the cart_item table to store the additional parameters.

fulopattila122 commented 6 years ago

Actually you need to extend the cart_item table with your custom fields. But still, you're right the addItem doesn't process additional fields. I'll fix that and extend the documentation with an example how to do that in the next 2 days.

fulopattila122 commented 6 years ago

0.3 is coming on this Sunday (March 18), this fix will be released with that.

fulopattila122 commented 6 years ago

@danishjamil Does your $product have width and height attributes?

danishjamil commented 6 years ago

@fulopattila122 width and height attributes were just an example and am implementing the Buyable Interface on a custom model.

Is it possible for you bump the "Product Options & Variants" feature to 0.4.

fulopattila122 commented 6 years ago

I'm asking because I see at least two ways to do this:

1. Passing Attributes Manually

just pass the extra attributes all the time manually:

Cart::addItem($product, 1, [ 'attrs' => ['width' => 123, 'height' => 12]]);

In this case any key/value under attrs would be added to CartItem as an attribute.

2. Auto Merging Extra Attributes

Using this approach you could configure the extra attributes of Buyables that the cart keeps adding to cart items. E.g.: config/vanilo.php:

//...
    'cart' => [
        'items' => [
            'auto_copy_attributes' => ['width', 'height']
        ]
    ]
//...

This way you could just pass you product:

Cart::addItem($product);

and the attributes specified in config would be automatically copied from the product to the cart item.

I think both makes sense and can be used arbitrarily, even together.

danishjamil commented 6 years ago

@fulopattila122 I would rather keep it at the model and/or at the Cart::addItem level instead of setting the copying attributes at the global level.

Because its not necessary that every Buyable Model (product) will have the attributes defined in the config file e.g. Digital Goods.

fulopattila122 commented 6 years ago

@danishjamil I've implemented both variants, so you can freely choose to use either/both/none solutions. Usage has been described in readme: https://github.com/vanilophp/cart/#setting-custom-item-attributes