PHP code example of baklysystems / laravel-shop-extended
1. Go to this page and download the library: Download baklysystems/laravel-shop-extended library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
baklysystems / laravel-shop-extended example snippets
class DatabaseSeeder extends Seeder
{
public function run()
{
Model::unguard();
$this->call('LaravelShopSeeder');
Model::reguard();
}
}
namespace App;
use BaklySystems\LaravelShop\Models\ShopItemModel;
class Item extends ShopItemModel
{
}
namespace App;
use BaklySystems\LaravelShop\Models\ShopCartModel;
class Cart extends ShopCartModel
{
}
namespace App;
use BaklySystems\LaravelShop\Models\ShopOrderModel;
class Order extends ShopOrderModel
{
}
namespace App;
use BaklySystems\LaravelShop\Models\ShopTransactionModel;
class Transaction extends ShopTransactionModel
{
}
use BaklySystems\LaravelShop\Traits\ShopUserTrait;
class User extends Model {
use Authenticatable, CanResetPassword, ShopUserTrait;
}
use BaklySystems\LaravelShop\Traits\ShopItemTrait;
class MyCustomProduct extends Model {
use ShopItemTrait;
// MY METHODS AND MODEL DEFINITIONS........
}
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');
});
}
}
use BaklySystems\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........
}
use BaklySystems\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
// Checks if cart has item with SKU "PROD0001"
$success = $cart->hasItem('PROD0001');
// This will create the order and set it to the status in configuration
$order = $cart->placeOrder();
$order = $cart->placeOrder('completed');
// Lets assume that the first Cart item is MyCustomProduct.
$item = $cart->items[0];
// Check if item has model
if ($item->hasObject) {
$myproduct = $item->object;
}
// Assuming MyCustomProduct has a types relationship.
$item->object->types;
// Assuming MyCustomProduct has myAttribute attribute.
$item->object->myAttribute;
// Get orders from specific user ID.
$orders = Order::findByUser($userId);
// Get orders from specific user ID and status.
$canceled_orders = Order::findByUser($userId, 'canceled');
// This will create the order and set it to the status in configuration
$transaction = $order->placeTransaction(
$gateway = 'my_gateway',
$transactionId = 55555,
$detail = 'Custom transaction 55555'
);
$completed = $order->isCompleted
// Checks if order is in a specific status.
$success = $order->is('completed');
// Quering
// Get orders from specific user ID.
$orders = Order::whereUser($userId)->get();
// Get orders from specific user ID and status.
$completed_orders = Order::whereUser($userId)
->whereStatus('completed')
->get();
class MyCustomStatusSeeder extends Seeder
{
public function run()
{
DB::table('order_status')->insert([
[
'code' => 'my_status',
'name' => 'My Status',
'description' => 'Custom status used in my shop.',
],
]);
}
}
$myStatusCode = 'my_status';
if ($order->is($myStatusCode)) {
echo 'My custom status work!';
}
namespace App\Handlers\Events;
use App\Order;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use BaklySystems\LaravelShop\Events\OrderCompleted;
class NotifyPurchase implements ShouldQueue
{
use InteractsWithQueue;
/**
* Handle the event.
*
* @param OrderPurchased $event
* @return void
*/
public function handle(OrderCompleted $event)
{
// The order ID
echo $event->id;
// Get order model object
$order = Order::find($event->id);
// My code here...
}
}
namespace Vendor\Package;
use BaklySystems\LaravelShop\Core\PaymentGateway;
use BaklySystems\LaravelShop\Exceptions\CheckoutException;
use BaklySystems\LaravelShop\Exceptions\GatewayException;
class GatewayPayPal extends PaymentGateway
{
/**
* Called on cart checkout.
* THIS METHOD IS OPTIONAL, DONE FOR GATEWAY VALIDATIONS BEFORE PLACING AN ORDER
*
* @param Order $order Order.
*/
public function onCheckout($cart)
{
throw new CheckoutException('Checkout failed.');
}
/**
* Called by shop to charge order's amount.
*
* @param Order $order Order.
*
* @return bool
*/
public function onCharge($order)
{
throw new GatewayException('Payment failed.');
return false;
}
}
public function onCharge($order)
{
// The transaction id generated by the provider i.e.
$this->transactionId = $paypal->transactionId;
// Custom detail of 1024 chars.
$this->detail = 'Paypal: success';
// Order status after method call.
$this->statusCode = 'in_process';
return true;
}
namespace Vendor\Package;
use BaklySystems\LaravelShop\Core\PaymentGateway;
use BaklySystems\LaravelShop\Exceptions\CheckoutException;
use BaklySystems\LaravelShop\Exceptions\GatewayException;
class GatewayWithCallbacks extends PaymentGateway
{
/**
* Called by shop to charge order's amount.
*
* @param Order $order Order.
*
* @return bool
*/
public function onCharge($order)
{
// Set the order to pending.
$this->statusCode = 'pending';
// Sets provider with the callback for successful transactions.
$provider->setSuccessCallback( $this->callbackSuccess );
// Sets provider with the callback for failed transactions.
$provider->setFailCallback( $this->callbackFail );
return true;
}
/**
* Called on callback.
*
* @param Order $order Order.
* @param mixed $data Request input from callback.
*
* @return bool
*/
public function onCallbackSuccess($order, $data = null)
{
$this->statusCode = 'completed';
$this->detail = 'successful callback';
$this->transactionId = $data->transactionId;
// My code...
}
/**
* Called on callback.
*
* @param Order $order Order.
* @param mixed $data Request input from callback.
*
* @return bool
*/
public function onCallbackFail($order, $data = null)
{
$this->detail = 'failed callback';
// My code...
}
}
bash
php artisan vendor:publish
bash
php artisan laravel-shop:migration
bash
php artisan migrate
bash
php artisan db:seed
bash
php artisan make:model Item
bash
php artisan make:model Cart
bash
php artisan make:model Order
bash
php artisan make:model Transaction
bash
php artisan migrate
bash
composer dump-autoload
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.