
Every ‘BUY’ button under a product is linked to cart method with it’s id. This adds details of the product to our shopping cart.

A link ‘SHOPPING CART’ at the top right and this will take a customer to our shopping cart page. The link is pages/cart and redirected to cart method.
If a customer has any products in the shopping cart, it displays the details as you see on the left image.
We display names, number of orders, subtotal of each product and total price.
A shipping charge will be calculated and added under the order details.
On the left column, each product has a delete button. This will recalculate subtotal and total cost and returns the result.
A customer can change the number of order and by clicking ‘UPDATE’ button, it will return the subtotal and total price.
‘GO to Checkout’ button will take a customer to a checkout page.
As I mentioned it before, the following code creates each product with a link to cart.
echo '<a href="' . site_url()."/".lang('webshop_folder'). '/cart/'.$image['id']. '"><p class="addtocart">'.lang('webshop_buy').'</p></a></div>';
Controllers: Calculating shipping price
This code is geared for Norwegian shipping rules, so you need to change according to your shipping method.
function shippingprice(){
// You need to modify this. This is for Norwegian system. At the moment, if a max of individual product is more
// than 268 kr, then shipping price will be 65 kr otherwise 0 kr or 25 kr.
$maxprice = 0;
if(isset($_SESSION['cart'])){
foreach ($_SESSION['cart'] as $item) {
if ($item['price'] > $maxprice) {
$maxprice = $item['price'];
}
}
$data['maxprice']=$maxprice;
$shippingprice = 0;
if ($maxprice > 268 ){
$shippingprice = 65.0;
}elseif($maxprice == 0){
$shippingprice = 0;
}else{
$shippingprice = 25.0;
}
$_SESSION['shippingprice'] = $shippingprice;
$data['shippingprice']=$shippingprice;
return $data;
}
}
cart method
function cart($productid=0){
$shippingprice = $this-> shippingprice();
$data['shippingprice']=$shippingprice['shippingprice'];
if ($productid > 0){
$fullproduct = $this->MProducts->getProduct($productid);
$this->MOrders->updateCart($productid,$fullproduct);
redirect( lang('webshop_folder').'/product/'.$productid, 'refresh');
}else{
$data['title'] = lang('webshop_shop_name')." | ". "Shopping Cart";
if (isset($_SESSION['cart'])){
$data['page'] = $this->config->item('backendpro_template_shop') . 'shoppingcart';
$data['module'] = lang('webshop_folder');
$this->load->view($this->_container,$data);
}else{
flashMsg('info',lang('orders_no_item_yet'));
// $this->session->set_flashdata('msg',lang('orders_no_item_yet'));
$data['page'] = $this->config->item('backendpro_template_shop') . 'shoppingcart';
$data['module'] = lang('webshop_folder');
$this->load->view($this->_container,$data);
}
}
}
function ajax_cart(){
// this is called by assets/js/shopcustomtools.js
// this is used when a customer click a update button in /index.php/webshop/cart page
$this->MOrders->updateCartAjax($this->input->post('ids'));
}
function ajax_cart_remove(){
// this is called by assets/js/shopcustomtools.js
// this is used when a customer click a delete button in /index.php/webshop/cart page
$this->MOrders->removeLineItem($this->input->post('id'));
}
view shoppingcart
modules/webshop/views/shop/shoppingcart.php
<h1><?php echo lang('general_shopping_cart'); ?></h1>
<div id='pleft'>
<?php
if ($this->session->flashdata('msg')){
echo "<div class='status_box'>";
echo $this->session->flashdata('msg');
echo "</div>";
}
?>
<?php echo form_open(lang('webshop_folder').'/checkout'); ?>
<table border='1' cellspacing='0' cellpadding='5' width='90%'>
<?php
if(isset($_SESSION['totalprice'])){
$data['totalprice'] = $_SESSION['totalprice'];
}
if (isset($_SESSION['cart'])){
foreach ($_SESSION['cart'] as $PID => $row){
$data = array(
'name' => "li_id[$PID]",
'value'=>$row['count'],
'id' => "li_id_$PID",
'class' => 'process',
'size' => 5
);
echo "<tr valign='top'>\n";
echo "<td>". form_input($data)."</td>\n";
echo "<td id='li_name_".$PID."'>". $row['name']."</td>\n";
echo "<td id='li_price_".$PID."'>".lang('webshop_currency_symbol'). $row['price']."</td>\n";
echo "<td id='li_total_".$PID."'>".lang('webshop_currency_symbol').number_format($row['price'] * $row['count'], 2,'.',',')."</td>\n";
echo "<td><input type='button' name='delete' value='".lang('webshop_delete')."' onclick='jsRemoveProduct($PID)'></td>\n";
echo "</tr>\n";
}
$TOTALPRICE = $_SESSION['totalprice'];
$TOTALPRICE = number_format($TOTALPRICE,2,'.',',');
$total_data = array('name' => 'total', 'id'=>'total', 'value' => $TOTALPRICE);
echo "<tr valign='top'>\n";
echo "<td colspan='3'>".lang('orders_total_price')."</td>\n";
echo "<td colspan='2'>".lang('webshop_currency_symbol')."$TOTALPRICE ".form_hidden($total_data)."</td>\n";
echo "</tr>\n";
echo "<tr valign='top'>\n";
echo "<td colspan='3'> </td>\n";
echo "<td colspan='2'><input type='button' name='update' value='".lang('webshop_update')."' onclick='jsUpdateCart()'/></td>\n";
echo "</tr>\n";
echo "<tr valign='top'>\n";
echo "<td colspan='3'> </td>\n";
$data = array(
'name' => 'submit',
'value' => lang('webshop_checkout'),
);
echo "<td colspan='2'>".form_submit($data)."</td>\n";
echo "</tr>\n";
}else{
//just in case!
echo "<tr><td id='tdmes'>" . lang('webshop_no_items_to_show') . "</td></tr>\n";
}//end outer if count
?>
</table>
<?php echo form_close(); ?>
<br />
<?php
if($shippingprice>0){
echo "<div class='status_box'><h2>";
echo lang('webshop_shipping_charge');
if (isset($shippingprice)){
echo " ". lang('webshop_currency_symbol')."$shippingprice";
}else{
echo "0 ". lang('webshop_currency');
}
echo " " . lang('webshop_will_be_added');
echo "</h2></div>";
}
?>
<div id='ajax_msg'></div>
</div>


















