Easily develop a Ecommerce website with laravel

I have tested many packages for laravel Ecommerce website development but many of them were incomplete or useless but here is a great package that will ease your laravel ecommerce development .

Here i am talking about LARAVEL SHOP package which is very handy and flexiable to use ,

Laravel Shop is flexible way to add shop functionality to Laravel 5.1. Aimed to be the e-commerce solution for artisans.

Laravel shop adds shopping cart, orders and payments to your new or existing project; letting you transform any model into a shoppable item.

It supports multiple payment gateways , ex – paypal , stripe e.tc

PayPalOmnipay

Current version of package is tested with Laravel 5.1 here are some features that comes out of the box in this package

  • Shop Items (transforms existing models into shoppable items that can be added to cart and orders)
  • Cart
  • Orders
  • Transactions
  • Payment gateways support
  • PayPal
  • Events

On the horizon:

  • Guest user cart
  • Shipping orders
  • Coupons
  • Product and variations solution
  • Backend dashboard
  • Frontend templates

Ok now lets get started with installation

With composer

composer require amsgames/laravel-shop

Or add

"amsgames/laravel-shop": "0.2.*"

to your composer.json. Then run composer install or composer update.

Then in your config/app.php add

Amsgames\LaravelShop\LaravelShopProvider::class,

in the providers array.

Then add

'Shop'      => Amsgames\LaravelShop\LaravelShopFacade::class,

in the aliases array.

 

Now its time for some Configuration in our app

Set the configuration values in the config/auth.php file. This package will use them to refer to the user table and model.

Publish the configuration for this package to further customize table names, model namespaces, currencies and other values. Run the following command:

php artisan vendor:publish

A shop.php file will be created in your app/config directory.

 

now set up Database

Generate package migration file:

php artisan laravel-shop:migration

The command below will generate a new migration file with database commands to create the cart and item tables. The file will be located in database/migrations. Add additional fields if needed to fill your software needs.

The command will also create a database seeder to fill shop catalog of status and types.

Create schema in database:

php artisan migrate

Add the seeder to database/seeds/DatabaseSeeder.php:

class DatabaseSeeder extends Seeder
{

  public function run()
  {
    Model::unguard();

    $this->call('LaravelShopSeeder');

    Model::reguard();
  }

}

Run seeder (do composer dump-autoload first):

php artisan db:seed

Models

The following models must be created for the shop to function, these models can be customizable to fir your needs.

Item

Create a Item model:

php artisan make:model Item

This will create the model file app/Item.php, edit it and make it look like (take in consideration your app’s namespace):

<?php

namespace App;

use Amsgames\LaravelShop\Models\ShopItemModel;

class Item extends ShopItemModel
{
}

The Item model has the following main attributes:

  • id — Item id.
  • sku — Stock Keeping Unit, aka your unique product identification within your store.
  • price — Item price.
  • tax — Item tax. Defaulted to 0.
  • shipping — Item shipping. Defaulted to 0.
  • currency — Current version of package will use USD as default.
  • quantity — Item quantity.
  • class — Class reference of the model being used as shoppable item. Optional when using array data.
  • reference_id — Id reference of the model being used as shoppable item. Optional when using array data.
  • user_id — Owner.
  • displayPrice — Price value formatted for shop display. i.e. “$9.99” instead of just “9.99”.
  • displayTax — Tax value formatted for shop display. i.e. “$9.99” instead of just “9.99”.
  • displayShipping — Tax value formatted for shop display. i.e. “$9.99” instead of just “9.99”.
  • displayName — Based on the model’s item name property.
  • shopUrl — Based on the model’s item route property.
  • wasPurchased — Flag that indicates if item was purchased. This base on the status set in config file.
  • created_at — When the item record was created in the database.
  • updated_at — Last time when the item was updated.
Recommended :  How to use Events and Observers In laravel

Business definition: Item used as a cart item or an order item.

Cart

Create a Cart model:

php artisan make:model Cart

This will create the model file app/Cart.php, edit it and make it look like (take in consideration your app’s namespace):

<?php

namespace App;

use Amsgames\LaravelShop\Models\ShopCartModel;

class Cart extends ShopCartModel 
{
}

The Item model has the following main attributes:

  • id — Cart id.
  • user_id — Owner.
  • items — Items in cart.
  • count — Total amount of items in cart.
  • totalPrice — Total price from all items in cart.
  • totalTax — Total tax from all items in cart, plus global tax set in config.
  • totalShipping — Total shipping from all items in cart.
  • total — Total amount to be charged, sums total price, total tax and total shipping.
  • displayTotalPrice — Total price value formatted for shop display. i.e. “$9.99” instead of just “9.99”.
  • displayTotalTax — Total tax value formatted for shop display. i.e. “$9.99” instead of just “9.99”.
  • displayTotalShipping — Total shipping value formatted for shop display. i.e. “$9.99” instead of just “9.99”.
  • displayTotal — Total amount value formatted for shop display. i.e. “$9.99” instead of just “9.99”.
  • created_at — When the cart record was created in the database.
  • updated_at — Last time when the cart was updated.

Order

Create a Order model:

php artisan make:model Order

This will create the model file app/Order.php, edit it and make it look like (take in consideration your app’s namespace):

<?php

namespace App;

use Amsgames\LaravelShop\Models\ShopOrderModel;

class Order extends ShopOrderModel 
{
}

The Order model has the following main attributes:

  • id — Order id or order number.
  • user_id — Owner.
  • items — Items in order.
  • transactions — Transactions made on order.
  • statusCode — Status code.
  • count — Total amount of items in order.
  • totalPrice — Total price from all items in order.
  • totalTax — Total tax from all items in order, plus global tax set in config.
  • totalShipping — Total shipping from all items in order.
  • total — Total amount to be charged, sums total price, total tax and total shipping.
  • displayTotalPrice — Total price value formatted for shop display. i.e. “$9.99” instead of just “9.99”.
  • displayTotalTax — Total tax value formatted for shop display. i.e. “$9.99” instead of just “9.99”.
  • displayTotalShipping — Total shipping value formatted for shop display. i.e. “$9.99” instead of just “9.99”.
  • displayTotal — Total amount value formatted for shop display. i.e. “$9.99” instead of just “9.99”.
  • created_at — When the order record was created in the database.
  • updated_at — Last time when the order was updated.
Recommended :  Complete Beginners guide For Creating web pages with Laravel 5.2

Transaction

Create a Transaction model:

php artisan make:model Transaction

This will create the model file app/Transaction.php, edit it and make it look like (take in consideration your app’s namespace):

<?php

namespace App;

use Amsgames\LaravelShop\Models\ShopTransactionModel;

class Transaction extends ShopTransactionModel 
{
}

The Order model has the following main attributes:

  • id — Order id or order number.
  • order — Items in order.
  • gateway — Gateway used.
  • transaction_id — Transaction id returned by gateway.
  • detail — Detail returned by gateway.
  • token — Token for gateway callbacks.
  • created_at — When the order record was created in the database.
  • updated_at — Last time when the order was updated.

User

Use the ShopUserTrait trait in your existing User model. By adding use Amsgames\LaravelShop\Traits\ShopUserTrait and use ShopUserTrait like in the following example:

<?php

use Amsgames\LaravelShop\Traits\ShopUserTrait;

class User extends Model {

    use Authenticatable, CanResetPassword, ShopUserTrait;

}

This will enable the relation with Cart and shop needed methods and attributes.

  • cart — User’s cart.
  • items — Items (either order or cart).
  • orders — User’s orders.

 

Existing Model Conversion

Laravel Shop package lets you convert any existing Eloquent model to a shoppable item that can be used within the shop without sacrificing any existing functionality. This feature will let the model be added to carts or orders. The will require two small steps:

Use the ShopItemTrait in your existing model. By adding use Amsgames\LaravelShop\Traits\ShopItemTrait and use ShopItemTrait like in the following example:

<?php

use Amsgames\LaravelShop\Traits\ShopItemTrait;

class MyCustomProduct extends Model {

    use ShopItemTrait;

    // MY METHODS AND MODEL DEFINITIONS........

}

Add sku (string) and price (decimal, 20, 2) fields to your model’s table. You can also include name (string), tax (decimal, 20, 2) and shipping (decimal, 20, 2), although these are optional. You can do this by creating a new migration:

php artisan make:migration alter_my_table

Define migration to look like the following example:

<?php

class AlterMyTable extends Migration {

    public function up()
    {
        Schema::table('MyCustomProduct', function($table)
        {
            $table->string('sku')->after('id');
            $table->decimal('price', 20, 2)->after('sku');
            $table->index('sku');
            $table->index('price');
        });
    }

    public function down()
    {
        // Restore type field
        Schema::table('MyCustomProduct', function($table)
        {
            $table->dropColumn('sku');
            $table->dropColumn('price');
        });
    }

}

Run the migration:

php artisan migrate
Item name

By default, Laravel Shop will look for the name attribute to define the item’s name. If your exisintg model has a different attribute assigned for the name, simply define it in a property within your model:

<?php

use Amsgames\LaravelShop\Traits\ShopItemTrait;

class MyCustomProduct extends Model {

    use ShopItemTrait;

    /**
     * Custom field name to define the item's name.
     * @var string
     */
    protected $itemName = 'product_name';

    // MY METHODS AND MODEL DEFINITIONS........

}
Item url

You can define the URL attribute of the item by setting itemRouteName and itemRouteParams class properties. In the following example the url defined to show the product’s profile is product/{slug}, the following changes must be applied to the model:

<?php

use Amsgames\LaravelShop\Traits\ShopItemTrait;

class MyCustomProduct extends Model {

    use ShopItemTrait;

    /**
     * Name of the route to generate the item url.
     *
     * @var string
     */
    protected $itemRouteName = 'product';

    /**
     * Name of the attributes to be included in the route params.
     *
     * @var string
     */
    protected $itemRouteParams = ['slug'];

    // MY METHODS AND MODEL DEFINITIONS........

}

Dump Autoload

Dump composer autoload

composer dump-autoload

Payment Gateways

Installed payment gateways can be configured and added in the gateways array in the shop.php config file, like:

'gateways' => [
    'paypal'            =>  Amsgames\LaravelShopGatewayPaypal\GatewayPayPal::class,
    'paypalExpress'     =>  Amsgames\LaravelShopGatewayPaypal\GatewayPayPalExpress::class,
],

PayPal

Laravel Shop comes with PayPal support out of the box. You can use PayPal’s Direct Credit Card or PayPal Express payments.

Recommended :  Scanning routes in laravel

To configure PayPal and know how to use the gateways, please visit the PayPal Gateway Package page.

Omnipay

Install Omnipay Gateway to enable other payment services like 2Checkout, Authorize.net, Stripe and to name a few.

You might need to get some extra understanding about how Omnipay works.

 

 

Coming Next – basic usage ….

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest

Shares