我使用Yii2作爲我的電子商務網站。
這裏我使用的是omnilight/yii2-shopping-cart,但我不確定它爲什麼不保存會話產品的信息。Yii2購物車不保存到會話
我已經使用ajax請求把我的產品放到購物車的位置。
function actionAddToCart() {
$pid = $_REQUEST['pid'];
$quantity = $_REQUEST['quantity'];
$model = Product::findOne($pid);
$model->quantity = $quantity;
if ($model) {
// @@@ Add Cookie Data here
$cart = \Yii::$app->cart;
$params = [];
$params['price'] = $model->price;
$params['quantity'] = $quantity;
$cartPosition = $model->getCartPosition($params);
$cart->put($cartPosition, $quantity);
// var_dump($cart);
// die();
return $this->renderAjax('productView', [
'product' => $model
]);
}
}
當我在這裏得到購物車日誌時,我可以看到產品已添加到會話中。
但加載productView後,我看到會話中沒有產品。
任何幫助,將不勝感激。
這裏我使用CartPositionInterface,它具有ID,價格,顏色,尺寸,長度,數量等參數。 的車返回下面的結果:
object(yz\shoppingcart\ShoppingCart)#109 (6) {
["storeInSession"]=>
bool(true)
["session"]=>
object(yii\web\Session)#58 (6) {
["flashParam"]=>
string(7) "__flash"
["handler"]=>
NULL
["_cookieParams":"yii\web\Session":private]=>
array(1) {
["httponly"]=>
bool(true)
}
["_hasSessionId":"yii\web\Session":private]=>
bool(true)
["_events":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
NULL
}
["cartId"]=>
string(23) "myshoppingCart"
["_positions":protected]=>
array(1) {
["404c11b84c06bda0bf7464d5fdc85604"]=>
object(common\models\ProductCartPosition)#111 (7) {
["_product":protected]=>
NULL
["id"]=>
int(1)
["price"]=>
float(250)
["color"]=>
string(1) "1"
["size"]=>
string(1) "L"
["length"]=>
string(2) "56"
["quantity"]=>
string(1) "1"
}
}
["_events":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
array(0) {
}
}
但是當我從ProductView的日誌,它返回下面的結果:
object(yz\shoppingcart\ShoppingCart)#49 (6) {
["storeInSession"]=>
bool(true)
["session"]=>
object(yii\web\Session)#52 (6) {
["flashParam"]=>
string(7) "__flash"
["handler"]=>
NULL
["_cookieParams":"yii\web\Session":private]=>
array(1) {
["httponly"]=>
bool(true)
}
["_hasSessionId":"yii\web\Session":private]=>
NULL
["_events":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
NULL
}
["cartId"]=>
string(23) "aljazeera_shopping_cart"
["_positions":protected]=>
array(0) {
}
["_events":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
NULL
}
請讓我知道,如果您有任何意見。
感謝您的快速回復。 我以爲擴展會處理會話打開,所以我會再次檢查。 所以,你的意思是我必須在打電話給put方法之前先打開會話? – fxpage
以及當我使用var_dump方法獲得日誌時,我能夠看到會話有我的產品信息的原因是什麼? – fxpage
可能會議工作直接,但在這一刻我不記得確切..無論如何,你可以看到這個指南更多信息http://www.yiiframework.com/doc-2.0/guide-runtime-sessions-cookies .html – scaisEdge