yii2-cart icon indicating copy to clipboard operation
yii2-cart copied to clipboard

Argument 1 passed to yii2mod\cart\Cart::add() must be an instance of yii2mod\cart\models\CartItemInterface

Open gabriele-carbonai opened this issue 5 years ago • 6 comments

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:

  1. Remove configuration in main.php and is not working because configuration are missing, so mean is correctly configurate
  2. implement CartItemInterface in controller
  3. implement add() method in model and call from controller
  4. I try to pass in method integer, array and string

Where I am wrong? Thank you

gabriele-carbonai avatar Jul 11 '19 09:07 gabriele-carbonai

Hi, you need to implement CartItemInterface in your model.

ihorchepurnyi avatar Jul 11 '19 09:07 ihorchepurnyi

From docs

// Product is an AR model implementing CartProductInterface
$product = Product::findOne(1);

// add an item to the cart
$cart->add($product);

ihorchepurnyi avatar Jul 11 '19 09:07 ihorchepurnyi

In this case I can not add serialize post?

gabriele-carbonai avatar Jul 11 '19 10:07 gabriele-carbonai

You can only add active record model to cart.

ihorchepurnyi avatar Jul 11 '19 10:07 ihorchepurnyi

ok understand, thank you

gabriele-carbonai avatar Jul 11 '19 10:07 gabriele-carbonai

the solution?

mavelar avatar Apr 24 '20 07:04 mavelar