|
Server : Apache/2.4.18 (Ubuntu) System : Linux canvaswebdesign 3.13.0-71-generic #114-Ubuntu SMP Tue Dec 1 02:34:22 UTC 2015 x86_64 User : oppastar ( 1041) PHP Version : 7.0.33-0ubuntu0.16.04.15 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, Directory : /var/www/kamariallee.com/public_html/application/controllers/ |
Upload File : |
<?php
if (!defined('BASEPATH'))
{
exit('No direct script access allowed');
}
class Cart extends Public_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('cart_model');
$this->load->model('product_m');
$this->load->library('form_validation');
$this->load->library('cart');
}
public function index()
{
//get SEO
$this->db->select('website_name')->from('configuration')->where('id_configuration', 1);
$website_name = $this->db->get()->row();
$this->data_header['browser_title'] = ucwords($website_name->website_name) . ' - My Cart';
$this->data_header['meta_description'] = ucwords($website_name->website_name) . ' - My Cart';
if (!$this->cart->contents())
{
$data['message'] = '<p style="background:grey; color:white;
padding:10px; margin-bottom:100px;">Your cart is empty</p>';
} else
{
$data['message'] = $this->session->flashdata('message');
}
// echo '<pre>';
// var_dump($this->cart->contents());
// echo '</pre>';
// exit;
//calculate subtotals
$data['subtotals'] = 0;
foreach($this->cart->contents() as $item)
{
$data['subtotals'] += $item['subtotal'];
if(isset($item['options']['greetingcard_price_int']))
{
$data['subtotals'] += $item['options']['greetingcard_price_int'];
}
if(isset($item['options']['chosenHeelsTypePrice']))
{
$data['subtotals'] += ($item['qty'] * $item['options']['chosenHeelsTypePrice']);
}
if(isset($item['options']['chosenHeelsHeightPrice']))
{
$data['subtotals'] += ($item['qty'] * $item['options']['chosenHeelsHeightPrice']);
}
if(isset($item['options']['chosenPlatformStylePrice']))
{
$data['subtotals'] += ($item['qty'] * $item['options']['chosenPlatformStylePrice']);
}
if(isset($item['options']['chosenPlatformHeightPrice']))
{
$data['subtotals'] += ($item['qty'] * $item['options']['chosenPlatformHeightPrice']);
}
if(isset($item['options']['chosenInitialEngravedPrice']))
{
$data['subtotals'] += ($item['qty'] * $item['options']['chosenInitialEngravedPrice']);
}
}
$this->load->view('template/header', $this->data_header);
$this->load->view('cart', $data);
$this->load->view('template/footer', $this->data_footer);
}
public function remove($rowid)
{
if ($rowid=="all") {
$this->cart->destroy();
}
else
{
$data = array(
'rowid' => $rowid,
'qty' => 0
);
$this->cart->update($data);
}
redirect('cart');
}
public function update_cart() {
//check if there is post request, if not, reject & redirect
if (!isset($_POST['update_cart'])) {
redirect('cart');
}
// Recieve post values,calcute them and update
$cart_info = $_POST['cart'] ;
foreach( $cart_info as $id => $cart) {
$sku = $cart['options']['sku'];
$attributes = $cart['options']['size'];
//check the stock for current SKU and Size
$this->db->select('stock')->from('product_details')->where('sku', $sku)->where('attributes', $attributes);
$available_stock = $this->db->get()->row()->stock;
if ($cart['qty'] > $available_stock) {
//stocks are not enough
$this->session->set_flashdata('no_stock', '<br>
<p style="background:grey; color:white; padding:5px; font-weight:bold;">Sorry not enough stock for chosen quantity. Please choose smaller quantity.</p>');
redirect('cart');
} else {
//stocks are enough
$rowid = $cart['rowid'];
$price = $cart['price'];
$qty = $cart['qty'];
$amount = $price * $cart['qty'];
$data = array(
'rowid' => $rowid,
'price' => $price,
'amount' => $amount,
'qty' => $qty
);
$this->cart->update($data);
}
}
redirect('cart');
}
//callback function validation cek stock available when add to cart
public function cek_stock() {
$id_product_details = $this->input->post('product_size');
$chosen_quantity = (int) $this->input->post('qty');
//get current stock froms product_details table
$this->db->select('stock');
$this->db->from('product_details');
$this->db->where('id_product_details', $id_product_details);
$query = $this->db->get();
$current_stock = (int) $query->row()->stock;
//check if quantity is less or equal to current stock
if ($chosen_quantity > $current_stock) {
return FALSE;
} else {
return TRUE;
}
}
//add cart from product page
public function ajax_addtocart()
{
//test if ajax call to prevent direct access
if(!$this->input->is_ajax_request())
{
exit('No direct script access allowed');
}
$chosen_color = trim(strtolower($this->input->post('chosen_color')));
$chosen_size = trim($this->input->post('chosen_size'));
$product_id = (int) $this->input->post('product_id');
$greetingcard_text = $this->input->post('greetingcard_text');
//check if this combination exist
$attributes = $chosen_size . ',' . $chosen_color;
$product_detail = $this->db->select('*')->from('product_details')->where('product_id', $product_id)->where('attributes', $attributes)->get()->row_array();
if(!$product_detail)
{
return $this->output
->set_content_type('application/json')
->set_status_header(404)
->set_output(json_encode([
'error' => 'Product not found',
]));
}
//check current cart quantity
$cart_qty = 0;
$is_new_item = TRUE;
$rowid = NULL;
if(count($this->cart->contents()) > 0)
{
foreach($this->cart->contents() as $item)
{
if($item['id'] == $product_detail['product_id'] && $item['options']['sku'] == $product_detail['sku'])
{
$is_new_item = FALSE;
$cart_qty = $item['qty'];
$rowid = $item['rowid'];
break;
}
}
}
$requested_qty = $cart_qty + 1;
if($requested_qty > $product_detail['stock'])
{
return $this->output
->set_content_type('application/json')
->set_status_header(400)
->set_output(json_encode([
'error' => 'Stock not enough',
]));
}
//get product data
$product = $this->db->select('title, image1, image2, image3, image4, image5')->from('products')->where('id_products', $product_detail['product_id'])->get()->row_array();
$data_cart['id'] = $product_id;
$data_cart['name'] = ucwords($product['title']);
$data_cart['qty'] = $requested_qty;
if($product_detail['discounted_price'] > 0)
{
$data_cart['price'] = $product_detail['discounted_price'];
}
else
{
$data_cart['price'] = $product_detail['price'];
}
$data_cart['options']['sku'] = $product_detail['sku'];
$data_cart['options']['color'] = ucwords($chosen_color);
$data_cart['options']['size'] = $chosen_size;
//get chosen image
switch ($product_detail['image']) {
case 'image1':
$data_cart['options']['image'] = base_url() . 'uploads/product/small/' . $product['image1'];
break;
case 'image2':
$data_cart['options']['image'] = base_url() . 'uploads/product/small/' . $product['image2'];
break;
case 'image3':
$data_cart['options']['image'] = base_url() . 'uploads/product/small/' . $product['image3'];
break;
case 'image4':
$data_cart['options']['image'] = base_url() . 'uploads/product/small/' . $product['image4'];
break;
case 'image5':
$data_cart['options']['image'] = base_url() . 'uploads/product/small/' . $product['image5'];
break;
}
if($greetingcard_text)
{
$data_cart['options']['greetingcard_text'] = $greetingcard_text;
//get greeting card price
$data_cart['options']['greetingcard_price'] = 'Rp ' . number_format($this->db->select('greetingcard_price')->from('configuration')->where('id_configuration', 1)->get()->row()->greetingcard_price, 0,",",".");
$data_cart['options']['greetingcard_price_int'] = $this->db->select('greetingcard_price')->from('configuration')->where('id_configuration', 1)->get()->row()->greetingcard_price;
}
$this->cart->product_name_rules = '[:print:]'; //this is to eliminate cart product name restriction on special characters
if($is_new_item)
{
$data_cart['cart_rowid'] = $this->cart->insert($data_cart);
}
else
{
$data_cart['rowid'] = $rowid;
$this->cart->update($data_cart);
$data_cart['cart_rowid'] = $rowid;
}
return $this->output
->set_content_type('application/json')
->set_status_header(201)
->set_output(json_encode([
'data_cart' => $data_cart,
]));
}
//add cart from product page custom
public function ajax_addtocart_customize()
{
//test if ajax call to prevent direct access
if(!$this->input->is_ajax_request())
{
exit('No direct script access allowed');
}
$chosen_color = trim(strtolower($this->input->post('chosen_color')));
$chosen_size = trim($this->input->post('chosen_size'));
$product_id = (int) $this->input->post('product_id');
$greetingcard_text = $this->input->post('greetingcard_text');
$chosenHeelsTypeId = $this->input->post('chosenHeelsTypeId');
$chosenHeelsHeightId = $this->input->post('chosenHeelsHeightId');
$chosenPlatformStyleId = $this->input->post('chosenPlatformStyleId');
$chosenPlatformHeightId = $this->input->post('chosenPlatformHeightId');
$isChosenInitialLetters = $this->input->post('isChosenInitialLetters');
$chosenInitialLetters = $this->input->post('chosenInitialLetters');
$chosenInitialLettersPrice = $this->input->post('chosenInitialLettersPrice');
$chosenInitialLettersImage = $this->input->post('chosenInitialLettersImage');
//check if this combination exist
$attributes = $chosen_size . ',' . $chosen_color;
$product_detail = $this->db->select('*')->from('product_details')->where('product_id', $product_id)->where('attributes', $attributes)->get()->row_array();
if(!$product_detail)
{
return $this->output
->set_content_type('application/json')
->set_status_header(404)
->set_output(json_encode([
'error' => 'Product not found',
]));
}
//get product data
$product = $this->db->select('title, image1, image2, image3, image4, image5')->from('products')->where('id_products', $product_detail['product_id'])->get()->row_array();
$data_cart['id'] = $product_id;
$data_cart['name'] = ucwords($product['title']);
$data_cart['qty'] = 1;
$data_cart['subtotals'] = 0;
if($product_detail['discounted_price'] > 0)
{
$data_cart['price'] = $product_detail['discounted_price'];
}
else
{
$data_cart['price'] = $product_detail['price'];
}
$data_cart['subtotals'] += $data_cart['price'];
$data_cart['options']['sku'] = $product_detail['sku'];
$data_cart['options']['color'] = ucwords($chosen_color);
$data_cart['options']['size'] = $chosen_size;
//get chosen image
switch ($product_detail['image']) {
case 'image1':
$data_cart['options']['image'] = base_url() . 'uploads/product/small/' . $product['image1'];
break;
case 'image2':
$data_cart['options']['image'] = base_url() . 'uploads/product/small/' . $product['image2'];
break;
case 'image3':
$data_cart['options']['image'] = base_url() . 'uploads/product/small/' . $product['image3'];
break;
case 'image4':
$data_cart['options']['image'] = base_url() . 'uploads/product/small/' . $product['image4'];
break;
case 'image5':
$data_cart['options']['image'] = base_url() . 'uploads/product/small/' . $product['image5'];
break;
}
if($greetingcard_text)
{
$data_cart['options']['greetingcard_text'] = $greetingcard_text;
//get greeting card price
$data_cart['options']['greetingcard_price'] = 'Rp ' . number_format($this->db->select('greetingcard_price')->from('configuration')->where('id_configuration', 1)->get()->row()->greetingcard_price, 0,",",".");
$data_cart['options']['greetingcard_price_int'] = $this->db->select('greetingcard_price')->from('configuration')->where('id_configuration', 1)->get()->row()->greetingcard_price;
$data_cart['subtotals'] += $data_cart['options']['greetingcard_price_int'];
}
if($chosenHeelsTypeId)
{
$chosenHeels = $this->db->select('product_attributes, additional_price')->from('product_attributes')->where('id_product_attributes', $chosenHeelsTypeId)->get()->row_array();
$data_cart['options']['chosenHeelsTypeId'] = $chosenHeelsTypeId;
$data_cart['options']['chosenHeelsType'] = $chosenHeels['product_attributes'];
$data_cart['options']['chosenHeelsTypePrice'] = $chosenHeels['additional_price'];
$data_cart['subtotals'] += $data_cart['options']['chosenHeelsTypePrice'];
}
if($chosenHeelsHeightId)
{
$chosenHeels = $this->db->select('product_attributes, additional_price')->from('product_attributes')->where('id_product_attributes', $chosenHeelsHeightId)->get()->row_array();
$data_cart['options']['chosenHeelsHeightId'] = $chosenHeelsHeightId;
$data_cart['options']['chosenHeelsHeight'] = $chosenHeels['product_attributes'];
$data_cart['options']['chosenHeelsHeightPrice'] = $chosenHeels['additional_price'];
$data_cart['subtotals'] += $data_cart['options']['chosenHeelsHeightPrice'];
}
if($chosenPlatformStyleId)
{
$chosenPlatform = $this->db->select('product_attributes, additional_price')->from('product_attributes')
->where('id_product_attributes', $chosenPlatformStyleId)->get()->row_array();
$data_cart['options']['chosenPlatformStyleId'] = $chosenPlatformStyleId;
$data_cart['options']['chosenPlatformStyle'] = $chosenPlatform['product_attributes'];
$data_cart['options']['chosenPlatformStylePrice'] = $chosenPlatform['additional_price'];
$data_cart['subtotals'] += $data_cart['options']['chosenPlatformStylePrice'];
}
if($chosenPlatformHeightId)
{
$chosenPlatform = $this->db->select('product_attributes, additional_price')->from('product_attributes')->where('id_product_attributes', $chosenPlatformHeightId)->get()->row_array();
$data_cart['options']['chosenPlatformHeightId'] = $chosenPlatformHeightId;
$data_cart['options']['chosenPlatformHeight'] = $chosenPlatform['product_attributes'];
$data_cart['options']['chosenPlatformHeightPrice'] = $chosenPlatform['additional_price'];
$data_cart['subtotals'] += $data_cart['options']['chosenPlatformHeightPrice'];
}
if($chosenInitialLetters)
{
$data_cart['options']['chosenInitialEngraved'] = $chosenInitialLetters;
$data_cart['options']['chosenInitialEngravedPrice'] = $chosenInitialLettersPrice;
$data_cart['options']['chosenInitialEngravedImage'] = $chosenInitialLettersImage;
$data_cart['subtotals'] += $data_cart['options']['chosenInitialEngravedPrice'];
}
$this->cart->product_name_rules = '[:print:]'; //this is to eliminate cart product name restriction on special characters
$data_cart['cart_rowid'] = $this->cart->insert($data_cart);
return $this->output
->set_content_type('application/json')
->set_status_header(201)
->set_output(json_encode([
'data_cart' => $data_cart,
]));
}
//remove cart from product page
public function ajax_removefromcart()
{
//test if ajax call to prevent direct access
if(!$this->input->is_ajax_request())
{
exit('No direct script access allowed');
}
$data = array(
'rowid' => $this->input->post('cart_rowid'),
'qty' => 0
);
$this->cart->update($data);
$data_cart['name'] = '';
$data_cart['qty'] = 0;
$data_cart['price'] = 0;
$data_cart['options']['color'] = '';
$data_cart['options']['size'] = '';
$data_cart['options']['image'] = '';
return $this->output
->set_content_type('application/json')
->set_status_header(201)
->set_output(json_encode([
'data_cart' => $data_cart,
]));
}
//cart add quantity from cart page
public function add_quantity($rowid)
{
$requested_qty = 0;
$stock_enough = TRUE;
foreach($this->cart->contents() as $item)
{
if($item['rowid'] == $rowid)
{
$requested_qty = $item['qty'] + 1;
//check if customize products, which has heels type
if(!isset($item['options']['chosenHeelsTypePrice']))
{
//regular product
//check current stock
$product_detail = $this->db->select('stock')->from('product_details')->where('product_id', $item['id'])->where('sku', $item['options']['sku'])->get()->row_array();
if($product_detail['stock'] < $requested_qty)
{
$stock_enough = FALSE;
}
break;
}
}
}
if(!$stock_enough)
{
$this->session->set_flashdata('message', "Stock not enough");
redirect(base_url('cart'));
}
$data_cart = array(
'rowid' => $rowid,
'qty' => $requested_qty
);
$this->cart->update($data_cart);
redirect(base_url('cart'));
}
//cart remove quantity from cart page
public function remove_quantity($rowid)
{
$requested_qty = 0;
$stock_enough = TRUE;
foreach($this->cart->contents() as $item)
{
if($item['rowid'] == $rowid)
{
$requested_qty = $item['qty'] - 1;
//check if customize products, which has heels type
if(!isset($item['options']['chosenHeelsTypePrice']))
{
//regular product
//check current stock
$product_detail = $this->db->select('stock')->from('product_details')->where('product_id', $item['id'])->where('sku', $item['options']['sku'])->get()->row_array();
if($product_detail['stock'] < $requested_qty)
{
$stock_enough = FALSE;
}
break;
}
}
}
if(!$stock_enough)
{
$this->session->set_flashdata('message', "Stock not enough");
redirect(base_url('cart'));
}
$data_cart = array(
'rowid' => $rowid,
'qty' => $requested_qty
);
$this->cart->update($data_cart);
redirect(base_url('cart'));
}
//cart add quantity from product page
public function ajax_addquantity()
{
//test if ajax call to prevent direct access
if(!$this->input->is_ajax_request())
{
exit('No direct script access allowed');
}
$requested_qty = 0;
foreach($this->cart->contents() as $item)
{
if($item['rowid'] == $this->input->post('cart_rowid'))
{
$requested_qty = $item['qty'] + 1;
break;
}
}
//check current stock
$product_detail = $this->db->select('stock')->from('product_details')->where('product_id', $item['id'])->where('sku', $item['options']['sku'])->get()->row_array();
if($product_detail['stock'] < $requested_qty)
{
return $this->output
->set_content_type('application/json')
->set_status_header(400)
->set_output(json_encode([
'error' => 'Stock not enough',
]));
}
$data_cart = array(
'rowid' => $this->input->post('cart_rowid'),
'qty' => $requested_qty
);
$this->cart->update($data_cart);
return $this->output
->set_content_type('application/json')
->set_status_header(201)
->set_output(json_encode([
'qty' => $data_cart['qty'],
]));
}
//cart add quantity from product page custom
public function ajax_addquantity_customize()
{
//test if ajax call to prevent direct access
if(!$this->input->is_ajax_request())
{
exit('No direct script access allowed');
}
$requested_qty = 0;
foreach($this->cart->contents() as $item)
{
if($item['rowid'] == $this->input->post('cart_rowid'))
{
$requested_qty = $item['qty'] + 1;
break;
}
}
$data_cart = array(
'rowid' => $this->input->post('cart_rowid'),
'qty' => $requested_qty
);
$this->cart->update($data_cart);
return $this->output
->set_content_type('application/json')
->set_status_header(201)
->set_output(json_encode([
'qty' => $data_cart['qty'],
]));
}
//cart add quantity from product page
public function ajax_removequantity()
{
//test if ajax call to prevent direct access
if(!$this->input->is_ajax_request())
{
exit('No direct script access allowed');
}
$current_qty = 0;
foreach($this->cart->contents() as $item)
{
if($item['rowid'] == $this->input->post('cart_rowid'))
{
$current_qty = $item['qty'];
break;
}
}
$data_cart = array(
'rowid' => $this->input->post('cart_rowid'),
'qty' => $current_qty - 1
);
$this->cart->update($data_cart);
if($current_qty == 0)
{
$data_cart['name'] = '';
$data_cart['qty'] = 0;
$data_cart['price'] = 0;
$data_cart['options']['color'] = '';
$data_cart['options']['size'] = '';
$data_cart['options']['image'] = '';
return $this->output
->set_content_type('application/json')
->set_status_header(201)
->set_output(json_encode([
'qty' => $data_cart['qty'],
'data_cart' => $data_cart,
]));
}
else
{
return $this->output
->set_content_type('application/json')
->set_status_header(201)
->set_output(json_encode([
'qty' => $data_cart['qty'],
]));
}
}
//cart add quantity from product page custom
public function ajax_removequantity_customize()
{
//test if ajax call to prevent direct access
if(!$this->input->is_ajax_request())
{
exit('No direct script access allowed');
}
$current_qty = 0;
foreach($this->cart->contents() as $item)
{
if($item['rowid'] == $this->input->post('cart_rowid'))
{
$current_qty = $item['qty'];
break;
}
}
$data_cart = array(
'rowid' => $this->input->post('cart_rowid'),
'qty' => $current_qty - 1
);
$this->cart->update($data_cart);
if($current_qty == 0)
{
$data_cart['name'] = '';
$data_cart['qty'] = 0;
$data_cart['price'] = 0;
$data_cart['options']['color'] = '';
$data_cart['options']['size'] = '';
$data_cart['options']['image'] = '';
return $this->output
->set_content_type('application/json')
->set_status_header(201)
->set_output(json_encode([
'qty' => $data_cart['qty'],
'data_cart' => $data_cart,
]));
}
else
{
return $this->output
->set_content_type('application/json')
->set_status_header(201)
->set_output(json_encode([
'qty' => $data_cart['qty'],
]));
}
}
//cart update purchase_notes
public function ajax_update_purchasenote()
{
//test if ajax call to prevent direct access
if(!$this->input->is_ajax_request())
{
exit('No direct script access allowed');
}
$purchase_notes = $this->security->xss_clean($this->input->post('purchase_notes'));
$this->session->set_userdata('purchase_notes', $purchase_notes);
return $this->output
->set_content_type('application/json')
->set_status_header(201)
->set_output(json_encode([
'purchase_notes' => $this->session->userdata('purchase_notes'),
]));
}
}