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:
<?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>
<?phpnamespace 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];}}