Closed danishjamil closed 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.
0.3 is coming on this Sunday (March 18), this fix will be released with that.
@danishjamil Does your $product
have width
and height
attributes?
@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.
I'm asking because I see at least two ways to do this:
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.
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.
@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.
@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
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.