Setting the Default Payment Method Radio Button in Magento
Hello fellow developers! Today, we’re going to tackle a common question that many of you might be facing while working on your Magento projects - how to set the default payment method radio button.
Understanding the Problem
Our user faced an issue where the PayPal radio button was always selected by default, even though they had configured the credit card option to appear first in the checkout process. The methods.phtml file is responsible for rendering the payment method list, but changing the order of elements did not solve the problem.
The Solution: Modifying the Payment Method Block
To resolve this issue, we need to modify the way Magento renders and selects the default payment method. This involves editing the layout XML files and adding a new attribute to set the selected payment method.
Step 1: Add the Selected Attribute to the Layout File
Open your theme’s checkout_onepage_paymentmethods.phtml file located under app/design/frontend/{your_vendor}/{your_theme}/template/checkout/. If you’re working with Magento 2, this file is usually found at vendor/magento/module-checkout/view/frontend/templates/payment/methods.phtml.
In the <referenceContainer name="payment.methods"> section, add a new attribute called selected-method and set its value to your desired payment method code:
<referenceContainer name="payment.methods">
<container name="payment_methods" as="methods">
<!-- Add this line -->
<argument name="selected-method" xsi:type="string">your_payment_method_code</argument>
<block class="Magento\Payment\Block\Form\Methods" template="Magento_Payment::methods.phtml"/>
</container>
</referenceContainer>
Replace your_payment_method_code with the actual code of your desired payment method. For example, if you want to set credit card as the default method, use paypal_standard.
Step 2: Update the Payment Methods Template
Next, we need to update the methods.phtml file to apply the selected attribute.
Open the methods.phtml file located under app/design/frontend/{your_vendor}/{your_theme}/template/payment/ or in your Magento 2 theme directory.
Find the section where the payment method options are rendered using a foreach loop. Inside this loop, add an if statement to check for the selected attribute and set it accordingly:
<?php foreach ($methods as $method): ?>
<li class="item item-method-<?= $method->getId() ?>">
<?php if ($method->getCode() === $selectedMethod): ?>
<input type="radio" id="<?= $method->getId(); ?>" name="payment[method]" value="<?= $method->getId(); ?>"
checked="checked"/>
<?php else: ?>
<input type="radio" id="<?= $method->getId(); ?>" name="payment[method]" value="<?= $method->getId(); ?>"/>
<?php endif; ?>
<label for="<?= $method->getId(); ?>">
<?= $method->translateGetTitle() ?>
</label>
</li>
<?php endforeach; ?>
The checked="checked" attribute is added when the current payment method matches the selected method.
Conclusion
By following these steps, you should now be able to set a default payment method for your Magento store. Remember that the code examples provided assume you’re using a custom theme and have access to the necessary files.
Don’t forget to flush your cache after making changes in layout XML files:
php bin/magento cache:flush
If you still encounter any issues or need further assistance with your Magento development, feel free to reach out to our team at Laravel Company. We’re always here to help!