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

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

Open
gabriele-carbonai opened this issue Jul 11, 2019 · 6 comments

Comments

@gabriele-carbonai
Copy link

gabriele-carbonai commented Jul 11, 2019

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

@ihorchepurnyi
Copy link
Contributor

Hi, you need to implement CartItemInterface in your model.

@ihorchepurnyi
Copy link
Contributor

ihorchepurnyi commented Jul 11, 2019

From docs

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

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

@gabriele-carbonai
Copy link
Author

In this case I can not add serialize post?

@ihorchepurnyi
Copy link
Contributor

You can only add active record model to cart.

@gabriele-carbonai
Copy link
Author

ok understand, thank you

@mavelar
Copy link

mavelar commented Apr 24, 2020

the solution?

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

3 participants