Yii2 购物车不保存到会话
Yii2 shopping cart doesn't save to session
我正在为我的电子商务网站使用 Yii2。
我在这里使用 omnilight/yii2-shopping-cart,但我不确定为什么它没有保存到会话产品信息中。
我已经使用 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 获取日志时,它 returns 低于结果 :
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
}
如果您有任何建议,请告诉我。
对于 sessione 你应该使用
$session = new Session;
$session->open();
$value1 = $session['name1']; // get session variable 'name1'
$value2 = $session['name2']; // get session variable 'name2'
foreach ($session as $name => $value) // traverse all session variables
$session['name3'] = $value3; // set session variable 'name3'
参考这个。 http://www.yiiframework.com/doc-2.0/yii-web-session.html
这发生在我的本地主机上,因为在我的 xampp 设置中,php 会话已关闭。
我不确定,但在尝试解决之后,它开始保存会话。
我认为在 xampp 目录的 php.ini 中将 session_auto_start 标志更改为 1 后它开始工作了。
我正在为我的电子商务网站使用 Yii2。
我在这里使用 omnilight/yii2-shopping-cart,但我不确定为什么它没有保存到会话产品信息中。
我已经使用 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 获取日志时,它 returns 低于结果 :
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
}
如果您有任何建议,请告诉我。
对于 sessione 你应该使用
$session = new Session;
$session->open();
$value1 = $session['name1']; // get session variable 'name1'
$value2 = $session['name2']; // get session variable 'name2'
foreach ($session as $name => $value) // traverse all session variables
$session['name3'] = $value3; // set session variable 'name3'
参考这个。 http://www.yiiframework.com/doc-2.0/yii-web-session.html
这发生在我的本地主机上,因为在我的 xampp 设置中,php 会话已关闭。 我不确定,但在尝试解决之后,它开始保存会话。
我认为在 xampp 目录的 php.ini 中将 session_auto_start 标志更改为 1 后它开始工作了。