Build Shopping Cart with CodeIgniter, Ajax and MySQL
In online marketing, a shopping cart is a piece of software that facilitates the purchase of products. The Cart Class permits items to be added to a session that stays active while a user is browsing your site. These items can be retrieved and displayed in a standard “shopping cart” format, allowing the user to update the quantity or remove items from the cart.In this tutorial, you will learn how to create a shopping cart with CodeIgniter, Ajax, and MySQL.You can also download the source code of live to build your codeIgniter shopping cart from scratch.
Step 1: Create MySQL Database and Table
The following SQL creates a product table in the MySQL database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php CREATE TABLE `product` ( `product_id` int(11) NOT NULL, `model` varchar(64) NOT NULL, `sku` varchar(64) NOT NULL, `quantity` int(4) NOT NULL DEFAULT '0', `image` varchar(255) DEFAULT NULL, `price` decimal(15,2) NOT NULL DEFAULT '0.00', `date_available` date NOT NULL DEFAULT '0000-00-00', `sort_order` int(11) NOT NULL DEFAULT '0', `status` tinyint(1) NOT NULL DEFAULT '0', `date_added` varchar(12) NOT NULL, `date_modified` varchar(12) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `product` ADD PRIMARY KEY (`product_id`); ALTER TABLE `product` MODIFY `product_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1; ?> |
The following SQL creates a product_description table in the MySQL database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php CREATE TABLE `product_description` ( `product_id` int(11) NOT NULL, `language_id` int(11) NOT NULL, `slug` varchar(255) DEFAULT NULL, `name` varchar(255) NOT NULL, `description` text NOT NULL, `tag` text NOT NULL, `meta_title` varchar(255) NOT NULL, `meta_description` varchar(255) NOT NULL, `meta_keyword` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `product_description` ADD PRIMARY KEY (`product_id`,`language_id`), ADD KEY `name` (`name`); ?> |
The following SQL creates a product_image table in the MySQL database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php CREATE TABLE `product_image` ( `id` int(11) NOT NULL, `product_id` int(11) NOT NULL, `image` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ALTER TABLE `product_image` ADD PRIMARY KEY (`id`); ALTER TABLE `product_image` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1; ?> |
The following SQL creates a orders table in the MySQL database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php CREATE TABLE `orders` ( `order_id` int(11) NOT NULL, `invoice_no` varchar(16) DEFAULT NULL, `invoice_prefix` varchar(26) NOT NULL, `customer_id` int(11) NOT NULL DEFAULT '0', `firstname` varchar(32) NOT NULL, `lastname` varchar(32) NOT NULL, `email` varchar(96) NOT NULL, `phone` varchar(32) NOT NULL, `payment_firstname` varchar(32) NOT NULL, `payment_lastname` varchar(32) NOT NULL, `payment_company` varchar(60) NOT NULL, `payment_address` varchar(128) NOT NULL, `payment_city` varchar(128) NOT NULL, `payment_postcode` varchar(10) NOT NULL, `payment_country` varchar(128) NOT NULL, `payment_state` varchar(100) NOT NULL, `payment_method` varchar(128) NOT NULL, `payment_code` varchar(128) NOT NULL, `comment` text NOT NULL, `total` decimal(15,4) NOT NULL DEFAULT '0.0000', `order_status_id` int(11) NOT NULL DEFAULT '0', `currency_id` int(11) NOT NULL, `currency_code` varchar(3) NOT NULL, `currency_value` decimal(15,8) NOT NULL DEFAULT '1.00000000', `date_added` varchar(12) NOT NULL, `date_modified` varchar(12) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `orders` ADD PRIMARY KEY (`order_id`); ALTER TABLE `orders` MODIFY `order_id` int(11) NOT NULL AUTO_INCREMENT; ?> |
The following SQL creates a orders_product table in the MySQL database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php CREATE TABLE `orders_product` ( `order_product_id` int(11) NOT NULL, `order_id` int(11) NOT NULL, `product_id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `model` varchar(64) NOT NULL, `quantity` int(4) NOT NULL, `price` decimal(15,4) NOT NULL DEFAULT '0.0000', `total` decimal(15,4) NOT NULL DEFAULT '0.0000' ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `orders_product` ADD PRIMARY KEY (`order_product_id`), ADD KEY `order_id` (`order_id`); ALTER TABLE `orders_product` MODIFY `order_product_id` int(11) NOT NULL AUTO_INCREMENT; ?> |
The following SQL creates a customer table in the MySQL database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php CREATE TABLE `customer` ( `customer_id` int(11) NOT NULL, `firstname` varchar(32) NOT NULL, `lastname` varchar(32) NOT NULL, `email` varchar(96) NOT NULL, `phone` varchar(32) NOT NULL, `password` varchar(40) DEFAULT NULL, `salt` varchar(9) DEFAULT NULL, `status` tinyint(1) NOT NULL, `date_added` varchar(12) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `customer` ADD PRIMARY KEY (`customer_id`); ALTER TABLE `customer` MODIFY `customer_id` int(11) NOT NULL AUTO_INCREMENT; ?> |
Step 2: Create a model file
Create a model file named “Product_model.php” inside “application/models” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
<?php /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * Description of Product Model: CodeIgniter * * @author TechArise Team * * @email info@techarise.com */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class Product_model extends CI_Model { private $_productID; private $_productName; private $_model; private $_price; private $_qty; private $_subTotal; private $_slug; private $_status; private $_limit; private $_pageNumber; private $_offset; private $_orderID; private $_invoiceNo; private $_invoicePrefix; private $_customerID; private $_firstName; private $_lastName; private $_email; private $_phone; private $_paymentFirstName; private $_paymentLastName; private $_paymentCompany; private $_paymentAddress; private $_paymentCity; private $_paymentPostCode; private $_paymentCountry; private $_paymentState; private $_paymentMethod; private $_paymentCode; private $_comment; private $_total; private $_orderStatusID; private $_currencyID; private $_currencyCode; private $_currencyValue; private $_timeStamp; private $_batchData; public function setProductID($productID) { $this->_productID = $productID; } public function setProductName($productName) { $this->_productName = $productName; } public function setModel($model) { $this->_model = $model; } public function setPrice($price) { $this->_price = $price; } public function setQty($qty) { $this->_qty = $qty; } public function setSubTotal($subTotal) { $this->_subTotal = $subTotal; } public function setSlug($slug) { $this->_slug = $slug; } public function setStatus($status) { $this->_status = $status; } public function setLimit($limit) { $this->_limit = $limit; } public function setPageNumber($pageNumber) { $this->_pageNumber = $pageNumber; } public function setOffset($offset) { $this->_offset = $offset; } public function setOrderID($orderID) { $this->_orderID = $orderID; } public function setInvoiceNo($invoiceNo) { $this->_invoiceNo = $invoiceNo; } public function setInvoicePrefix($invoicePrefix) { $this->_invoicePrefix = $invoicePrefix; } public function setCustomerID($customerID) { $this->_customerID = $customerID; } public function setFirstName($firstName) { $this->_firstName = $firstName; } public function setLastName($lastName) { $this->_lastName = $lastName; } public function setEmail($email) { $this->_email = $email; } public function setPhone($phone) { $this->_phone = $phone; } public function setPaymentFirstName($paymentFirstName) { $this->_paymentFirstName = $paymentFirstName; } public function setPaymentLastName($paymentLastName) { $this->_paymentLastName = $paymentLastName; } public function setPaymentCompany($paymentCompany) { $this->_paymentCompany = $paymentCompany; } public function setPaymentAddress($paymentAddress) { $this->_paymentAddress = $paymentAddress; } public function setPaymentCity($paymentCity) { $this->_paymentCity = $paymentCity; } public function setPaymentPostCode($paymentPostCode) { $this->_paymentPostCode = $paymentPostCode; } public function setPaymentCountry($paymentCountry) { $this->_paymentCountry = $paymentCountry; } public function setPaymentState($paymentState) { $this->_paymentState = $paymentState; } public function setPaymentMethod($paymentMethod) { $this->_paymentMethod = $paymentMethod; } public function setPaymentCode($paymentCode) { $this->_paymentCode = $paymentCode; } public function setComment($comment) { $this->_comment = $comment; } public function setTotal($total) { $this->_total = $total; } public function setOrderStatusID($orderStatusID) { $this->_orderStatusID = $orderStatusID; } public function setCurrencyID($currencyID) { $this->_currencyID = $currencyID; } public function setCurrencyCode($currencyCode) { $this->_currencyCode = $currencyCode; } public function setCurrencyValue($currencyValue) { $this->_currencyValue = $currencyValue; } public function setTimeStamp($timeStamp) { $this->_timeStamp = $timeStamp; } public function setBatchData($batchData) { $this->_batchData = $batchData; } // count all product public function getAllProductCount() { $this->db->where('status', $this->_status); $this->db->from('product'); return $this->db->count_all_results(); } // Get all details ehich store in "products" table in database. public function getProductList() { $this->db->select(array('p.product_id', 'pd.name', 'pd.slug', 'p.sku', 'p.price', 'p.model', 'pd.description', 'p.image')); $this->db->from('product as p'); $this->db->join('product_description as pd', 'pd.product_id = p.product_id', 'left'); $this->db->where('p.status', $this->_status); $this->db->limit($this->_pageNumber, $this->_offset); $query = $this->db->get(); return $query->result_array(); } // get currency code public function getCurrencyFormat() { $this->db->select(array('c.currency_id', 'c.symbol_left', 'c.symbol_right', 'c.code', 'c.value', 'c.decimal_place')); $this->db->from('currency as c'); $this->db->where('c.currency_id', $this->_currencyID); $query = $this->db->get(); return $query->row_array(); } // get resource centre items public function getProduct() { $this->db->select(array('p.product_id', 'pd.name', 'p.sku', 'p.price', 'p.model', 'pd.slug', 'pd.description', 'p.image')); $this->db->from('product as p'); $this->db->join('product_description as pd', 'pd.product_id = p.product_id', 'left'); if(!empty($this->_productID)) { $this->db->where('p.product_id', $this->_productID); } if(!empty($this->_slug)) { $this->db->where('pd.slug', $this->_slug); } $query = $this->db->get(); return $query->row_array(); } public function getProductImage() { $this->db->select(array('m.id', 'm.product_id', 'm.image')); $this->db->from('product_image as m'); $this->db->join('product as p', 'm.product_id = p.product_id', 'left'); $this->db->where('p.product_id', $this->_productID); $query = $this->db->get(); return $query->result_array(); } // order now public function createOrder() { $data = array( 'invoice_no' => $this->_invoiceNo, 'invoice_prefix' => $this->_invoicePrefix, 'customer_id' => $this->_customerID, 'firstname' => $this->_firstName, 'lastname' => $this->_lastName, 'email' => $this->_email, 'phone' => $this->_phone, 'payment_firstname' => $this->_paymentFirstName, 'payment_lastname' => $this->_paymentLastName, 'payment_company' => $this->_paymentCompany, 'payment_address' => $this->_paymentAddress, 'payment_city' => $this->_paymentCity, 'payment_postcode' => $this->_paymentPostCode, 'payment_country' => $this->_paymentCountry, 'payment_state' => $this->_paymentState, 'payment_method' => $this->_paymentMethod, 'payment_code' => $this->_paymentCode, 'comment' => $this->_comment, 'total' => $this->_total, 'order_status_id' => $this->_orderStatusID, 'currency_id' => $this->_currencyID, 'currency_code' => $this->_currencyCode, 'currency_value' => $this->_currencyValue, 'date_added' => $this->_timeStamp, 'date_modified' => $this->_timeStamp, ); $this->db->insert('orders', $data); return $this->db->insert_id(); } // count Invoice public function countInvoice() { $this->db->from('orders'); return $this->db->count_all_results(); } // add order product item public function addOrderItem() { $data = $this->_batchData; $this->db->insert_batch('orders_product', $data); } // create customer public function createCustomer() { $data = array( 'firstname' => $this->_firstName, 'lastname' => $this->_lastName, 'email' => $this->_email, 'phone' => $this->_phone, 'status' => 1, 'date_added' => $this->_timeStamp, ); $this->db->insert('customer', $data); return $this->db->insert_id(); } // email validation public function validateEmail($email) { return preg_match('/^[^\@]+@.*.[a-z]{2,15}$/i', $email)?TRUE:FALSE; } // mobile validation public function validateMobile($mobile) { return preg_match('/^[0-9]{10}+$/', $mobile)?TRUE:FALSE; } } ?> |
Step 3: Create a controller file
Next create a controller file named “Product.php” inside “application/controllers” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
<?php /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * @package Product : CodeIgniter * * @author TechArise Team * * @email info@techarise.com * * Description of Product Controller */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class Product extends CI_Controller { public function __construct() { parent::__construct(); //load model $this->load->model('Product_model', 'basket'); $this->load->library('pagination'); $this->load->library('cart'); $this->load->helper('text'); } // list product public function index() { $data = array(); $data['page'] = 'product-list'; $data['title'] = 'Product | TechArise'; $data['breadcrumbs'] = array('Home' => '#'); $this->basket->setCurrencyID(5); $currencyInfo = $this->basket->getCurrencyFormat(); $data['currency'] = $currencyInfo; $config['total_rows'] = $this->basket->getAllProductCount(); $page_number = $this->uri->segment(3); $config['base_url'] = base_url() . 'cart/index/'; if (empty($page_number)) $page_number = 1; $offset = ($page_number - 1) * $this->pagination->per_page; $this->basket->setPageNumber($this->pagination->per_page); $this->basket->setOffset($offset); $this->pagination->cur_page = $offset; $this->pagination->initialize($config); $data['page_links'] = $this->pagination->create_links(); $this->basket->setStatus(1); $data['products'] = $this->basket->getProductList(); $this->load->view('product/index', $data); } // product description public function single($slug='') { $data = array(); $data['page'] = 'product-view'; $data['title'] = 'View Product | TechArise'; $data['breadcrumbs'] = array('Home' => site_url(), 'View' => '#'); $this->basket->setSlug($slug); $data['productInfo'] = $this->basket->getProduct(); $this->basket->setProductID($data['productInfo']['product_id']); $data['productImage'] = $this->basket->getProductImage(); $this->load->view('product/single', $data); } // quickView public function quickView() { $json = array(); $productID = $this->input->post('product_id'); $this->basket->setProductID($productID); $json['productInfo'] = $this->basket->getProduct(); $this->output->set_header('Content-Type: application/json'); $this->load->view('product/render/view', $json); } } ?> |
Step 4: Create a controller file
Next create a controller file named “Cart.php” inside “application/controllers” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
<?php /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * @package Cart : CodeIgniter * * @author TechArise Team * * @email info@techarise.com * * Description of Cart Controller */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class Cart extends CI_Controller { public function __construct() { parent::__construct(); //load model $this->load->model('Product_model', 'basket'); $this->load->library('pagination'); $this->load->library('cart'); $this->load->helper('text'); } public function index() { $data = array(); $data['page'] = 'shopping-cart'; $data['title'] = 'My Cart | TechArise'; $data['breadcrumbs'] = array('Home' => site_url(), 'Cart' => '#'); $data['productInfo'] = $this->cart->contents(); $this->load->view('cart/index', $data); } public function single() { $data = array(); $data['page'] = 'shopping-cart'; $data['title'] = 'Shopping Cart | TechArise'; $data['breadcrumbs'] = array('Shopping Cart' => '#', 'List' => '#'); $this->load->view('cart/single', $data); } // product add to basket function add() { $json = array(); if (!empty($this->input->post('productID'))) { $this->basket->setProductID($this->input->post('productID')); $qty = $this->input->post('qty'); $productInfo = $this->basket->getProduct(); $cartData = array( 'id' => $productInfo['product_id'], 'name' => $productInfo['name'], 'model' => $productInfo['model'], 'price' => $productInfo['price'], 'slug' => $productInfo['slug'], 'qty' => $qty, 'image' => $productInfo['image'], ); $this->cart->insert($cartData); $json['status'] = 1; $json['counter'] = count($this->cart->contents()); } else { $json['status'] = 0; } header('Content-Type: application/json'); echo json_encode($json); } // remove cart item function remove() { $json = array(); if (!empty($this->input->post('productID'))) { $rowid = $this->input->post('productID'); $data = array( 'rowid' => $rowid, 'qty' => 0 ); $this->cart->update($data); } header('Content-Type: application/json'); echo json_encode($json); } // update cart item function update() { $json = array(); if (!empty($this->input->post('productID'))) { $rowid = $this->input->post('productID'); $qty = $this->input->post('qty'); $data = array( 'rowid' => $rowid, 'qty' => $qty ); $this->cart->update($data); } header('Content-Type: application/json'); echo json_encode($json); } // checkout item function checkout() { $data = array(); $data['metaDescription'] = 'Shopping Cart'; $data['metaKeywords'] = 'Shopping, Cart'; $data['title'] = "Shopping Cart - TechArise"; $data['breadcrumbs'] = array('Home' => site_url(), 'Checkout' => '#'); $data['productInfo'] = $this->cart->contents(); $this->load->view('cart/checkout', $data); } // order now public function orderNow() { $productInfo = $this->cart->contents(); $grandTotal = 0; $productList = array(); foreach ($productInfo as $key => $element) { $grandTotal += $element['subtotal']; $productList[] = array( 'id' => $element['id'], 'name' => $element['name'], 'model' => $element['model'], 'price' => $element['price'], 'qty' => $element['qty'], 'subtotal' => $element['subtotal'], ); } $customerID = 0; $firstname = $this->input->post('firstname'); $lastname = $this->input->post('lastname'); $email = $this->input->post('email'); $phone = $this->input->post('phone'); $company = $this->input->post('company'); $address = $this->input->post('address'); $country = $this->input->post('country'); $state = $this->input->post('state'); $city = $this->input->post('city'); $zipcode = $this->input->post('zipcode'); $timeStamp = time(); // firstname validation if(empty(trim($firstname))){ $json['error']['firstname'] = 'Please enter first name'; } // firstname validation if(empty(trim($lastname))){ $json['error']['lastname'] = 'Please enter last name'; } // email validation if(empty(trim($email))){ $json['error']['email'] = 'Please enter email address'; } // check email validation if ($this->basket->validateEmail($email) == FALSE) { $json['error']['email'] = 'Please enter valid email address'; } // check conatct no validation if($this->basket->validateMobile($phone) == FALSE) { $json['error']['phone'] = 'Please enter valid contact no. format: 9000000001'; } // company validation if(empty($company)){ $json['error']['company'] = 'Please enter company'; } if(empty($address)){ $json['error']['address'] = 'Please enter address name'; } if(empty($address)){ $json['error']['address'] = 'Please enter address'; } if(empty($country)){ $json['error']['country'] = 'Please enter country'; } if(empty($state)){ $json['error']['state'] = 'Please enter state'; } if(empty($city)){ $json['error']['city'] = 'Please enter city'; } if(empty($zipcode)){ $json['error']['zipcode'] = 'Please enter zipcode'; } if(empty($json['error'])){ $this->basket->setFirstName($firstname); $this->basket->setLastName($lastname); $this->basket->setEmail($email); $this->basket->setPhone($phone); $this->basket->setTimeStamp($timeStamp); // create customer $customerID = $this->basket->createCustomer(); $this->basket->setCustomerID($customerID); $countInvoice = $this->basket->countInvoice(); $fullInvoice = 'INV-' . str_pad($countInvoice + 1, 4, '0', STR_PAD_LEFT); $this->basket->setInvoiceNo($fullInvoice); $this->basket->setInvoicePrefix('INV'); $this->basket->setPaymentFirstName($firstname); $this->basket->setPaymentLastName($lastname); $this->basket->setPaymentCompany($company); $this->basket->setPaymentAddress($address); $this->basket->setPaymentCity($city); $this->basket->setPaymentPostCode($zipcode); $this->basket->setPaymentCountry($country); $this->basket->setPaymentState($state); $this->basket->setPaymentMethod('COD'); $this->basket->setPaymentCode('COD'); $this->basket->setComment('note'); $this->basket->setTotal($grandTotal); $this->basket->setOrderStatusID(1); $this->basket->setCurrencyID(1); $this->basket->setCurrencyCode('USD'); $this->basket->setCurrencyValue('0.000000000'); try { $last_id = $this->basket->createOrder(); } catch (Exception $e) { var_dump($e->getMessage()); } if(!empty($last_id)) { foreach ($productList as $key => $element) { $batch[] = array( 'order_id' => $last_id, 'product_id' => $element['id'], 'model' => $element['model'], 'quantity' => $element['qty'], 'price' => $element['price'], 'total' => $element['subtotal'], ); } $this->basket->setBatchData($batch); $this->basket->addOrderItem(); $this->session->unset_userdata('cart_contents'); } if (!empty($last_id) && $last_id > 0) { $orderID = str_pad($last_id, 4, '0', STR_PAD_LEFT); $json['order_id'] = $orderID; $json['status'] = 'success'; } } $this->output->set_header('Content-Type: application/json'); echo json_encode($json); } // checkout item function success() { $data = array(); $data['metaDescription'] = 'Shopping Cart'; $data['metaKeywords'] = 'Shopping, Cart'; $data['title'] = "Shopping Cart - TechArise"; $data['breadcrumbs'] = array('Home' => site_url(), 'Success' => '#'); $order_id = $this->input->get('order_id'); $data['order_id'] = $order_id; $this->load->view('cart/success', $data); } } ?> |