Codeigniter shopping cart v1.1 Part 7: orders module

Home of Orders module shows Order ID, First name, Last name, Email, Total, Telephone, Order Date, Delivery Date, Payment Date, City and Actions.

Download Page

Manage Orders Page Overview

We use jquery dataTables to show our information. This will allow us to sort by each column, display different number of rows and seach.

Actions

There are paid, delivered and details links under Actions. When you click ‘paid’, ‘Payment Date’ will be updated. And ‘delivered’ link will update ‘Delivery Date’.
‘details’ link takes you to Order details page.

Order Details Page Overview


‘details’ link in the order home takes you to the details of the order.
There are Order ID, Product Name, Quantity, Price and Action. And under the action field you can find a delete button.

Database

CREATE TABLE IF NOT EXISTS `omc_order` (
`order_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`customer_id` int(10) unsigned NOT NULL,
`total` decimal(10,2) NOT NULL,
`order_date` datetime NOT NULL,
`delivery_date` datetime NOT NULL,
`payment_date` datetime NOT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Module structure

You can see the structure in this image. Please click to enlarge the image.

admin controller

Please read the comments in the following code. modules/orders/controllers/admin.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends Admin_Controller {
  	function Admin(){
   	parent::Admin_Controller();
	// Check for access permission
		check('Orders');
		// load category model from module categories
		$this->load->module_model('categories','MCats');
		$this->load->module_model('products','MProducts');
		// load orders model
		$this->load->model('MOrders');
		// Set breadcrumb
		$this->bep_site->set_crumb($this->lang->line('backendpro_orders'),'orders/admin');	

	}

  	function index(){

		$data['title'] = "Manage Orders";
		//$data['main'] = 'admin_orders_home';
		$data['products'] = $this->MProducts->getAllProducts();
		$data['categories'] = $this->MCats->getCategoriesDropDown();
		$data['orders'] = $this->MOrders->getAllOrders();
		$data['header'] = $this->lang->line('backendpro_access_control');
		$data['page'] = $this->config->item('backendpro_template_admin') . "admin_orders_home";
		$data['module'] = 'orders';
		$this->load->view($this->_container,$data);

  	}

  	function details($id){

		$data['title'] = "Order Details";
		//$data['main'] = 'admin_orders_details';
		$data['products'] = $this->MProducts->getAllProducts();
		$data['categories'] = $this->MCats->getCategoriesDropDown();
		$data['orderdetails'] = $this->MOrders->getOrderDetails($id);
		// Set breadcrumb
		$this->bep_site->set_crumb($this->lang->line('userlib_order_details'),'orders/admin/details');
		$data['header'] = $this->lang->line('backendpro_access_control');
		$data['page'] = $this->config->item('backendpro_template_admin') . "admin_orders_details";
		$data['module'] = 'orders';
		$this->load->view($this->_container,$data);
  	}

  	function paid($id){
		$this->MOrders->setpayment($id);
		$this->session->set_flashdata('message', 'Payment Date updated!');
		redirect('orders/admin');
   	}

  	function delivered($id){
		$this->MOrders->setdelivery($id);
		$this->session->set_flashdata('message', 'Delivery Date updated!');
		redirect('orders/admin/');
   	}	

  	function deleteitem($order_id, $order_item_id){
		$order_id = $this->uri->segment(4);
		$order_item_id = $this->uri->segment(5);

		if (count($this->MOrders->findsiblings($order_id)) < 2){
			$this->MOrders->deleteOrder($order_id);
			$this->MOrders->deleteOrderItem($order_item_id);
			$this->session->set_flashdata('message','Order deleted');
			redirect('orders/admin/index','refresh');
		}else{
		    $this->MOrders->deleteOrderItem($order_item_id);
			$this->session->set_flashdata('message','Order item deleted');
			redirect('orders/admin/details/'.$order_id,'refresh');
		}
	}

}

?>

morders model

<?php

class MOrders extends Model{
	 function  __construct(){
	    parent::Model();
	 }

	 function getAllOrders(){
		$this->db->from('omc_order');
		$this->db->join('omc_customer', 'omc_order.customer_id = omc_customer.customer_id');
		$Q = $this->db->get();
		if ($Q->num_rows() > 0){
			foreach ($Q->result_array() as $row){
			$data[] = $row;
		}
		$Q->free_result();
		return $data;
		}
	 }

	 function getOrders(){
		 $Q = $this->db->get('omc_order');
		 return $Q;
	 }

	 function ordersToComplete(){
	 	$Q = $this->db->get_where('omc_order', array('delivery_date' => 0));
	  	return $Q;

	 }

	function getOrderDetails($id){
		 $this->db->select('omc_order_item.order_item_id,omc_order_item.order_id,omc_order_item.product_id,
						   omc_order_item.quantity,omc_order_item.price,omc_product.name,
						   omc_order.order_date, omc_order.delivery_date, omc_order.payment_date');
		 $this->db->from('omc_order_item');
		 $this->db->join('omc_product', 'omc_product.id = omc_order_item.product_id');
		 $this->db->join('omc_order', 'omc_order.order_id = omc_order_item.order_id');
		 $this->db->where('omc_order_item.order_id', $id);
		 $Q = $this->db->get();

		 if ($Q->num_rows() > 0){
		       foreach ($Q->result_array() as $row){
		         $data[] = $row;
		       }
		 }
		 $Q->free_result();
		 return $data; 

	}

	function updateCart($productid,$fullproduct){
		$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();
		$productid = id_clean($productid);
		$totalprice = 0;
		if (count($fullproduct)){
			if (isset($cart[$productid])){
				$prevct = $cart[$productid]['count'];
				$prevname = $cart[$productid]['name'];
				$prevprice = $cart[$productid]['price'];
				$cart[$productid] = array(
						'name' => $prevname,
						'price' => $prevprice,
						'count' => $prevct + 1
						);
			}else{
				$cart[$productid] = array(
						'name' => $fullproduct['name'],
						// 'price' => $this->format_currency($fullproduct['price']),
						// This should be done in view
						'price' => $fullproduct['price'],
						'count' => 1
						);
			}
				foreach ($cart as $id => $product){
					   	$totalprice += $product['price'] * $product['count'];
				} 

			// This format should be done later in a view otherwise it will mess up.
			$_SESSION['totalprice'] = $totalprice;
			$_SESSION['cart'] = $cart;
			$msg = lang('orders_added_cart');
			$this->session->set_flashdata('conf_msg', $msg);
		}
	}

	function removeLineItem($id){
		$id = id_clean($id);
		$totalprice = 0;
		$cart = $_SESSION['cart'];//$this->session->userdata('cart');
		if (isset($cart[$id])){
			unset($cart[$id]);
			foreach ($cart as $id => $product){
				$totalprice += $product['price'] * $product['count'];
			}
			// this should be done later
			$_SESSION['totalprice'] = $totalprice;
			$_SESSION['cart'] = $cart;
			$msg = lang('orders_product_removed');
			echo $msg;
		}else{
			 $msg = lang('orders_not_in_cart');
			echo "Product not in cart!";
		}
	}

	function updateCartAjax($idlist){
		$cart = $_SESSION['cart'];//$this->session->userdata('cart');
		//split idlist on comma first
		$records = explode(',',$idlist);
		$updated = 0;
		$totalprice = $_SESSION['totalprice'];
		if (count($records)){
			foreach ($records as $record){
				if (strlen($record)){
					//split each record on colon
					$fields = explode(":",$record);
					$id = id_clean($fields[0]);
					$ct = $fields[1];

					if ($ct > 0 && $ct != $cart[$id]['count']){
						$cart[$id]['count'] = $ct;
						$updated++;
					}elseif ($ct == 0){
						unset($cart[$id]);
						$updated++;
					}
				}
			}
			if ($updated){
				$totalprice=0;
				$shippingprice = 0;
					foreach ($cart as $id => $product){
					   	$totalprice += $product['price'] * $product['count'];
					 	$maxprice = 0;
						foreach ($_SESSION['cart'] as $item) {
						    if ($item['price'] > $maxprice) {
						        $maxprice = $item['price'];
						    }
						}
					   if ($maxprice > 268 ){
					       $shippingprice = 65.0;
					   }else{
					   		$shippingprice = 25.0;
					   }
					}
				$_SESSION['shipping'] = $shippingprice;
				$_SESSION['totalprice'] = $totalprice;
				$_SESSION['cart'] = $cart;
				switch ($updated){
					case 0:
					$string = lang('orders_no_records');
					break;

					case 1:
					$string = $updated . " " . lang('orders_record');
					break;

					default:
					$string = $updated . " " . lang('orders_records');
					break;
				}
				echo $string . $updated . " " . lang('orders_updated');
			}else{
				echo lang('orders_no_changes_detected');
			}
		}else{
			echo lang('orders_nothing_to_update');
		}
	}

	function verifyCart(){
		$cart = $_SESSION['cart'];
		$change = false;

		if (count($cart)){
			foreach ($cart as $id => $details){
				$idlist[] = $id;
			}
			$ids = implode(",",$idlist);
			$this->db->select('id,price');
			$this->db->where("id in ($ids)");
			$Q = $this->db->get('omc_product');
	    	if ($Q->num_rows() > 0){
				foreach ($Q->result_array() as $row){
					$db[$row['id']] = $row['price'];
				}
			}
			foreach ($cart as $id => $details){
				if (isset($db[$id])){
					if ($details['price'] != $db[$id]){
						$details['price'] = $db[$id];
						$change = true;
					}
					$final[$id] = $details;
				}else{
					$change = true;
				}
			}
			$totalprice=0;
			foreach ($final as $id => $product){
				$totalprice += $product['price'] * $product['count'];
			}
			$_SESSION['totalprice'] = $totalprice;
			$_SESSION['cart'] = $final;
			$this->session->set_flashdata('change',$change);
		}else{
			//nothing in cart!
			$this->session->set_flashdata('error',lang('orders_nothing_in_cart'));
		}
	}

	 function format_currency($number){
		 return number_format($number,2,'.',',');
	 }

	 function enterorder($totalprice){

		  $data = array (
			  'customer_last_name' => db_clean($this->input->post('customer_last_name')),
			  'customer_first_name' => db_clean($this->input->post('customer_first_name')),
			  'phone_number' => db_clean($this->input->post('telephone')),
			  'email' => db_clean($this->input->post('email')),
			  'address' => db_clean($this->input->post('shippingaddress')),
			  'city' => db_clean($this->input->post('city')),
			  'post_code' => db_clean($this->input->post('post_code'))
		  );

		  	$e = $this->input->post('email');
			$numrow = $this->MCustomers->checkCustomer($e);
			if ($numrow == TRUE){
				// if there is email in db, then update the details
				$this->db->where('email', $e);
				$this->db->update('omc_customer',$data);
				// get the customer_id
				$customer_details = $this -> MCustomers->getCustomerByEmail($e);
				$customer_id = $customer_details['customer_id'];
			}else{
				// no email entry, then insert the details
		  		$this->db->insert('omc_customer',$data);
		  		// get the customer_id
		  		$customer_id = $this->db->insert_id();
			}

		  $data = array (
			   'customer_id'=> $customer_id,
			   'total' => $totalprice
		  );
		  $this->db->set('order_date', 'NOW()', FALSE);
		  $this->db->insert('omc_order', $data);
		  $order_id = $this->db->insert_id();
		  $cart = $_SESSION['cart'];
		  foreach ($cart as $id => $product){
				$data = array(
						'order_id' => $order_id,
						'product_id'=> $id ,
						'quantity' => $product['count'],
						'price'=> $product['price']
				);
		  $this->db->insert('omc_order_item', $data);
				}
	 }

	 function setpayment($id){
		  $this->db->where('order_id', $id);
		  $this->db->set('payment_date', 'NOW()', FALSE);
		  $this->db->update('omc_order');
	 }

	  function setdelivery($id){
		  $this->db->where('order_id', $id);
		  $this->db->set('delivery_date', 'NOW()', FALSE);
		  $this->db->update('omc_order');
	 }

	 function deleteOrderItem($id){
			$this->db->where('order_item_id', id_clean($id));
			$this->db->delete('omc_order_item');
	 }

	 function deleteOrder($id){
			$this->db->where('order_id', id_clean($id));
			$this->db->delete('omc_order');
	 }

	 function checkOrphans($id){
		 	$data = array();
		 	$this->db->select('order_item_id,name');
		 	$this->db->where('order_id',id_clean($id));
		 	$Q = $this->db->get('omc_order_item');
		    if ($Q->num_rows() > 0){
				return TRUE;
		    }else{
			 	return FALSE;
			}
		    $Q->free_result();
	 }

	 function findParent($order_item_id){
		  $this->db->where('order_item_id', $order_item_id);
		  $Q = $this->db->get('omc_order_item');
		    	if ($Q->num_rows() > 0){
					foreach ($Q->result_array() as $row){
						$data[] = $row;
					}
				}
		   $Q->free_result();
		   return $data;
	 }

	 function findsiblings($order_id){
		  $this->db->where('order_id', $order_id);
		  $Q = $this->db->get('omc_order_item');
		    	if ($Q->num_rows() > 0){
					foreach ($Q->result_array() as $row){
						$data[] = $row;
					}
				}
		   $Q->free_result();
		   return $data;
	 }

}//end class
?>

Views

admin_product_create

modules/orders/views/admin/admin_orders_home.php

<h2><?php echo $title;?></h2>
<?php
if ($this->session->flashdata('message')){
	echo "<div class='status_box'>".$this->session->flashdata('message')."</div>";
}

if (count($orders)){

	echo '<table id="tablesorter_product" class="tablesorter" border="1" cellspacing="0" cellpadding="3" width="100%">';
	echo "<thead>\n<tr valign='top'>\n";
	echo "<th>&nbsp;</th><th>Order ID</th>\n<th>First name</th><th>Last name</th><th>Email</th><th>Total</th><th>Telephone</th><th>Order Date</th><th>Delivery Date</th><th>Payment Date</th><th>City</th><th>Actions</th>\n";
	echo "</tr>\n</thead>\n<tbody>\n";
	foreach ($orders as $key => $list){
		echo "<tr valign='top'>\n";
		echo "<td align='center'>".form_checkbox('p_id[]',$list['order_id'],FALSE)."</td>";
		echo "<td align='center'>".$list['order_id']."</td>\n";
		echo "<td align='center'>".$list['customer_first_name']."</td>\n";
		echo "<td align='center'>".$list['customer_last_name']."</td>\n";
		echo "<td align='center'>".$list['email']."</td>\n";
		echo "<td align='center'>".$list['total']."</td>\n";

		// echo "<td align='center'>".$list['category_id']."</td>\n";
		echo "<td align='center'>".$list['phone_number']."</td>\n";
		echo "<td align='center'>".$list['order_date']."</td>\n";
		echo "<td align='center'>".$list['delivery_date']."</td>\n";
		echo "<td align='center'>".$list['payment_date']."</td>\n";
		echo "<td align='center'>".$list['city']."</td>\n";
		echo "<td align='center'>";
		echo anchor('orders/admin/paid/'.$list['order_id'],'paid');
		echo " | ";
		echo anchor('orders/admin/delivered/'.$list['order_id'],'delivered');
		echo " | ";
		echo anchor('orders/admin/details/'.$list['order_id'],'details');
		echo "</td>\n";
		echo "</tr>\n";
	}
	echo "</tbody></table>";
	echo form_close();
}
?>

admin_orders_details

modules/orders/views/admin/admin_orders_details.php

<h2><?php echo $title;?></h2>
<?php
if ($this->session->flashdata('message')){
	echo "<div class='status_box'>".$this->session->flashdata('message')."</div>";
}

if (count($orderdetails)){
	echo '<table id="tablesorter_product" class="tablesorter" border="1" cellspacing="0" cellpadding="3" width="100%">';
	echo "<thead>\n<tr valign='top'>\n";
	echo "<th>&nbsp;</th><th>Order ID</th>\n<th>Product Name</th><th>Quantity</th><th>Price</th><th>Action</th>\n";
	echo "</tr>\n</thead>\n<tbody>\n";
	foreach ($orderdetails as $key => $list){
		echo "<tr valign='top'>\n";
		echo "<td align='center'>".form_checkbox('p_id[]',$list['order_id'],FALSE)."</td>";
		echo "<td align='center'>".$list['order_id']."</td>\n";
		echo "<td align='center'>".$list['name']."</td>\n";
		echo "<td align='center'>".$list['quantity']."</td>\n";
		echo "<td align='center'>".$list['price']."</td>\n";
		echo "<td align='center'>";
		echo anchor('orders/admin/deleteitem/'.$list['order_id'].'/'.$list['order_item_id'],'delete');
		echo "</td>\n";
		echo "</tr>\n";
	}
	echo "</tbody></table>";
	echo form_close();
}
?>

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>