【问题标题】:yii2: trouble with using shopping cart extensionyii2:使用购物车扩展的麻烦
【发布时间】:2015-06-09 04:41:00
【问题描述】:

我尝试使用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] => )

【问题讨论】:

    标签: yii2 shopping-cart yii-extensions


    【解决方案1】:

    在您的产品视图中:

    <?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 ?>
    

    【讨论】:

    • 点击按钮后 - 重定向到'cart-view',所以,我认为这是可行的!谢谢!但是我怎样才能显示购物车项目,我尝试` foreach (Yii::$app->cart->getPositions() as $pos) { echo 'id: ' 。 $pos->getId() 。 ', 成本: ' 。 $pos->getCost(); }` 但它不起作用
    • 如果您的购物车索引视图示例有效,我可以在所有页面中使用它,对吗?我创建_cart_item.php;我尝试将它(购物车索引视图)放在布局中,但出了点问题。它什么都不显示。当我尝试编写“Yii::$app->ca”或“Yii::$app->cart->pos”时,我的 phpstorm 没有显示自动完成提示。
    • 而且它不会抛出异常。所以,我认为添加商品或显示购物车时遇到麻烦.. :(
    • 先试试它作为控制器视图。没关系 phpstorm 自动完成不能与组件一起工作。尝试使用 print_r(Yii::$app->cart->positions) 进行调试
    • print_r(Yii::$app-&gt;cart-&gt;positions) 的结果:Array ( )。还有一件事:点击按钮(添加到购物车)后,我在cart-view 上重定向并得到#404 错误。所以,我不能创建actionCart-view() 对。我尝试 redirect(['cart']) 并创建 actionCart 渲染 cart-view 渲染 _cart_itemprint_r(Yii::$app-&gt;cart-&gt;positions) 反正显示空数组。
    猜你喜欢
    • 2014-06-11
    • 2012-05-31
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 2023-01-29
    • 2015-12-13
    • 2018-11-25
    • 1970-01-01
    相关资源
    最近更新 更多