Suppose we have developed a custom module or package for Bagisto then we should have an option to Enable/Disable it’s functionality from admin panel anytime whenever we need it.
Let’s discuss it with an example:
Suppose we have a package named Whats App for placing an order, now using a Whats App button we are performing action for placing an order.
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 |
<?php return [ [ 'key' => 'whatsapp', 'name' => 'whatsApplang::app.whats-app-setting', 'sort' => 7, ], [ 'key' => 'whatsapp.whatsapp', 'name' => 'whatsApplang::app.whats-app', 'sort' => 1, ], [ 'key' => 'whatsapp.whatsapp.locale_options', 'name' => 'WhatsApp Status', 'sort' => 1, 'fields' => [ [ 'name' => 'is_active', 'title' => 'whatsApplang::app.enable-or-disable', 'type' => 'boolean', 'channel_based' => false, 'locale_based' => false, ], ], ] ]; |
Now register your config into your package provider.
Here we have also loaded the translation files and views into provider. Now under registerConfig() method we have merged our config file
Also add all the translations into translation files.
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 |
<?php namespace Webkul\WhatsAppNotifications\Providers; use Illuminate\Support\ServiceProvider; class WhatsAppServiceProvider extends ServiceProvider { /** * Bootstrap application services. * * @param \Illuminate\Routing\Router $router * @return void */ public function boot(): void { $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'whatsApplang'); $this->loadViewsFrom(__DIR__ . '/../Resources/views', 'WhatsAppViews'); } /** * Register services. * * @return void */ public function register() { $this->registerConfig(); } /** * Register package config. * * @return void */ protected function registerConfig() { $this->mergeConfigFrom( dirname(__DIR__) . '/Config/system.php', 'core' ); } } |
Run this command i.e. php artisan optimize
Now, It will display in admin panel.
Now after clicking on WhatsApp Setting menu link you will redirect to configuration page.
Finally Just by adding a condition over the button we can check status of our module.
1 2 3 4 5 |
@if (core()->getConfigData('whatsapp.whatsapp.locale_options.is_active')) <div class="default-wrap" style="margin-top:10px; display:block;"> <button type="button" style="width:100%" class="btn btn-lg btn-primary addtocart" onclick="window.location='{{ route('shop.productOrCategory.index', $product->url_key) }}'">{{__('whatsApplang::app.whats-app') }}</button> </div> @endif |
So by this way, you can Enable/Disable module functionality and control it from admin panel.
I’ve explored this while contributing to Laravel based project Bagisto, there are a lot more things to learn and you could also contribute to an open source E-Commerce framework.
Thanks for reading me. I hope you’ll get an idea how create an option to Enable/Disable a module in Bagisto.