Shopping Cart using Laravel 5

In this tutorial, we are going to implement the shopping cart functionality in laravel 5 and use package. The package project is on GitHub LaravelShoppingcart. Let’s implement a practical example of an e-commerce website that has shopping cart functionality. To be clear, we will be using an existing composer package that already has shopping cart functionality.

Steps For Shopping Cart using Laravel 5

Step 1. Installing Laravel Shopping Cart via composer

In this section, we will install the package for Laravel Shopping cart “gloudemans/shoppingcart”: “^2.3”.

"require": {
    "php": ">=5.5.9",
    "laravel/framework": "5.2.*",
    "gloudemans/shoppingcart": "^2.3",
    "netshell/paypal": "dev-master"
},

In above code implement in composer.json after update composer

composer update

Step 2. Configuring Laravel 5 to work with Shopping Cart

We now need to make a few configurations to our project so that the shopping cart can work (config/app.php).

'providers' => [
    Gloudemans\Shoppingcart\ShoppingcartServiceProvider::class,
],
'aliases' => [
    'Cart' => Gloudemans\Shoppingcart\Facades\Cart::class,
],

Step 3. Create route

We are required four route for show item form view and one another is for make post HTTP request and third for checkout page and four one is remove cart item. you can like also paypal payment gateway system in laravel 5.

Route::get('item', [
    'as' => 'item',
    'uses' => 'CheckoutController@item'
]);

Route::post('cart', [
    'as' => 'cart',
    'uses' => 'CheckoutController@cart'
]);

Route::get('remove-cart/{rowid}', [
    'as' => 'remove-cart',
    'uses' => 'CheckoutController@RemoveCart'
]);


Route::get('checkout', [
    'as' => 'checkout',
    'uses' => 'CheckoutController@checkout'
]);

Step 3. Item Page View

In this page set all value in specific name wise and click on buy now button to call cart route

<form action="{{url('cart')}}" method="post">
  <input type="hidden" name="_token" value="{{ csrf_token() }}">
  <input type="hidden" value="{{$getproject->title}}" name="name">
  <input type="hidden" value="{{$getproject->imagefile}}" name="img">
  <input type="hidden" value="{{$getproject->id}}" name="id">
  <input type="hidden" value="{{$getproject->price}}" name="price">
  <button class="buy-ebook__button"  type="submit" ><span class="buy-ebook__button-label">Buy Now</span></button>
</form>

Step 4. CheckoutController with cart function

Before we modify the cart method, we will first need to import the Cart namespace

use Cart;

Click on buy now button and add data in cart and redirect checkout page to display all item.

public function cart(Request $request)
{ 
	$name = $request->get('name');
	$price = $request->get('price');
	$image = $request->get('image');
	$id = $request->get('id');
    Cart::add(['id' => $id, 'name' => $name, 'img' => $image, 'qty' => 1, 'price' => $price]);
    return redirect('checkout');
}

Step 5. CheckoutController with checkout function

In this step to see all item list.

public function checkout()
{    	
	return view('checkout.checkout');
}

checkout.blade.php file

<table id="edd_checkout_cart" class="ajaxed">
   <thead>
      <tr class="edd_cart_header_row">
         <th class="edd_cart_item_name">Item Name</th>
         <th class="edd_cart_item_price">Item Price</th>
         <th class="edd_cart_actions">Actions</th>
      </tr>
   </thead>
   <tbody>
      @foreach(Cart::content() as $row)
      <tr class="edd_cart_item" id="edd_cart_item_0_244951" data-download-id="244951">
         <td class="edd_cart_item_name">
            <div class="edd_cart_item_image">
               <img width="25" height="13" src="{{url('projectimage/'.$row->image)}}" class="attachment-25x25 size-25x25 wp-post-image" alt="" />
            </div>
            <span class="edd_checkout_cart_item_title">{{$row->name}}</span>
         </td>
         <td class="edd_cart_item_price">
            &#36;{{$row->price}}					
         </td>
         <td class="edd_cart_actions">
            <a class="" href="{{url('remove-cart/'.$row->rowId)}}">Remove</a>
         </td>
      </tr>
      @endforeach
   </tbody>
   <tfoot>
      <tr class="edd_cart_footer_row edd_cart_discount_row"  style="display:none;">
         <th colspan="5" class="edd_cart_discount">
         </th>
      </tr>
      <tr class="edd_cart_footer_row">
         <th colspan="5" class="edd_cart_total">Tax: <span class="edd_cart_amount" data-subtotal="9.69" data-total="9.69">&#36;{{Cart::tax()}}</span></th>
      </tr>
      <tr class="edd_cart_footer_row">
         <th colspan="5" class="edd_cart_total">Total: <span class="edd_cart_amount" data-subtotal="9.69" data-total="9.69">&#36;{{Cart::total()}}</span></th>
      </tr>
   </tfoot>
</table>

Step 6. CheckoutController with RemoveCart function

In this step click on remove button to remove cart item.

public function RemoveCart($rowid)
{
    Cart::remove($rowid);
    return redirect()->back();
}

Leave a Reply

Your email address will not be published. Required fields are marked *