在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):amsgames/laravel-shop开源软件地址(OpenSource Url):https://github.com/amsgames/laravel-shop开源编程语言(OpenSource Language):PHP 93.7%开源软件介绍(OpenSource Introduction):ITwrx fork of LARAVEL SHOP (minor changes for Laravel 5.2 Compatibility)Laravel Shop is flexible way to add shop functionality to Laravel 5.2. 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. Supports Contents
ScopeCurrent version includes:
On the horizon:
InstallationWith composer composer require amsgames/laravel-shop Or add "amsgames/laravel-shop": "0.2.*" to your composer.json. Then run Then in your Amsgames\LaravelShop\LaravelShopProvider::class, in the Then add 'Shop' => Amsgames\LaravelShop\LaravelShopFacade::class, in the ConfigurationSet the configuration values in the 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 Database SetupGenerate 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 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 class DatabaseSeeder extends Seeder
{
public function run()
{
Model::unguard();
$this->call('LaravelShopSeeder');
Model::reguard();
}
} Run seeder (do php artisan db:seed ModelsThe following models must be created for the shop to function, these models can be customizable to fir your needs. ItemCreate a Item model: php artisan make:model Item This will create the model file <?php
namespace App;
use Amsgames\LaravelShop\Models\ShopItemModel;
class Item extends ShopItemModel
{
} The
Business definition: Item used as a cart item or an order item. CartCreate a Cart model: php artisan make:model Cart This will create the model file <?php
namespace App;
use Amsgames\LaravelShop\Models\ShopCartModel;
class Cart extends ShopCartModel
{
} The
OrderCreate a Order model: php artisan make:model Order This will create the model file <?php
namespace App;
use Amsgames\LaravelShop\Models\ShopOrderModel;
class Order extends ShopOrderModel
{
} The
TransactionCreate a Transaction model: php artisan make:model Transaction This will create the model file <?php
namespace App;
use Amsgames\LaravelShop\Models\ShopTransactionModel;
class Transaction extends ShopTransactionModel
{
} The
UserUse the <?php
use Amsgames\LaravelShop\Traits\ShopUserTrait;
class User extends Model {
use Authenticatable, CanResetPassword, ShopUserTrait;
} This will enable the relation with
Existing Model ConversionLaravel Shop package lets you convert any existing Use the <?php
use Amsgames\LaravelShop\Traits\ShopItemTrait;
class MyCustomProduct extends Model {
use ShopItemTrait;
// MY METHODS AND MODEL DEFINITIONS........
} Add 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 nameBy default, Laravel Shop will look for the <?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 urlYou can define the URL attribute of the item by setting <?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 AutoloadDump composer autoload composer dump-autoload Payment GatewaysInstalled payment gateways can be configured and added in the 'gateways' => [
'paypal' => Amsgames\LaravelShopGatewayPaypal\GatewayPayPal::class,
'paypalExpress' => Amsgames\LaravelShopGatewayPaypal\GatewayPayPalExpress::class,
], PayPalLaravel Shop comes with PayPal support out of the box. You can use PayPal's To configure PayPal and know how to use the gateways, please visit the PayPal Gateway Package page. OmnipayInstall 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. UsageShopShop methods to consider: Format prices or other values to the price format specified in config: $formatted = Shop::format(9.99);
// i.e. this will return $9.99 or the format set in the config file. Purchase FlowWith Laravel Shop you can customize things to work your way, although we recommend standarize your purchase or checkout flow as following (will explain how to use the shop methods below):
Payment GatewayBefore any shop method is called, a payment gateway must be set: // Select the gateway to use
Shop::setGateway('paypal');
echo Shop::getGateway(); // echos: paypal You can access the gateway class object as well: $gateway = Shop::gateway();
echo $gateway; // echos: [{"id":"paypal"}] CheckoutOnce a payment gateway has been selected, you can call cart to checkout like this: // Checkout current users' cart
$success = Shop::checkout();
// Checkout q specific cart
$success = Shop::checkout($cart); This will call the Order PlacementOnce a payment gateway has been selected and user has checkout, you can call order placement like: // Places order based on current users' cart
$order = Shop::placeOrder();
// Places order based on a specific cart
$order = Shop::placeOrder($cart); NOTE: This will call the PaymentsPayments are handled gateways, this package comes with PayPal out of the box. You can use PayPal's To configure PayPal and know how to use its gateways, please visit the PayPal Gateway Package page. ExceptionsIf checkout or placeOrder had errores, you can call and see the exception related: // On checkout
if (!Shop::checkout()) {
$exception = Shop::exception();
echo $exception->getMessage(); // echos: error
}
// Placing order
$order = Shop::placeOrder();
if ($order->hasFailed) {
$exception = Shop::exception();
echo $exception->getMessage(); // echos: error
} Critical exceptions are stored in laravel's log. Shopping CartCarts are created per user in the database, this means that a user can have his cart saved when logged out and when he switches to a different device. Let's start by calling or creating the current user's cart: // From cart
$cart = Cart::current();
// Once a cart has been created, it can be accessed from user
$user->cart; Note: Laravel Shop doen not support guest at the moment. Get the cart of another user: $userId = 1;
$cart = Cart::findByUser($userId); Adding ItemsLest add one item of our test and existing model $cart = Cart::current()->add(MyCustomProduct::find(1)); By default the add method will set a quantity of 1. Instead lets add 3 $cart = Cart::current();
$cart->add(MyCustomProduct::find(1), 3); Only one item will be created per sku in the cart. If an item of the same $product = MyCustomProduct::find(1);
// Adds 1
$cart->add($product);
// Adds 3
$cart->add($product, 3);
// Adds 2
$cart->add($product, 2);
echo $cart->count; // echos: 6
$second_product = MyCustomProduct::findBySKU('TEST');
// Adds 2 of product 'TEST'
$cart->add($second_product, 2);
// Count based on quantity
echo $cart->count; // echos: 8
// Count based on products
echo $cart->items->count(); // echos: 2 We can reset the quantity of an item to a given value: // Add 3
$cart->add($product, 3);
echo $cart->count; // echos: 3
// Reset quantity to 4
$cart->add($product, 4, $forceReset = true);
echo $cart->count; // echos: 4 |