Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Basket extends Controller {
- var $CustomerId;
- var $ColsolidatedShippingCharges;
- function Basket()
- {
- parent::Controller();
- $this->load->model('ProductOption');
- $this->load->model('ProductCustomiseArea');
- $this->load->model('OptionValue');
- $this->load->model('Product');
- $this->load->model('Option');
- $this->load->model('Currency');
- $this->Currency->Load();
- $this->load->helper('date');
- if( $this->session->userdata('logged_in')==true )
- {
- $this->load->model('CustomerBasket');
- $this->load->model('CustomerBasketProductOption');
- $this->load->model('CustomerBasketCustomiseArea');
- $this->Basket = $this->CustomerBasket;
- $this->BasketProductOption = $this->CustomerBasketProductOption;
- $this->BasketCustomiseArea = $this->CustomerBasketCustomiseArea;
- $this->CustomerId = $this->session->userdata('customer_id');
- }
- else
- {
- $this->load->model('VisitorBasket');
- $this->load->model('VisitorBasketProductOption');
- $this->load->model('VisitorBasketCustomiseArea');
- $this->Basket = $this->VisitorBasket;
- $this->BasketProductOption = $this->VisitorBasketProductOption;
- $this->BasketCustomiseArea = $this->VisitorBasketCustomiseArea;
- $this->CustomerId = $this->session->userdata('session_id');
- }
- }
- function index()
- {
- redirect('/home/index');
- // $this->layout->render('welcome_message');
- }
- function add()
- {
- if(empty($_POST['product_id']))
- die('Invalid Data');
- $post_count=count($_POST);
- $total_price_show = 0;
- $total_quantity_show = 0;
- $pro_data=$this->Product->Getprice($_POST['product_id']);
- //if($pro_data['price_type']== "regular" || $pro_data['price_type']== "sale")
- {
- $productdata = $this->Basket->GetCartValues($this->CustomerId);
- if(!empty($productdata))
- {
- foreach($productdata as $index=>$value)
- {
- $total_price_show += $value["price"];
- $total_quantity_show++;
- if($value['product_id']==$_POST['product_id'])
- {
- $newdata = array(
- 'themessage' => 'The product is already in the basket. You can update the quantity from below.',
- 'condition' => 'Y'
- );
- //$this->session->set_flashdata($newdata);
- //redirect('basket/view');
- }
- }
- }
- }
- if( $this->session->userdata('logged_in') == true ){
- $rec = $this->CustomerBasket->new_record();
- $rec->customer_id = $this->CustomerId;
- }
- else{
- $rec = $this->VisitorBasket->new_record();
- $rec->session_id = $this->session->userdata('session_id');
- }
- $rec->product_id = $_POST["product_id"];
- $quantity = ($_POST["quantity"] == '' || $_POST["quantity"] == 0 || !is_numeric($_POST["quantity"])) ? 1 : $_POST["quantity"];
- $rec->quantity = $quantity;
- $totalprice=0;
- $unit_price=0;
- $groupValues='';
- $group_Price=array();
- $groupPrice=0;
- foreach ($_POST as $key => $value) {
- if(strpos($key, 'groupOption_') !== false){
- if(empty($groupValues))
- $groupValues = $value;
- else
- $groupValues .= ",".$value;
- }
- }
- if(!empty($groupValues)){
- $group_Price = $this->ProductOption->getOptionsValuePrice($groupValues);
- }
- if(!empty($group_Price)){
- if($pro_data['price_type'] == "sale"){
- $groupPrice = $group_Price[0]['option_values_sale_price'];
- }
- else{
- $groupPrice = $group_Price[0]['option_values_price'];
- }
- }
- $rec->price = $totalprice+$groupPrice;
- $rec->unit_price = $unit_price;
- $rec->date_updated = mdate("%Y-%m-%d %h:%i:%s");
- $rec->date_added = mdate("%Y-%m-%d %h:%i:%s");
- $rec->save();
- $sizelist = $this->ProductOption->GetSizeList();
- $attachedAreaId = NULL;
- // if ($pro_data["price_type"]=="option_based")
- // {
- $this->ProductOption->group_by('option_id');
- $values=$this->ProductOption->find_all_by('product_id',$_POST["product_id"]);
- $order_total = 0;
- $pro = $this->Product->find($_POST["product_id"]);
- $base_price = ($pro->price_type == "regular" || $pro->price_type == "option_based") ? $pro->regular_price : $pro->sale_price;
- $order_total += $base_price;
- $basketid = $this->db->insert_id();
- $_SESSION['basketid'] = $basketid;
- $i = 0;
- // Add non percent product options
- foreach($values as $value)
- {
- // combo box, radiobox
- if( isset($_POST["option_" . $value->option_id]) && is_numeric($_POST["option_" . $value->option_id]) ){
- $opt = $this->OptionValue->find($_POST["option_" . $value->option_id]);
- if(in_array($opt->option_id, $sizelist))
- $attachedAreaId = $opt->option_id;
- if($opt->is_percent == 'N'){
- $order_total += ($this->addProductOptionValue($basketid, $value->option_id, $_POST["option_" . $value->option_id], $order_total,$pro_data['price_type']));
- }
- }
- // checkbox
- else if (!empty($_POST["option_".$value->option_id]) && is_array($_POST["option_".$value->option_id]))
- {
- $opt = $this->OptionValue->find($value->option_id);
- if($opt->is_percent == 'N'){
- foreach($_POST["option_".$value->option_id] as $OptionValueId){
- if(is_numeric($OptionValueId))
- $order_total += ($this->addProductOptionValue($basketid, $value->option_id, $OptionValueId, $order_total,$pro_data['price_type']));
- }
- }
- }
- } // end product options
- if($groupValues !=''){
- $opt_name=$this->ProductOption->get_option_name($groupValues);
- $totl_pr=$_POST['regular_price'];
- foreach ($opt_name as $value) {
- $totl_pr += ($this->addProductOptionValue($basketid, $value->option_id, $value->option_value_id, $totl_pr,$pro_data['price_type']));
- }
- }
- // insert customise area information****************************************
- // Add customize areas
- $areas =$this->ProductCustomiseArea->find_all_by('product_id',$_POST["product_id"]);
- foreach($areas as $value)
- {
- if(!empty($_POST["length_" . $value->id]) && !empty($_POST["width_" . $value->id]))
- {
- if( $this->session->userdata('logged_in') == true ){
- $recarea = $this->BasketCustomiseArea->new_record();
- $recarea->customer_basket_id = $basketid;
- }
- else{
- $recarea = $this->BasketCustomiseArea->new_record();
- $recarea->visitor_basket_id = $basketid;
- }
- $recarea->customise_area_id = $value->id;
- $recarea->title = $value->title;
- $recarea->unit = $value->unit;
- $recarea->width = $_POST["width_" . $value->id];
- $recarea->height = $_POST["length_" . $value->id];
- $recarea->first_label = $value->first_label;
- $recarea->second_label = $value->second_label;
- if($value->is_calculateable == 'Y'){
- $recarea->unit_price = $value->unit_price;
- $area_price = $this->ProductCustomiseArea->getAreaPrice($attachedAreaId, $_POST["length_" . $value->id], $_POST["width_" . $value->id], $value->unit_price, $order_total);
- $recarea->total_price = $area_price;
- }
- else{
- $recarea->unit_price = '0';
- $recarea->total_price = '0';
- }
- $order_total += $recarea->total_price;
- $recarea->save();
- }
- // $a++;
- }// `end customise area insertion ***********************************************************
- // }
- // Add percentage product options
- $this->ProductOption->group_by('option_id');
- $values2=$this->ProductOption->find_all_by('product_id',$_POST["product_id"]);
- foreach($values2 as $value)
- {
- // combo box, radiobox
- if( isset($_POST["option_" . $value->option_id]) && is_numeric($_POST["option_" . $value->option_id]) ){
- $opt2 = $this->OptionValue->find($_POST["option_" . $value->option_id]);
- if($opt2->is_percent == 'Y'){
- $order_total += ($this->addProductOptionValue($basketid, $value->option_id, $_POST["option_" . $value->option_id], $order_total,$pro_data['price_type']));
- }
- }
- // checkbox
- else if (!empty($_POST["option_".$value->option_id]) && is_array($_POST["option_".$value->option_id]))
- {
- $opt2 = $this->OptionValue->find($value->option_id);
- if($opt2->is_percent == 'Y'){
- foreach($_POST["option_".$value->option_id] as $OptionValueId){
- if(is_numeric($OptionValueId))
- $order_total += ($this->addProductOptionValue($basketid, $value->option_id, $OptionValueId, $order_total,$pro_data['price_type']));
- }
- }
- }
- } // end product options
- // update Basket *****************
- $rec_update = $this->Basket->find($basketid);
- $rec_update->price = $order_total * $quantity+$groupPrice;
- $rec_update->unit_price = $order_total+$groupPrice;
- if($rec_update->save())
- {
- $this->view(true);
- global $CURRENCY;
- $total_quantity_show++;
- $total_price_show += $rec_update->price;
- $return_data['html']= '<p>You have '.$total_quantity_show.' items in your basket</p>
- <p><strong>Total Cost: '.current($CURRENCY).number_format($total_price_show, 2, '.', '').'</strong></p>
- ';
- $return_data['quantity']=$total_quantity_show;
- $return_data['price']=number_format($total_price_show,2,'.','');
- echo json_encode($return_data);
- }
- else
- echo 'error';
- exit;
- //redirect('basket/view');
- }
- private function addProductOptionValue($basketid, $OptionId, $selectedOptionValue, $TotalOrder=0, $price_type='Regular')
- {
- if( $this->session->userdata('logged_in') == true )
- $recrd = $this->CustomerBasketProductOption->new_record();
- else
- $recrd = $this->VisitorBasketProductOption->new_record();
- $recrd->basket_id = $basketid;
- $sizelist = $this->ProductOption->GetSizeList();
- $data_opt = $this->ProductOption->GetProductValueDetail($selectedOptionValue, $_POST["product_id"]);
- $isAreaSelected =false;
- $areas =$this->ProductCustomiseArea->find_all_by(array('product_id','is_calculateable','is_active', 'is_deleted'),array($_POST["product_id"],'Y', 'Y', 'N'));
- foreach($areas as $value)
- {
- if(!empty($_POST["length_" . $value->id]) && !empty($_POST["width_" . $value->id]) )
- $isAreaSelected =true;
- }
- if(empty($data_opt)) return 0;
- if(($isAreaSelected && !in_array($data_opt[0]['option_id'], $sizelist)) || (!$isAreaSelected))
- {
- $recrd->option_id = $data_opt[0]['option_id'];
- $recrd->option_value_id = $data_opt[0]['option_value_id'];
- if($price_type == "sale"){
- $recrd->price = ($data_opt[0]['is_percent']=='Y') ? ($data_opt[0]['price']/100)* $TotalOrder: $data_opt[0]['sale_price'];
- }
- else{
- $recrd->price = ($data_opt[0]['is_percent']=='Y') ? ($data_opt[0]['price']/100)* $TotalOrder: $data_opt[0]['price'];
- }
- $recrd->prefix = $data_opt[0]['prefix'];
- }
- else
- return 0;
- $option_name = $this->Option->find($OptionId);
- $recrd->name = $option_name->name;
- $option_value = $this->OptionValue->find($selectedOptionValue);
- $recrd->value = $option_value->value;
- $recrd->save();
- return ''.$recrd->prefix.$recrd->price;
- }
- function view($returnData=false)
- {
- global $CURRENCY;
- if( $this->session->userdata('logged_in') == true )
- $data = $this->Basket->GetCartValues($this->CustomerId);
- else
- $data = $this->Basket->GetCartValues($this->session->userdata('session_id'));
- $data_array=array();
- $j=0;
- $totalQuantity = $totalProducts = $totalPrice = 0;
- $currencyCode = '';
- $currencyCode = (current($CURRENCY));
- foreach($data as $key=>$value)
- {
- $product_p=$this->Product->Getprice($value['product_id']);
- if($product_p['price_type']=='sale') { $product_price=$product_p['sale_price']; }
- else { $product_price=$product_p['regular_price']; }
- $url_slug=$this->ProductOption->get_slug('products','id',$value['product_id']);
- $data_array['rec'][$j]['name'] = $value['name'];
- $data_array['rec'][$j]['code'] = $value['code'];
- $data_array['rec'][$j]['url_slug'] = $url_slug;
- $data_array['rec'][$j]['base_price'] = $product_price;
- $data_array['rec'][$j]['currency_code'] = $CURRENCY[$value['currency_id']] ;
- $data_array['rec'][$j]['unit_price'] = $value['unit_price'];
- $data_array['rec'][$j]['quantity'] = $value['quantity'];
- $data_array['rec'][$j]['price'] = $value['price'];
- $data_array['rec'][$j]['price_type'] = $value['price_type'];
- $data_array['rec'][$j]['id'] = $value['id'];
- $data_array['rec'][$j]['product_id'] = $value['product_id'];
- $data_array['rec'][$j]['currency_id'] = $value['currency_id'];
- $data_array['rec'][$j]['option_values'] = $value['values'];
- $data_array['rec'][$j]['area_values'] = $value['customise_areas'];
- $totalQuantity += $value['quantity'];
- $totalPrice += $value['price'];
- $totalProducts++;
- $j++;
- }
- $this->session->set_userdata('quantity', $totalQuantity);
- $this->session->set_userdata('total_price', $totalPrice);
- $this->session->set_userdata('currency_code', $currencyCode);
- $this->session->set_userdata('totalpurchase', $totalProducts);
- $data_array['currency_code'] = $currencyCode;
- $data_array['meta']['title'] = HOME_META_TITLE;
- $data_array['meta']['keywords'] = HOME_META_KEYWORD;
- $data_array['meta']['description'] = HOME_META_DESCRIPTION;
- if($returnData)
- return $data_array;
- else
- $this->layout->render('basket/view', $data_array,false,'header_less_layout');
- }
- function update()
- {
- foreach($_POST["quantity"] as $basket_id=>$quantity)
- {
- $allprodutcs = "";
- $myBaskets = $this->Basket->find_by(array('id'=> $basket_id));
- $quantity_response = $this->Product->VerifyQuantity($myBaskets->product_id, $quantity);
- if($quantity_response === true)
- $this->Basket->Update_Basket($basket_id, $quantity);
- else
- {
- $allproducts .= $quantity_response . ", ";
- }
- }
- if(!empty($allproducts))
- {
- $newdata = array(
- 'showmessage' => 'Y',
- 'products' => $allproducts
- );
- $this->session->set_flashdata($newdata);
- }
- redirect('basket/view');
- }
- function remove($basket_id)
- {
- if( $this->session->userdata('logged_in') == true ){
- $this->Basket->Delete_Basket($basket_id);
- //$this->BasketProductOption->Delete_Basket($basket_id);
- }
- else{
- $this->Basket->Delete_Basket($basket_id);
- //$this->BasketProductOption->Delete_Basket($basket_id);
- }
- redirect('basket/view');
- }
- function listing()
- {
- if( $this->session->userdata('logged_in') == true )
- $data = $this->Basket->GetCartValues($this->CustomerId);
- else
- $data = $this->Basket->GetCartValues($this->CustomerId);
- print_r($data);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment