How to configure and pre-select default payment method in Magento 2 checkout
In this article we will explore how to create a module that allows you to configure and pre-select any payment method in Magento 2 checkout. Often times it is important to make sure a method is selected as soon as checkout is loaded, for example, a preferable choice or simply selecting the only available method - the use cases are endless. It is also useful to allow changing the default method for store administrators.
In order to achieve this we will create new system configuration where you will be able to select the default payment method from admin. In addition we will extend the default checkout functionality to select the configured payment method by passing in data using checkout config provider.
Lets get started. We are going to be working on Magento 2.4 however the code in this article would work with other Magento 2 versions as well.
Firstly you will need to create a custom module but we assume you are already familiar with that. Once you have that lets start by defining our system configuration. We will create a new tab and have a single field which will allow us to select the payment method we want to set as default. We need to declare the configuration file in etc/adminhtml/system.xml:
<?xml version="1.0" ?>
<!--
~ * @category Techflarestudio
~ * @author Wasalu Duckworth
~ * @copyright Copyright (c) 2021 Techflarestudio, Ltd. (https://techflarestudio.com)
~ * @license http://opensource.org/licenses/OSL-3.0 The Open Software License 3.0 (OSL-3.0)
~
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="techflarestudio" translate="label" class="techflarestudio_checkout_configuration" sortOrder="10">
<label>Custom Checkout Configuration</label>
</tab>
<section id="techflarestudio_checkout" showInDefault="1" showInWebsite="0" showInStore="1" sortOrder="10" translate="label">
<label>Checkout</label>
<tab>techflarestudio</tab>
<resource>Magento_Checkout::checkout</resource>
<group id="payment" translate="label comment" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Payment step</label>
<comment>Custom configuration regarding payment step.</comment>
<field id="default_method" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Default Payment Method</label>
<source_model>\Techflarestudio\DefaultPayment\Model\Config\Source\PaymentOptions</source_model>
</field>
</group>
</section>
</system>
</config>
A few things to mention here:
- We define a new tab techflarestudio which will appear in the configuration sidebar
- We define a new section techflarestudio_checkout which will appear as a subsection in the sidebar
- We define a new group for payment configuration and add a single select field default_method
- We define a custom source model for the field - \Techflarestudio\DefaultPayment\Model\Config\Source\PaymentOptions. This will allow us to pass in the available payment methods to the field options.
The next step is to create the source model we have already defined for our field. Here is the code for it:
We are extending \Magento\Framework\Data\OptionSourceInterface to define the source model list. This is the new preferred method as the Magento\Framework\Option\ArrayInterface is now deprecated. The new interface requires only a single method toOptionArray where we need to return the list of options in an array with each entry consisting of label -> value pair.
In order to fetch the available payment methods we are using the PaymentMethodListInterface which allows us to fetch the available payment methods per store view. To get the current configuration scope we are fetching the store parameter from the request. Once we have the list we prepare the data in the required format and that is that. Make sure to enable the module, clear the cache and you should be able to see the new configuration in the admin:
Once we have the ability to save our preferred default payment method we should make it possible to fetch this value in other components. The standard practice here is to create a help which allows to abstract from direct configuration calls. With a single configuration field the approach is a bit of an overkill however this does pay off once you start adding more fields and more logic.
In order to create the helper we need to define a new file in Helper/Data.php and make sure to extend the Magento\Framework\App\Helper\AbstractHelper. In addition we will need to fetch values from configuration for which we can use Magento\Store\Model\ScopeInterface which allows us to fetch configuration values per store view. Here is the helper:
We define our payment configuration path as a constant. Once we start adding new fields we can partially automate the paths by making a separate function for each system.xml group and only specifying the field id. However since we only have a single field this will do just fine.
Ok, now we have the ability to define a default payment method in admin and we have a helper class which we can use to fetch the data whenever we need. Next thing we will need is to make sure the default payment method is available in the checkout config. It will be accessible in window.checkoutConfig object. To do this first we need to define a new configProvider. We can do this by injecting a new argument in CompositeConfigProvider.
We also need to define the custom config provider class.
Two things we are doing here:
- We are fetching the default payment configuration value for the current store view by using the helper class we defined earlier
- We are passing in the payment method to a new config key default_payment_method. We will use this later to select the default payment method.
Final step is to create functionality that will allow us to select the payment method by default. For this we can create a mixin for Magento_Checkout/js/model/checkout-data-resolver. First declare the mixin in your requirejs-config.js file:
Once we have defined our mixin we need to implement the class and add additional logic to resolvePaymentMethod function. Here is the code:
The relevant code here has to do with paymentMethod variable. We want to make sure that an already selected payment method stays selected after reload or checkout step change - therefore we are first checking if a method has already been selected and if it has not we select the configured default method by accessing window.checkoutConfig.default_payment_method.
That is it. If you followed everything correctly, deploy the new code, clear cache and go to checkout. Once visiting payment step you should be able to see that the default method has been selected by default:
Let us know if you have any comments or would like to request a tutorial about any other common feature you are wondering about.