Setting Credit Card as the Default Payment Method in Magento Checkout
In Magento, when you have multiple payment methods enabled for your store, customers are presented with a selection of available options during checkout. By default, no method is preselected. In this guide, we’ll show you how to make the Credit Card payment method always selected and open initially.
Understanding the Default Behavior
By default in Magento 2.x, when multiple payment methods are enabled (like PayPal, Braintree, or Authorize.net), customers see a list of options during checkout. The first one is not preselected; the customer must manually choose their preferred method before proceeding.
This default behavior is designed to provide flexibility for your customers. However, there may be cases where you want to enforce a specific payment method, especially if it’s the only one available or the most common option used by your customers.
Changing the Default Payment Method
To set Credit Card as the default and initially open method, we need to modify the checkout form and the payment methods configuration. Here are the steps:
- Configure the Payment Methods
First, ensure that you have the Credit Card method enabled in your Magento admin panel. Navigate to Stores > Settings > Configuration > Sales > Payment Methods.
Locate the Credit Card section (assuming you’re using a standard credit card module). Set the Enable field to Yes and save the configuration.
- Modify the Checkout Form
The default checkout form is rendered using a template file located at vendor/magento/module-checkout/view/frontend/templates/summary/available.phtml. This file contains the logic for displaying the available payment methods.
To make Credit Card the initial selection, we need to modify this file or create an override in your theme.
- Open the original file and look for the following code snippet:
<?php foreach ($methods as $method): ?>
<dt>
<input type="radio" name="payment_method"
id="payment_method_<?= /* @escapeNotVerified */ $method->getCode() ?>"
value="<?= /* @escapeNotVerified */ $method->getCode() ?>"
data-method="<?= /* @escapeNotVerified */ $method->getCode() ?>"
class="radio"
<?php if ($method->getCode() == $quote->getPayment()->getMethod()
|| $method->getCode() == $quote->getPayment()->getAdditionalInformation('last_real_method')): ?>
checked
<?php endif; ?>
/>
<label for="payment_method_<?= /* @escapeNotVerified */ $method->getCode() ?>"
class="<?= /* @escapeNotVerified */ $method->getCode() ?> payment-method-label">
<?= /* @escapeNotVerified */ $method->getTitle() ?>
</label>
</dt>
<dd><?= /* @escapeNotVerified */ $method->getDescriptionIfAvailable() ?></dd>
<?php endforeach; ?>
- Add the Override
Create a new file named available.phtml in your theme’s checkout/summary directory (e.g., app/design/frontend/Vendor/Theme/Magento_Checkout/templates/summary/available.phtml).
Copy and paste the code snippet from the original file into this new one. Modify the checked attribute condition to always be true for the Credit Card method:
<?= /* @escapeNotVerified */ $method->getCode() ?>"
value="<?= /* @escapeNotVerified */ $method->getCode() ?>"
data-method="<?= /* @escapeNotVerified */ $method->getCode() ?>"
class="radio"
checked
/>
Now, when a customer visits the checkout page and multiple payment methods are available, Credit Card will be preselected and displayed as the default option.
Conclusion
Setting a default payment method is a straightforward process in Magento. By understanding the structure of the checkout form and applying simple template modifications, you can enforce specific payment methods for your customers, improving their shopping experience while reducing cognitive load during the purchasing process.
Remember to test this change thoroughly after deployment to ensure it works as expected across different browsers and devices. If you encounter any issues or need further assistance with customizing the checkout process, feel free to reach out to our expert Magento development team at Laravel Company.
Happy coding!