我使用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