<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Add_to_cart extends CI_Controller
{

    public function index()
    {
        if(isset($_POST['data']) && !empty($_POST['data'])){
            // Convert JSON string to Array
            $someArray = json_decode($_POST['data'], true);
            
            
            $_SESSION['products']=$someArray;
            if(!empty($_SESSION['primary_id'])){
                unset($_SESSION['primary_id']);
                unset($_SESSION['cart_p_id']);
                unset($_SESSION['cart_size_id']);
                unset($_SESSION['cart_size_name']);
                unset($_SESSION['cart_color_id']);
                unset($_SESSION['p_pv']);
                unset($_SESSION['cart_color_name']);
                unset($_SESSION['cart_p_qty']);
                unset($_SESSION['cart_p_current_price']);
                unset($_SESSION['cart_p_old_price']);
                unset($_SESSION['cart_p_name']);
                unset($_SESSION['cart_p_featured_photo']);
            }    
                
            for($i=0;$i<count($someArray);$i++){
                $_SESSION['primary_id'][$i] = $someArray[$i]["productId"];
                $_SESSION['cart_p_id'][$i] = $someArray[$i]["cart_p_id"];
                $_SESSION['cart_size_id'][$i] = $someArray[$i]["cart_size_id"];
                $_SESSION['cart_size_name'][$i] = $someArray[$i]["cart_size_name"];
                $_SESSION['cart_color_id'][$i] = $someArray[$i]["cart_color_id"];
                $_SESSION['p_pv'][$i] = $someArray[$i]["p_pv"];
                $_SESSION['cart_color_name'][$i] = $someArray[$i]["cart_color_name"];
                $_SESSION['cart_p_qty'][$i] = $someArray[$i]["cart_p_qty"];
                $_SESSION['cart_p_current_price'][$i] = $someArray[$i]["cart_p_current_price"];
                $_SESSION['cart_p_old_price'][$i] = $someArray[$i]["cart_p_old_price"];
                $_SESSION['cart_p_name'][$i] = $someArray[$i]["cart_p_name"];
                $_SESSION['cart_p_featured_photo'][$i] = $someArray[$i]["image"];
                
            }
            echo 1;
        }
    }
}