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

Additional parameters passed in addItem are not being saved #4

Closed
danishjamil opened this issue Mar 15, 2018 · 7 comments
Closed

Additional parameters passed in addItem are not being saved #4

danishjamil opened this issue Mar 15, 2018 · 7 comments

Comments

@danishjamil
Copy link

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
Copy link
Member

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
Copy link
Member

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

@fulopattila122
Copy link
Member

fulopattila122 commented Mar 15, 2018

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

@danishjamil
Copy link
Author

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

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
Copy link
Author

danishjamil commented Mar 15, 2018

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

@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

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

2 participants