Search code examples
yii2shopping-cartyii-extensions

yii2: trouble with using shopping cart extension


I trying to use https://github.com/omnilight/yii2-shopping-cart

I change my product model like this

class ShopItems extends ActiveRecord implements CartPositionInterface
{
    use CartPositionTrait;

    //...

    public function getPrice()
    {
        return $this->price;
    }

    public function getId()
    {
        return $this->id;
    }

    //...
}

I added action in my controller

public function actionAddToCart($id)
{
    $cart = new ShoppingCart();

    $model = ShopItems::findOne($id);
    if ($model) {
        $cart->put($model, 1);
        return $this->redirect(['cart-view']);
    }
    throw new NotFoundHttpException();
}

And of course add component in main config.

'components' => [
    'cart' => [
        'class' => 'yz\shoppingcart\ShoppingCart',
        'cartId' => 'my_application_cart',
    ]
]

How can i add product in cart now? I find this, but i can't understand how build action form.

Please, help!

UPD

I use action like this:

  public function actionAddToCart($id) {
    $cart = new ShoppingCart();
    $model = ShopItems::findOne($id);
    if ($model) {
      $cart->put($model, 1);
      return print_r($cart);
//      return $this->redirect(['cart-view']);
    }
    throw new NotFoundHttpException();
  }

And get next:

yz\shoppingcart\ShoppingCart Object ( [storeInSession] => 1 [session] => yii\web\Session Object ( [flashParam] => __flash [handler] => [_cookieParams:yii\web\Session:private] => Array ( [httponly] => 1 ) [_hasSessionId:yii\web\Session:private] => [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => ) [cartId] => yz\shoppingcart\ShoppingCart [_positions:protected] => Array ( [95] => common\modules\shop\models\ShopItems Object ( [_attributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 95 [category_id] => 1 [title] => Item1 [desc] =>
description

[price] => 200 [currency] => USD [discount] => 20 [in_stock] => 1 [photos] => ["/common/modules/shop/uploads/bfd71_586_0af4a4fdbb56412bcc2b9ac3f9a2432d.jpg","/common/modules/shop/uploads/CkS1pXE8e6g.jpg","/common/modules/shop/uploads/e3pucV_1KJ0.jpg","/common/modules/shop/uploads/eEh-VNPe1jI.jpg","/common/modules/shop/uploads/HbzbFG9kG5s.jpg"] [options] => 0 [created_at] => 1424858174 [updated_at] => 1425898766 ) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 95 [category_id] => 1 [title] => Item1 [desc] =>
description

[price] => 200 [currency] => USD [discount] => 20 [in_stock] => 1 [photos] => ["/common/modules/shop/uploads/bfd71_586_0af4a4fdbb56412bcc2b9ac3f9a2432d.jpg","/common/modules/shop/uploads/CkS1pXE8e6g.jpg","/common/modules/shop/uploads/e3pucV_1KJ0.jpg","/common/modules/shop/uploads/eEh-VNPe1jI.jpg","/common/modules/shop/uploads/HbzbFG9kG5s.jpg"] [options] => 0 [created_at] => 1424858174 [updated_at] => 1425898766 ) [_related:yii\db\BaseActiveRecord:private] => Array ( ) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array ( [beforeInsert] => Array ( [0] => Array ( [0] => Array ( [0] => yii\behaviors\TimestampBehavior Object ( [createdAtAttribute] => created_at [updatedAtAttribute] => updated_at [value] => [attributes] => Array ( [beforeInsert] => Array ( [0] => created_at [1] => updated_at ) [beforeUpdate] => updated_at ) [owner] => common\modules\shop\models\ShopItems Object *RECURSION* ) [1] => evaluateAttributes ) [1] => ) ) [beforeUpdate] => Array ( [0] => Array ( [0] => Array ( [0] => yii\behaviors\TimestampBehavior Object ( [createdAtAttribute] => created_at [updatedAtAttribute] => updated_at [value] => [attributes] => Array ( [beforeInsert] => Array ( [0] => created_at [1] => updated_at ) [beforeUpdate] => updated_at ) [owner] => common\modules\shop\models\ShopItems Object *RECURSION* ) [1] => evaluateAttributes ) [1] => ) ) ) [_behaviors:yii\base\Component:private] => Array ( [0] => yii\behaviors\TimestampBehavior Object ( [createdAtAttribute] => created_at [updatedAtAttribute] => updated_at [value] => [attributes] => Array ( [beforeInsert] => Array ( [0] => created_at [1] => updated_at ) [beforeUpdate] => updated_at ) [owner] => common\modules\shop\models\ShopItems Object *RECURSION* ) ) [_quantity:protected] => 8 ) ) [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => Array ( ) ) 1

And after it <?php print_r( Yii::$app->cart ) ?> in cart-view show this:

yz\shoppingcart\ShoppingCart Object ( [storeInSession] => 1 [session] => yii\web\Session Object ( [flashParam] => __flash [handler] => [_cookieParams:yii\web\Session:private] => Array ( [httponly] => 1 ) [_hasSessionId:yii\web\Session:private] => [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => ) [cartId] => my_application_cart [_positions:protected] => Array ( ) [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => )

Solution

  • In your product view:

    <?php $form = ActiveForm::begin(['class'=>'form-horizontal', 'action'=>Url::toRoute(['controllername/add-to-cart','id'=>$product->id])]); ?>
    
        <?=Html::input('submit','submit','Add to cart',[
                    'class'=>'button add',
    
                  ])?>
    <?php ActiveForm::end(); ?>
    

    And in your controller:

    public function actionAddToCart($id)
    {
        $cart = Yii::$app->cart;;
    
        $model = ShopItems::findOne($id);
        if ($model) {
            $cart->put($model, 1);
            return $this->redirect(['cart-view']);
        }
        throw new NotFoundHttpException();
    }
    

    Cart index view:

    <?php
    
            /** @var ShoppingCart $sc */
            foreach(Yii::$app->cart->positions as $position){
              echo $this->render('_cart_item',['position'=>$position]);
              //var_dump($position);
            }
    
    
          ?>
    

    in _cart_item view $position is ShopItems model:

    <?= $position->id ?>
    <?= $position->price ?>
    <?= $position->name ?>
    <?= $position->quantity ?>