Step 1: Create MySQL Database and Table
The following SQL creates a product table in the MySQL database.
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.
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.
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.
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.
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.
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.
/*
* 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.
/*
* 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.
/*
* 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);
}
}
?>
Demo [sociallocker] Download[/sociallocker]