yii2:使用购物车扩展的麻烦

时间:2015-04-04 05:47:53

标签: yii2 shopping-cart yii-extensions

我尝试使用https://github.com/omnilight/yii2-shopping-cart

我改变了我的产品型号

class ShopItems extends ActiveRecord implements CartPositionInterface
{
    use CartPositionTrait;

    //...

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

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

    //...
}

我在控制器中添加了操作

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();
}

当然在主配置中添加组件。

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

我现在如何在购物车中添加产品? 我找到this,但我无法理解构建动作的形式。

请帮忙!

UPD

我使用这样的动作:

  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();
  }

接下来:

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

在购物车视图中显示<?php print_r( Yii::$app->cart ) ?>后显示:

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] => )

1 个答案:

答案 0 :(得分:2)

在您的产品视图中:

<?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(); ?>

在你的控制器中:

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();
}

购物车索引视图:

<?php

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


      ?>
_cart_item视图中的

$ position是ShopItems模型:

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