yii2-cart
yii2-cart copied to clipboard
Argument 1 passed to yii2mod\cart\Cart::add() must be an instance of yii2mod\cart\models\CartItemInterface
Hello,
I have this error:
Argument 1 passed to yii2mod\cart\Cart::add() must be an instance of yii2mod\cart\models\CartItemInterface
is in this method:
public function add(CartItemInterface $element, $save = true): self
I follow the doc and configured the extension in correct way:
'cart' => [
'class' => 'yii2mod\cart\Cart',
// you can change default storage class as following:
'storageClass' => [
'class' => 'yii2mod\cart\storage\DatabaseStorage',
// you can also override some properties
'deleteIfEmpty' => true
]
],
Then in my controller, just this simple code:
public function actionCart()
{
$cart = \Yii::$app->cart;
$cart->add(2);
}
I tried to:
- Remove configuration in main.php and is not working because configuration are missing, so mean is correctly configurate
- implement CartItemInterface in controller
- implement add() method in model and call from controller
- I try to pass in method integer, array and string
Where I am wrong? Thank you
Hi, you need to implement CartItemInterface in your model.
From docs
// Product is an AR model implementing CartProductInterface
$product = Product::findOne(1);
// add an item to the cart
$cart->add($product);
In this case I can not add serialize post?
You can only add active record model to cart.
ok understand, thank you
the solution?