Start a Project

How to create a package of Stock Notifications in Bagisto

Stock Notification allows users to get notified when an out-of-stock product gets back in stock and is available for purchase.

The users just have to provide their email ID and they will receive a mail when the product gets back in stock. The admin can choose whether the users get notified automatically or manually.

 I will guide you through the process of creating a Stock Notification Package in Bagisto.

Building a Stock Notification Package in Bagisto Step-by-Step Tutorial

The stock Notification module allows customers to subscribe to the email alerts for the out-of-stock products. An automated or manual email will be sent to the customers who have opted for the out-of-stock notification.

1) Module Setup

So in the installation of Bagisto, you will get the directory name Webkul which contains the packages for Bagisto.  Create a directory named  Notify & src inside the Webkul folder at the root of the Bagisto installation. packages directory is the container of all namespaces of packages. A namespace or vendor name is a group of entities or classes that are separated from classes of other namespaces with the same name so they don’t conflict. Every extension or package that we will develop should be kept in the same namespace to avoid conflict between classes with the same name.

Now add the below code inside the “autoload psr-4” in the  composer.json file  as shown below.                                  

Now register in the provider array with the array merge function in the config/app.php file which is presented in the root directory of Bagisto.

2) Providers

In the src folder, create a folder named Providers. Inside that folder, create a file named NotifyServiceProvider.php. Make sure to create the file name according to your module name. Now the structure will look like this.

Run the below command to register the package in the root directory of Bagisto.

3) Migrations

Create migrations to create a table inside the database and the directory structure will look like this-

So hit the below command in the terminal inside the root directory of Bagisto to create a new table by the name of notify in the database.

Loading Migrations From Package 

Now add the below code inside the NotifyServiceProvider.php file in the boot method.

Creating Table From Migrations

Run the given command in the root directory of Bagisto to create the notify table in your database.

4) Models

Now, create a folder named Contracts inside Webkul/Notify/src and create an interface file named Notify.php. As shown in the below image.

Now, create a Models folder inside packages/Webkul/Notify/src/. Inside the models folder, create a model proxy file named NotifyProxy.php. As shown in the below image.

Now, we need to create a provider

By the name of ModuleServiceProvider.php inside Webkul/Notify/src/Providers. And change the namespaces and the models class name accordingly.

Finally, register the Module Service Provider

Create ModuleServiceProvider.php file in the config/concord.php file and this file is presented in the root directory of Bagisto.

5) Repository

Create a Repository folder inside Webkul/Notify/src/ and create a file named NotifyRepository.php. In the repository class, create the model() method that returns the path of your contract class.

6) Routes

Create a Routes folder inside packages /Webkul/Notify/src/ and create 2 files named admin-routes.php and shop-routes.php. To learn in detail about Routes, you can visit the Laravel documentation here.

Admin-

Shop-

Loading Routes

Now, we need to register our routes in the service provider’s boot method, which is located in NotifyServiceProvider.php

7) Controllers

To learn in detail about Controllers, you can visit the Laravel documentation here

Create an Http folder in the packages/Webkul/Notify/src path. Inside the Http folder, create another folder named Controllers. Inside the Controllers folder, we need to create one file named Controller.php and two folders, namely Admin and Shop for the backend and frontend respectively. 

Inside both the Admin and Shop folders, create a NotifyController.php file. The updated directory structure will look like this. 

Admin Controller File –

Shop Controller File –

Controller.php 

8) Views

Create a Resources folder in the packages/Webkul/Notify/src path. Inside the Resources folder, create another folder named views. Now inside the views folder, we need to create one more folder ie adminNow create one file by the name of index.blade.php inside the admin folder. The structure will look something like this.

index.blade.php 

Load Views From Package

 Now we need to register our views in the service provider’s boot method. Open the file packages/Webkul/Notify/src/Providers/NotifyServiceProvider.php and update it as follows.

9) Localization

So, let’s create a language file for our package. Create a lang folder in the packages/Webkul/Notify/src/Resources path. Inside the lang folder, you create different folders for language translations. For example, you create a folder for English (‘en’), Hindi (‘hi’), etc. For now, let’s create a folder named en (representing the language code). Inside the en folder, create a file named app.php for language translations. The structure and file will look like this.

app.php

Load Translations From Package

Now register the language file in the service provider in the boot method.

10) Admin menu

To ensure that the admin menu includes the necessary configurations, follow the steps which are given below.

  1. In your package’s source directory, which is typically located at packages/Webkul/Notify/src, Create a new folder named Config
  2. Inside the newly created Config folder, create a file named admin-menu.php.

The structure and file will look like this.

admin-menu.php

      3. Inside merge the admin-menu.php configuration with the core menu file, use the mergeConfigFrom()          method in the register() method of your package’s service provider. Here’s an example.

Now run the below command to clear the cache in the root directory of Bagisto.

11) Data Grid

A DataGrid is a concept of displaying your database records in a tabular format. It is a powerful tool that helps you present large amounts of data in an organized way. At Bagisto, we have implemented this feature, which allows you to use it to display data in tabular format or write code from scratch to display data. In addition to the basic functionality of DataGrid, We have also implemented additional features such as sorting, filtering, and mass action.

  1. Create a folder called DataGrids inside the src folder of your package. Within the DataGrids folder, create a file name NotifyDataGrid.php
                                                             
    NotifyDataGrid.php

Now to make more changes and get the data into that DataGrid firstly you have to apply some of the changes in the 

packages/Webkul/Shop/src/Http/Controllers/ProductsCategoriesProxyController.php file to get the quantity into the product variable as shown below.                                               

So add one more change in the blade file which is coming and handling the logical part of the product detail page from frontend ie packages/Webkul/Shop/src/Resources/views/products/view.blade.php and apply this below code in the @pushOnce(‘scripts’).

So create a button of Notify Me in the same file below the Add To Cart button and apply a condition because if the quantity is more than 0 it will show the Add To Cart button and if the quantity is 0 it will show the Notify Me button. As shown below. And apply the condition as well.

12) Mail Sent To Customers

So send an Email directly to the customers after clicking on the button Send Notification from the DataGrid. But you just need to create 2 folders and create a respective file for that.

 The directory structures of both the files will look like this.

                                                         

ProductBackInStockNotification.php                                                           

ProductUpdateAfterListener.php

Now Create a new file packages/Webkul/Notify/src/providers/EventServiceProvider.php to send the event and mail to the bundled of customers after updating a product. The directory structure and file will look like this. 

So register these events inside the NotifyServiceProvider.php 

And the last step is to replace your SMTP credentials in the ENV file which is presented in the root directory. Check the response after clicking the button of Send Notification from the DataGrid. The DatGrid looks like this.


So this is our Product Detail Page looks like the product quantity will get Zero.

So Mail is received by the Customer After Hitting the button Send Notification for a particular customer. But after Updating the product mail is sent directly to all the customers which is been presented in the DataGrid.

Thanks for reading this blog. I hope you’ll get an idea of how to create a custom stock notification package. Please share your reviews on this, which will support me to write more.

You can directly get the code from the GitHub Stock Notification repository

Additionally, you can also Hire Laravel Developers to help you with your project requirements and ensure the successful implementation of your ideas.

PIjIxtgzOJk
Exit mobile version