Solving Magento Shipping Rate Issues in One Page Checkout for COD
Introduction
In Magento, changing the shipping rate cost to “0” when Cash on Delivery (COD) is selected as a payment method during one page checkout can be achieved through event observers. In this post, we’ll guide you step-by-step through the process, explaining which events to observe and providing practical code examples.
Understanding the Checkout Process
Before diving into the solution, it’s essential to understand how Magento’s one page checkout works:
- The user selects a shipping method.
- The selected method is saved.
- The totals are collected and updated.
- The payment method is saved.
Observing the Correct Event
To modify the shipping rate based on the chosen payment method, you should observe an event that occurs after the payment method has been saved but before the totals are collected for the final order summary review.
The checkout_controller_onepage_save_payment event fits this description perfectly. It is dispatched when a customer submits the payment information in the checkout process.
Implementing the Observer
Create a new file named COD_Shipping_Observer.php in your custom module and implement the observer as follows:
namespace YourNamespace\YourModule\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\Http;
use Magento\Quote\Model\QuoteFactory;
use Magento\Checkout\Model\Session;
class CODShippingObserver implements ObserverInterface
{
protected $quoteFactory;
protected $checkoutSession;
protected $request;
public function __construct(
QuoteFactory $quoteFactory,
Session $checkoutSession,
Http $request
) {
$this->quoteFactory = $quoteFactory;
$this->checkoutSession = $checkoutSession;
$this->request = $request;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
// Get the current quote
$quote = $this->checkoutSession->getQuote();
// Check if COD is selected as the payment method
if ($this->request->getPost('payment_method') === 'cashondelivery') {
// Set the shipping rate to 0
$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setShippingAmount(0);
$shippingAddress->setBaseShippingAmount(0);
// Collect totals to update the grand total
$quote->collectTotals();
// Save the quote with the updated shipping rate
$quote->save();
}
}
}
In this observer, we check if the payment method selected is COD. If it is, we set the shipping amount and base shipping amount to 0 for the current quote’s shipping address. We then collect the totals to ensure the grand total reflects the updated shipping rate.
Configuring the Event Observer
To make your module work with this observer:
- Declare a new configuration XML file named
events.xmlin the<your_module_path>/etcdirectory:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/config.xsd">
<event name="checkout_controller_onepage_save_payment_after">
<observer name="cods_shipping_observer" instance="YourNamespace\YourModule\Observer\CODShippingObserver" />
</event>
</config>
- Enable your module and clear the Magento cache.
Conclusion
By observing the checkout_controller_onepage_save_payment event, you can effectively change the shipping rate to 0 when Cash on Delivery is selected as the payment method during one page checkout in Magento.
This approach provides a clean and maintainable solution that fits seamlessly into the existing checkout process without requiring any core file modifications or additional external API requests.
Remember to replace YourNamespace and YourModule with your actual namespace and module name. Also, ensure you follow proper naming conventions for files and classes in your custom module.
For more advanced customizations and in-depth guides on Magento development, check out our comprehensive tutorials at Laravel Company. We cover everything from basic configurations to complex extension development, ensuring you have the resources needed to build robust and efficient Magento solutions.