Connect Multiple Database with single Magento instance

As we all know Magento is a one of the most popular ecommerce technologies, which also provides a reliabile method to use its multiple features which helps to increase sales growth as well as manage everything from Admin panel which makes it little complex as well.

So, as its complexity increases with its functionalities. It also provides a feature to connect more that one Database with the same instance and use its data for our work or to increase further validation for the Magento instance. It totally depends upon us, how we need to use it in multiple ways.

So, Let’s start with this Topic.

By default, we all know Magento uses a single Database to run and save the data of the store. All Database credentials are store in env.php file at magento_root/app/etc/env.php

So, to connect our new Database, we need to first provide access in the parameters which Magento accepts.

New Connection Setup

Here in the above env.php file, we have added our Custom database to create a connection.

Here, connection_name will be some specific name(string) given to identify the custom database. connection_name_setup name will be given in the resource which will be used to fetch the data from this DB.

Here we have created our connection with the new DB. But make sure the DB exists and the given credentials of the DB work fine.

So, Now the DB is connected. let’s proceed to see how to get data from this New DB.

So, Now to fetch data I am using a helper here which I would say a global file to call this connection through resource connection objection.

In the above code have added the connection name to create connection and get data from custom Database.

Have used \Magento\Framework\App\ResourceConnection to create connection bypassing the connection name that we have created in env.php file

Note: In the above Helper one can also add where condition to fetch data with a filter.

Example:
$connection->select()->from( [‘mainTable’ => ‘table_name’])->where(‘id’, ‘xxx’);

So, can be used for different scenario too.

Well, All Done for now.

Prevent Payment Methods According To The Conditions

Let’s Begin with the new thing today. The most common or I would say, one of the most important too topic of Magento 2. Every Store flow in Magento has to go through this. so we can say it’s a common path for all.

Before started working on the Payment Prevention method on the checkout page. Let’s have a look at why we need this method or functionality.

Example: if someone wants to hide some active Payment method for a few SKUs or for some attribute or may be for some cart subtotal, then by default Magento does not provide such functionality by which we can adjust payment methods. So, for this, we will need to do some customizations.

Let begin this step by step.

Let’s start with the Basic Module Structure First. Here I assume one should already Have Basic knowledge about Module and its structure, if not please refer to this link first.

I have created an Excellence_Preventpayment Module.

So, we first have to put a validation that prevents the active payment authentication on the payment section of checkout page.

To do this we need to use observer which will validate the payment method according to the condition

Excellence/Preventpayment/etc/events.xml

As we have added our custom observer PaymentMethodAvailable.php at Excellence\Preventpayment\Observer\PaymentMethodAvailable

So, now when a customer proceeds to the payment page this observer will get a call and hence will validate the payment methods as per our condition here.

So, now the most common scenario here which I am not going to implement as it depends upon the developer how he/she fetch the conditional data which we will implement on this observer.

Here, I am setting data into the store config from where Admin can select the payment method which he/she wants to disable on the payment page.

colorpicker

After this, will be fetching the selected payment methods in our observer with the help of observer and comparing these payments with the active payment methods on the store. so, those active payment methods match without the selected payment method will get hide on the payment page.

In the above code, I have checked that the module is enabled or not, then proceeded with rest of the functionalities to fetch the data and apply condition accordingly. Hence on the Payment page, now the selected methods would be disabled even if a particular payment method is enabled from admin panel.

So, That’s all folks.

I have given a small scenario that how we can implement this on the frontend, still one can utilize this with different scenarios and can also make it much better by implementing more functionalities according to their need.

Prevent Shipping Methods Based on Conditions

Let’s Begin with the new thing today. The most common or I would say, one of the most important too topic of Magento 2. Every Store flow in Magento has to go through this. so we can say it’s a common path for all.

Well Before proceeding with this topic Preventing Shipping. Let discuss the shipping page(Checkout Step 1) first.

Let’s not go into that deep. Just an overview that why we require the Shipping methods. As we all know on the Ecommerce store each product requires a shipping method to make the order successfully delivered to one’s Address.

So, the shipping methods comes many with challenges. Here are few Examples:

Shipping Method customization depends upon factors like Price, weight, products, country, etc.
Show custom message as per conditions and many more.

Here we will apply the condition to prevent Shipping methods depending upon conditions like products, price, weight, for all shipping methods, etc.

Let’s begin with this Step by step.

Let’s start with the Basic Module Structure First. Here I assume one should already Have Basic knowledge about Module and its structure, if not please refer to this link first.

I have created an Excellence_Preventshipping Module.

So, we first have to take an excess of the method which is responsible for filtering and shows valid shipping methods on checkout. Magento uses Shipping.php to validate all shipping methods

Magento/Shipping/Model/Shipping.php

Here in this we have to use collectCarrierRates($carrierCode, $request) function.

For this we can either override this model file or use plugin for this around/after/before to validate our conditions.

For now, I am overriding this model.

Let’s create di.xml file to override this class.

Now, as we have overridden the shipping.php class to our vendor files which are at Excellence\Preventshipping\Model\Rewrite\Shipping. So create a file at this path with the same name Shipping.php as we have given in di.xml.

Here in the above section, we have added the condition in collectCarrierRates() function for validation in which we are passing the method name.

Similarly, we can add a condition for price and weight too, depending upon our requirement.

We can also use a plugin in collectCarrierRates() function to do this and return false if our condition gets true.

Add Dynamic Grid Structure In Admin Config

Adding Dynamic Row in store config solves many problems for a developer as well as for the Admin. For example, by using this, one doesn’t require to use a custom Grid section in another part of the admin and fetch data from a custom table for specific work. So by using this we can also save Our time and it’s quite a user friendly.

Let’s start with the Basic Module Structure First. Here I assume one should already Have Basic knowledge about Module and its structure, if not please refer to this link first.

Now, as we are now familiar with the Basic module structure Lets proceed further. Here I have taken Module name as Excellence_Grid.

We will provide the access or configuration in Store configuration, Section Which is at Stores -> Configuration (under settings).

For this, we need to create the instruction for configuring. So, we will create a system.xml file in our module first.

Excellence/Grid/etc/adminhtml/system.xml

In above, we have added the configuration section in
STORES > Settings > Configuration > GENERAL > General > Quantity Ranges.

In the above, we have mentioned the block class through which we will create columns in the grid.

So, create a block class
Excellence\Grid\Block\Adminhtml\Form\Field\Ranges

Now, as we have defined the grid section, now we have to set the values which have to be dynamic. For this, we need to create a block file at
Excellence\Grid\Block\Adminhtml\Form\Field\TaxColumn

After That Just Run These Commands:
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean
php bin/magento cache:f
lush

Once done, you will be able to see the output like this.

colorpicker

Add ColorPicker In System Config Magento2

As a Developer, we all know we can edit color and code by just simply editing a file, but to increase the efficiency of our work and make things easier for admin, we can give a basic control to admin to change the color or front-end UI as per their need.

Example: if admin wants to change the color of any Background element on certain pages, so to select and apply the color we will give them a choice of his/her selection field to add update the color of the store, text, element, etc.

So, here we will discuss this Part Today that how we can add a color selection field in the Store configuration area.

Let’s start with the Basic Module Structure First.
Here I assume one should already Have Basic knowledge about Module and its structure, if not please refer to this link first.

Now, as we are now familiar with the Basic module structure Lets proceed Further.
Here I Have Taken Vendor Module as Excellence_ColorPicker.

We will provide the access or configuration in Store configuration, Section Which is at Stores -> Configuration (under settings).

For this, we need to create the instruction for configuring. So, we will create a system.xml file in our module first.

Excellence/ColorPicker/etc/adminhtml/system.xml

Now, after this we need to provide a color variation to the fields in the above system.xml configurations.

We need to create a Block file Color.php through which we will be using jquery color picker. In the code, you can see <frontend _model> it is required to add color picker in the field.

Excellence\ColorPicker\Block\Color

After That Just Run These Commands:
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean
php bin/magento cache:f
lush

Finally, we get this on admin End:

colorpicker

Well, All Done For This Here.
One can use this feature according to their need, fetch this color value, and use it as per their need.