2015-04-04 31 views
0

我試圖用https://github.com/omnilight/yii2-shopping-cartyii2:麻煩,使用購物車的擴展

更改我的產品型號這樣的

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

回答

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視圖$位置

是ShopItems型號:

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

After cl ick on button - 重定向'cart-view',所以,我認爲這是工作!謝謝!但我怎麼能顯示購物車項目,我嘗試'foreach(Yii :: $ app-> cart-> getPositions()爲$ pos){ echo'id:'。 $ pos-> getId()。 ',成本:'。 $ POS-> getCost(); }'但它是行不通的 – soovorow 2015-04-04 11:40:15

+0

更新的答案與購物車索引示例 – user1852788 2015-04-04 11:58:09

+0

如果您的購物車索引視圖示例是工作,我可以在所有頁面中使用它,對不對?我創建_cart_item.php;我嘗試把它(車索引視圖)放在佈局中,但出錯了。它什麼也沒有顯示。當我嘗試寫入「Yii :: $ app-> ca」或「Yii :: $ app-> cart-> pos」時,我的phpstorm不顯示自動完成提示。 – soovorow 2015-04-04 12:23:57