How to Pre-select Default Payment Method at the Checkout Page in Magento 2? - Magento source24


Hello magento friends todays in this blog post im share with you to How to Pre-select Default Payment Method at the Checkout Page in Magento 2. In Magento 2, you can pre-select a default payment method at the checkout page by customizing the code. By default, Magento does not have a built-in feature to do this through the admin panel, so some coding is required. Here's a step-by-step guide on how to achieve this:


Please note that modifying core files directly is not recommended in Magento as it can lead to issues during updates. Instead, you should create a custom module to implement this functionality. I'll outline the steps below:


➤ Create a custom module:

First, you need to create a custom module. You can do this by following these steps:

a. Create the folder structure:

In the app/code directory of your Magento installation, create the following folders: VendorName and ModuleName.


b. Create the module registration file:

Inside the app/code/VendorName/ModuleName folder, create the registration.php file with the following content:


How to Pre-select Default Payment Method at the Checkout Page in Magento 2? - Magento source24


c. Create the module configuration file:

Create the module.xml file inside app/code/VendorName/ModuleName/etc with the following content:


How to Pre-select Default Payment Method at the Checkout Page in Magento 2? - Magento source24


➤ Create a plugin for the Payment Method model:
We'll use a plugin to set the default payment method at the checkout. Create the di.xml file inside app/code/VendorName/ModuleName/etc/frontend with the following content:  


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Model\PaymentInformationManagement">
        <plugin name="preselect_default_payment" type="VendorName\ModuleName\Plugin\Checkout\Model\PaymentInformationManagementPlugin" sortOrder="1"/>
    </type>
</config>


➤ Create the plugin class:

Create the file PaymentInformationManagementPlugin.php inside app/code/VendorName/ModuleName/Plugin/Checkout/Model with the following content:


<?php

namespace VendorName\ModuleName\Plugin\Checkout\Model;

use Magento\Checkout\Model\PaymentInformationManagement as Target;
use Magento\Quote\Api\Data\PaymentInterface;

class PaymentInformationManagementPlugin
{
    /**
     * @param Target $subject
     * @param int $cartId
     * @param PaymentInterface $paymentMethod
     * @return array
     */
    public function beforeSavePaymentInformationAndPlaceOrder(
        Target $subject,
        $cartId,
        PaymentInterface $paymentMethod
    ) {
        // Set the payment method you want to pre-select
        $paymentMethod->setMethod('your_default_payment_method_code');

        return [$cartId, $paymentMethod];
    }
}

 

Replace 'your_default_payment_method_code' with the actual code of the payment method you want to pre-select. You can find the payment method code by inspecting the form elements on the checkout page.

Enable the module:
Finally, enable the custom module by running the following Magento CLI commands:


How to Pre-select Default Payment Method at the Checkout Page in Magento 2? - Magento source24





Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post