在 symfony2 中为购物车更新会话
Update session in symfony2 for shoppingg cart
我有以下代码:
public function addAction(Request $request){
//Get submited data
// Get Value from session
$sessionVal = $this->get('session')->get('aBasket');
// Append value to retrieved array.
$aBasket = $request->request->all();
if(count($sessionVal) > 0) {
foreach ($sessionVal as $key=>$value) {
if($aBasket['product_id'] == $sessionVal[$key]['product_id'])
{
$sessionVal[$key]['product_quantity'] = $sessionVal[$key]['product_quantity'] + $aBasket['product_quantity'];
$this->get('session')->set('aBasket', $sessionVal);
}
else
{
$sessionVal[] = $aBasket;
$this->get('session')->set('aBasket', $sessionVal);
}
}
}
else
{
$sessionVal[] = $aBasket;
$this->get('session')->set('aBasket', $sessionVal);
}
// Set value back to session
return $this->redirect($this->generateUrl('shop_desktop_homepage'));
}
想法是增加现有产品的数量,如果 id 不对应则添加它们。现在添加的数量是正确的,而且产品也是added.Exist一个解决方案??请帮助我...
你可以简化你的代码,我想你的会话数组看起来像
array(
'11' =>array('id'=>'11','title'=>'some product','product_quantity'=>2),
'12' =>array('id'=>'12','title'=>'some product','product_quantity'=>1),
'13' =>array('id'=>'13','title'=>'some product','product_quantity'=>3),
);
您的购物车数组中的键将是产品 ID,因此现在在下面的代码中我已经删除了 foreach
循环,而不是只使用了 if 检查 if(isset($sessionVal[$aBasket['product_id']]))
产品是否已存在于购物车数组中,将产品 ID 替换为键,例如 if(isset($sessionVal['11']))
如果存在则将数量增加一,如果不存在则将产品插入购物车数组
public function addAction( Request $request ) {
$sessionVal = $this->get( 'session' )->get( 'aBasket' );
$aBasket = $request->request->all();
if(isset($sessionVal[$aBasket['product_id']])){
$sessionVal[$aBasket['product_id']]['product_quantity'] += 1;
}else{
$sessionVal[$aBasket['product_id']]= array(
'id'=>$aBasket['product_id'],
'product_quantity' => 1,
'other_info' => '...'
);
}
$this->get( 'session' )->set( 'aBasket', $sessionVal );
return $this->redirect( $this->generateUrl( 'shop_desktop_homepage' ) );
}
我有以下代码:
public function addAction(Request $request){
//Get submited data
// Get Value from session
$sessionVal = $this->get('session')->get('aBasket');
// Append value to retrieved array.
$aBasket = $request->request->all();
if(count($sessionVal) > 0) {
foreach ($sessionVal as $key=>$value) {
if($aBasket['product_id'] == $sessionVal[$key]['product_id'])
{
$sessionVal[$key]['product_quantity'] = $sessionVal[$key]['product_quantity'] + $aBasket['product_quantity'];
$this->get('session')->set('aBasket', $sessionVal);
}
else
{
$sessionVal[] = $aBasket;
$this->get('session')->set('aBasket', $sessionVal);
}
}
}
else
{
$sessionVal[] = $aBasket;
$this->get('session')->set('aBasket', $sessionVal);
}
// Set value back to session
return $this->redirect($this->generateUrl('shop_desktop_homepage'));
}
想法是增加现有产品的数量,如果 id 不对应则添加它们。现在添加的数量是正确的,而且产品也是added.Exist一个解决方案??请帮助我...
你可以简化你的代码,我想你的会话数组看起来像
array(
'11' =>array('id'=>'11','title'=>'some product','product_quantity'=>2),
'12' =>array('id'=>'12','title'=>'some product','product_quantity'=>1),
'13' =>array('id'=>'13','title'=>'some product','product_quantity'=>3),
);
您的购物车数组中的键将是产品 ID,因此现在在下面的代码中我已经删除了 foreach
循环,而不是只使用了 if 检查 if(isset($sessionVal[$aBasket['product_id']]))
产品是否已存在于购物车数组中,将产品 ID 替换为键,例如 if(isset($sessionVal['11']))
如果存在则将数量增加一,如果不存在则将产品插入购物车数组
public function addAction( Request $request ) {
$sessionVal = $this->get( 'session' )->get( 'aBasket' );
$aBasket = $request->request->all();
if(isset($sessionVal[$aBasket['product_id']])){
$sessionVal[$aBasket['product_id']]['product_quantity'] += 1;
}else{
$sessionVal[$aBasket['product_id']]= array(
'id'=>$aBasket['product_id'],
'product_quantity' => 1,
'other_info' => '...'
);
}
$this->get( 'session' )->set( 'aBasket', $sessionVal );
return $this->redirect( $this->generateUrl( 'shop_desktop_homepage' ) );
}