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/mesinpolesshinemate.com/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() { /* echo '<pre>'; print_r($this->cart->contents()); echo '</pre>'; */ //get SEO $this->db->select('website_name, meta_keywords')->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'; $this->data_header['meta_keywords'] = $website_name->meta_keywords; 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'); } $this->load->view('template/header', $this->data_header); $this->load->view('cart', $data); $this->load->view('template/footer', $this->data_footer); } public function add() { //check if there is post request, if not, reject & redirect if (empty($_POST)) { redirect('cart'); } //validation check in action $config = array( array( 'field' => 'product_size', 'label' => 'product Size', 'rules' => 'trim|required|callback_cek_stock' ) ); $this->form_validation->set_rules($config); $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); if($this->form_validation->run($this) == TRUE) { $id_product_details = (int) $this->input->post('product_size'); //get product details $this->db->select('*')->from('product_details')->where('id_product_details', $id_product_details); $product_details = $this->db->get()->row(); $data['id'] = (int) $this->input->post('product_id'); //get attributes $attributes_array = explode(' ', trim($product_details->attributes)); $count_attributes = count($attributes_array); $attributes = ''; for($i = 0; $i<$count_attributes; $i++) { //get product size label $this->db->select('product_size.product_size as product_size')->from('product_size')->join('product_attributes', 'product_attributes.id_product_size = product_size.id_product_size')->where('product_attributes.product_attributes', $attributes_array[$i]); $product_size = $this->db->get()->row()->product_size; $attributes = $attributes . ucwords($product_size) . ' ' . ucwords($attributes_array[$i]) . '. '; } $data['name'] = '<strong>' . $this->input->post('product_name') . '</strong>' . '<br>Option: ' . $attributes . '<br>Product Code: ' . $product_details->sku; $data['qty'] = (int) $this->input->post('qty'); $data['price'] = (int) $this->input->post('price'); $data['options']['size'] = $attributes; $data['options']['sku'] = $product_details->sku; $this->cart->product_name_rules = '[:print:]'; //this is to eliminate cart product name restriction on special characters $this->cart->insert($data); redirect('cart'); } else { //get product alias $this->db->select('alias'); $this->db->from('products'); $this->db->where('id_products', (int) $this->input->post('product_id')); $query = $this->db->get(); $product_alias = $query->row(); $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(base_url() . 'product/' . $product_alias->alias); } } 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_array'] ; /* echo '<pre>'; print_r($cart_info); echo '</pre>'; exit(); */ foreach( $cart_info as $sku => $cart) { //get product detail id $this->db->select('id_product_details')->from('product_details')->where('sku', $sku); $id_product_detail = $this->db->get()->row()->id_product_details; //check the minimum purchase qty required if(isset($this->session->userdata('customer')['customer_id'])) { //customer is logged in //check if customer is a reseller. if reseller use reseller min quantity $this->db->select('reseller_id')->from('customers')->where('id_customers', $this->session->userdata('customer')['customer_id']); $reseller_id = $this->db->get()->row()->reseller_id; //check if reseller price already available (already input by admin) $this->db->select('price')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail); $count_reseller = $this->db->get()->num_rows(); if($reseller_id != NULL && $count_reseller > 0) { //customer is reseller, and data already inputtedby admin. so use reseller min quantity $this->db->select('min_quantity')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail); $min_quantity = $this->db->get()->row()->min_quantity; } elseif($reseller_id == NULL) { $min_quantity = 1; } elseif($reseller_id != NULL && $count_reseller == 0) { //customer is a reseller, but data not input yet, or customer choose empty option.. //then give default reseller min quantity //get reseller min quantity $this->db->select('min_quantity')->from('resellers_price')->where('reseller_id', $reseller_id)->where('product_detail_id', $id_product_detail); $min_quantity = $this->db->get()->row()->min_quantity; } if($cart['qty'] < $min_quantity) { //cart quantity is less than minimum quantity $this->session->set_flashdata('no_stock', "<br> <p style='background:grey; color:white; padding:5px; font-weight:bold;'>Sorry minimum quantity is {$min_quantity} pcs. Please choose higher quantity.</p>"); redirect('cart'); } } //check the available stock for current SKU $this->db->select('stock')->from('product_details')->where('sku', $sku); $available_stock = (int) $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; } } }