synapse10
            (Marcelorru)
          
          
          
              
              
          1
          
         
        
          I have this code in my webapplication develop with yii 1.x, i would  change this code for yii2
if(isset($_SESSION["cart_products"])){  //if session var already exist
            if(isset($_SESSION["cart_products"][$new_product['product_code']])) //check item exist in products array
            {
                unset($_SESSION["cart_products"][$new_product['product_code']]); //unset old array item
            }           
        }
        $_SESSION["cart_products"][$new_product['product_code']] = $new_product; //update or create product session with new item  
         
        
          
        
           
           
           
         
         
            
            
          
       
      
        
          
          
            synapse10
            (Marcelorru)
          
          
          
              
              
          3
          
         
        
          
i have crrected my code, butm another error in this line:  $session[‘cart_products’][$new_product[‘product_code’]] = $new_product;
the error is: Indirect modification of overloaded element of yii\web\Session has no effect
    $connection = \Yii::$app->db;
    $session = Yii::$app->session;
  
    //add product to session or create new one
    if(isset($_POST["type"]) && $_POST["type"]=='add' && $_POST["product_qty"]>0)
    {
        foreach($_POST as $key => $value){ //add all post vars to new_product array
            $new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING);
        }
        //remove unecessary vars
        unset($new_product['type']);
        unset($new_product['return_url']); 
        
        
       
  // $result= Yii::app()->db->createCommand('SELECT product_name, price FROM products WHERE product_code = :category')->bindValue('category',$_POST["product_code"])->queryAll();
   $user = $connection
    ->createCommand('SELECT product_name, price FROM products WHERE product_code = :category');
    $user->bindValue(':category', $_POST["product_code"]);
    $result = $user->query();
    $rs0=array();
    foreach ($result as $item0) {
        
        
        //fetch product name, price from db and add to new_product array
        $new_product["product_name"] = $rs0[]=$item0['product_name']; 
        $new_product["product_price"] = $rs0[]=$item0['price'];
      
        
        if(isset($session['cart_products'])){  //if session var already exist
            if(isset($session['cart_products'][$new_product['product_code']])) //check item exist in products array
            print_r($session['cart_products']);
            {
                unset($session['cart_products'][$new_product['product_code']]); //unset old array item
            }           
        }
        $session['cart_products'][$new_product['product_code']] = $new_product; //update or create product session with new item  
        
    } 
}
//update or remove items 
if(isset($_POST["product_qty"]) || isset($_POST["remove_code"]))
{
    //update item quantity in product session
    if(isset($_POST["product_qty"]) && is_array($_POST["product_qty"])){
        foreach($_POST["product_qty"] as $key => $value){
            if(is_numeric($value)){
                $session["cart_products"][$key]["product_qty"] = $value;
            }
        }
    }
    //remove an item from product session
    if(isset($_POST["remove_code"]) && is_array($_POST["remove_code"])){
        foreach($_POST["remove_code"] as $key){
            unset($session["cart_products"][$key]);
        }   
    }
}
//return $this->redirect('index',302);
//back to return url
$return_url = (isset($_POST["return_url"]))?urldecode($_POST["return_url"]):''; //return url
header('Location:'.$return_url);
//header('Location:'.$return_url);
    }