Bladephp: Now with 8+ codeigniter, php, and laravel project 8+ php project From $37/Month From Rs. 2499/Month

Shopping Cart using Codeigniter

by
Category:codeigniter
codeigniter
Shopping Cart using Codeigniter

In this tutorial, we are going to explain how we can use “Cart class in CodeIgniter”. This cart class allows us to add and remove products to a shopping cart and to update them. The Cart Class only provides the core “cart” functionality.  It does not provide other processing components. You may also like How to integrate Paypal payment gateway using Laravel 5 and Shopping Cart using Laravel 5.

Steps For Shopping Cart using Codeigniter

Step 1. Connect To Library File

In this step we have go to application/config/autoload.php and set libraries.

$autoload['libraries'] = array('database','session','cart');

otherwise to set direct library.

$this->load->library('cart');

Step 2. Create Route

In this step we have go to application/config/routes.php and set all route.

$route['product'] = 'frontcontroller/product';
$route['add_cart'] = 'frontcontroller/add_cart';
$route['cart'] = 'frontcontroller/cartitem';
$route['removecart'] = 'frontcontroller/removecart';
$route['updatecart'] = 'frontcontroller/updatecart';

Step 3. Create View using Controller

In this step we have create product list view and click on add to cart button to add item in cart after redirect cart item list.

<form action="<?=base_url()?>add_cart" method="post">
    <input type="hidden" name="id" value="<?=$result['product_id']?>" />
    <input type="hidden" name="qty" value="<?=$result['qty']?>" />
    <input type="hidden" name="price" value="<?=$result['price_working']?>" />
    <input type="hidden" name="name" value="<?=$result['name']?>" />
    <button class="right btn btn-primary" type="submit" style="padding: 8px;margin-left: 4px;font-weight: bold;">Add To Cart</button>
</form>

Step 4. Controller add_cart function using route

In this step click on add to cart button after add item in cart library and redirect in cart item list page.

public function add_cart() 
{
    $insert_data = array(
        'id' => $this->input->post('id'),
        'price' => $this->input->post('price'),
        'name' => $this->input->post('name'),
        'qty' => $this->input->post('qty')
    );

    $this->cart->insert($insert_data);
    redirect('cart');
}

Step 5. Controller cartitem function using route

In this step to see all item list.

public function cartitem() {
    $this->load->view('cart_item');
}

cart_item.php

<form action="<?= base_url() ?>updatecart" method="post">
    <div class="top">
        <ul>
            <li class="one">item</li>
            <li class="two">description</li>
            <li class="three" style="text-align: left;">Price</li>
            <li class="three">quantity </li>
            <li class="four">Subtotal</li>
        </ul>
    </div>

    <?php
    $finbalTotal = 0;
    $qtyTotal = 0;
    if (!empty($this->cart->contents())) {
        $cart = $this->cart->contents();
        foreach ($cart as $value) {
                $finbalTotal = $finbalTotal + $value['subtotal'];
                $qtyTotal = $qtyTotal + $value['qty'];
            ?>

            <input name="cart[<?= $value['id'] ?>][rowid]"  value="<?= $value['rowid'] ?>" type="hidden">
            <input name="cart[<?= $value['id'] ?>][name]"  value="<?= $value['name'] ?>" type="hidden">
            <input name="cart[<?= $value['id'] ?>][price]"  value="<?= $value['price'] ?>" type="hidden">

            <div class="bottom">
                <div class="bottom1">
                    <a href="<?= base_url() ?>removecart?rowid=<?= $value['rowid'] ?>">
                        <img src="<?= base_url() ?>img/close_1.png" style="height: 20px;">
                    </a>
                </div>
                <div class="bottom2">
                    <h5><?= $value['name'] ?></h5>
                </div>
                <div class="bottom4">
                    <h5><?php echo $value['price']; ?></h5>
                </div>
                <div class="bottom3" style="width: 134px !important;">
                    <input name="cart[<?= $value['id'] ?>][qty]"   value="<?= $value['qty'] ?>" type="text">
                </div>
                <div class="bottom4">
                    <h5><?php echo $value['subtotal']; ?></h5>
                </div>
            </div>
                <?php }
            ?>
    <button class="btn btn-success" type="submit" style="float: right;margin: 18px;margin-left: 7px;">Update Cart</button>
    <a class="btn btn-danger" href="<?= base_url() ?>removecart?rowid=all" style="float: right;margin: 18px;margin-right: 0px;">Clear Cart</a>
</form>

Step 6. Controller updatecart function using route

In this step click on update cart button after than process.

public function updatecart() {
    $cart_info = $_POST['cart'];
    foreach ($cart_info as $id => $cart) {
        $rowid = $cart['rowid'];
        $price = $cart['price'];
        $amount = $price * $cart['qty'];
        $qty = $cart['qty'];

        $data = array(
            'rowid' => $rowid,
            'price' => $price,
            'amount' => $amount,
            'qty' => $qty
        );
        $this->cart->update($data);
    }
    redirect('cart');
}

Step 7. Controller removecart function using route

In this step click on remove cart button after than process.

public function removecart() {
    $rowid = $this->input->get('rowid');
    if ($rowid === "all") {
        $this->cart->destroy();
    } else {
        $data = array(
            'rowid' => $rowid,
            'qty' => 0
        );
        $this->cart->update($data);
    }
    redirect('cart');
}

And if you like this tutorials please share it with your friends via Email or Social Media.


0 Comments


Html code work in comment box


Related Post