Updated 15 May 2023
It becomes very easy to create custom Shipping Methods in bagisto for both (i.e. Novice developer as well as Professional Developers). As the diversity of shipping methods provide the options to customer for various shipments when they proceed to checkout. On another perspective, multiple shipping methods are a great strategy to reach out to the global marketplace.
There are basically two ways to create Custom Shipping Methods in bagisto.
If you are using bagisto 1.2.0 or above, what you can do is, You can simply run the command below which will make appropriate directory structure for you which will be used further.
1 |
php artisan package:make-shipping-method ACME/FedEx |
If, in case your Package Name already exist at the same directory, you can also use –force with the above command which goes like this:
1 |
php artisan package:make-shipping-method ACME/FedEx --force |
You have to create the directory structure of your own. Follow and create the directory structure mentioned below.
You have to create the directory structure of your own. Follow and create the directory structure mentioned below.
Create this directory structure to create your payment method.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php return [ 'DemoShipping' => [ 'code' => 'demoshipping', 'title' => 'DemoShipping', 'description' => 'Demo Shipping', 'active' => true, 'type' => 'per_unit', 'class' => 'ACME\FedEx\Carriers\FedEx', ] ]; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php return [ [ 'key' => 'sales.carriers.demoshipping', 'name' => 'Demo Shipping', 'sort' => 1, 'fields' => [ [ 'name' => 'title', 'title' => 'admin::app.admin.system.title', 'type' => 'text', 'validation' => 'required', 'channel_based' => false, 'locale_based' => true, ], [ 'name' => 'description', 'title' => 'admin::app.admin.system.description', 'type' => 'textarea', 'channel_based' => false, 'locale_based' => true, ], [ 'name' => 'active', 'title' => 'admin::app.admin.system.status', 'type' => 'boolean', 'validation' => 'required', 'channel_based' => false, 'locale_based' => true, ] ] ] ]; |
What this will do is, It will create the fields in the admin end where you can configure your settings. It will look like this:
In order to render your custom shipping method at the checkout process, you just need to define ‘calculate()’ within your DemoShipping.php and return shipping rate, shipping title, shipping description within an object.
You can refer to the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?php namespace Webkul\DemoPayment\Carriers; use Config; use Webkul\Checkout\Models\CartShippingRate; use Webkul\Shipping\Facades\Shipping; use Webkul\Checkout\Facades\Cart; /** * Class Rate. * */ class DemoShipping extends AbstractShipping { /** * Payment method code * * @var string */ protected $code = 'demoshipping'; /** * Returns rate for flatrate * * @return CartShippingRate|false */ public function calculate() { if (! $this->isAvailable()) { return false; } $cart = Cart::getCart(); $object = new CartShippingRate; $object->carrier = 'flatrate'; $object->carrier_title = $this->getConfigData('title'); $object->method = 'flatrate_flatrate'; $object->method_title = $this->getConfigData('title'); $object->method_description = $this->getConfigData('description'); $object->price = 0; $object->base_price = 0; if ($this->getConfigData('type') == 'per_unit') { foreach ($cart->items as $item) { if ($item->product->getTypeInstance()->isStockable()) { $object->price += core()->convertPrice($this->getConfigData('default_rate')) * $item->quantity; $object->base_price += $this->getConfigData('default_rate') * $item->quantity; } } } else { $object->price = core()->convertPrice($this->getConfigData('default_rate')); $object->base_price = $this->getConfigData('default_rate'); } return $object; } } |
After doing all these things and enabling payment method from the admin end, you’ll be able to see the shipping method at the front end at the time of checkout.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Also how can do i it based on certain city?