Efficiently Adding Extra Fees to Magento 2 Orders and Checkout: A Step-by-Step Guide

Efficiently Adding Extra Fees to Magento 2 Orders and Checkout: A Step-by-Step Guide

Magento 2 is a powerful and flexible e-commerce platform, but sometimes you need to implement additional features to tailor it to your specific business needs. One common requirement is adding extra fees to orders and during checkout. This can be useful for a variety of reasons—such as handling special shipping costs, extra handling fees, or administrative charges. In this guide, we’ll walk you through the process of efficiently adding extra fees to Magento 2 orders and checkout.

1. Understanding the Requirements

Before diving into implementation, it’s important to clearly define why and how you want to add extra fees. Some common scenarios include:

  • Shipping Surcharges: Additional charges for expedited shipping or specific delivery areas.
  • Handling Fees: Fees for special packaging or handling.
  • Administrative Fees: Charges for processing or other administrative tasks.

2. Choose the Method for Adding Fees

There are several ways to add extra fees in Magento 2. The method you choose will depend on your requirements and technical proficiency. Here are the common methods:

  • Custom Module: Creating a custom module gives you the most flexibility but requires coding.
  • Magento 2 Extensions: There are many third-party extensions available that can handle fee addition.
  • Cart Price Rules: Useful for adding fees based on certain conditions.

In this guide, we’ll focus on creating a custom module as it provides the most control and flexibility.

3. Creating a Custom Module

3.1. Set Up the Module Structure

First, create the necessary directories for your custom module. Assuming your module is named ExtraFees, follow these steps:

  1. Create Module Directory: Navigate to app/code and create a directory named ExtraFees.
  2. Create Module Files:
  • app/code/ExtraFees/ExtraFees/registration.php
  • app/code/ExtraFees/ExtraFees/etc/module.xml
  • app/code/ExtraFees/ExtraFees/etc/frontend/di.xml

3.2. Define Module Registration

In registration.php, add the following code to register your module with Magento:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'ExtraFees_ExtraFees',
    __DIR__
);

3.3. Configure Module XML

In module.xml, define your module:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="ExtraFees_ExtraFees" setup_version="1.0.0"/>
</config>

3.4. Define Dependency Injection

In di.xml, specify how you’ll modify the checkout process:

<?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\Cart">
        <plugin name="extra_fees_cart_plugin" type="ExtraFees\ExtraFees\Plugin\CartPlugin"/>
    </type>
</config>

3.5. Implement the Plugin

Create CartPlugin.php in Plugin directory to add extra fees:

<?php
namespace ExtraFees\ExtraFees\Plugin;

class CartPlugin
{
    public function beforeCollectTotals(\Magento\Checkout\Model\Cart $subject)
    {
        $quote = $subject->getQuote();
        $quote->setSubtotal($quote->getSubtotal() + 10); // Adds $10 extra fee
        $quote->setGrandTotal($quote->getGrandTotal() + 10); // Updates grand total
    }
}

4. Testing Your Implementation

After setting up your module, it’s crucial to test it thoroughly:

  1. Enable Module: Run php bin/magento module:enable ExtraFees_ExtraFees.
  2. Upgrade and Clear Cache: Execute php bin/magento setup:upgrade and php bin/magento cache:clean.
  3. Check Frontend: Add items to your cart and ensure the extra fee is correctly applied.

5. Utilizing Magento 2 Extensions

If custom development is not ideal for you, consider using existing Magento 2 extensions. Many extensions offer configurable options to add extra fees based on conditions you define. Some popular options include:

  • Mageplaza Extra Fee: Allows adding fees to the cart, checkout, and order.
  • Amasty Fee Extension: Provides a range of fee addition capabilities, including conditional fees.

6. Conclusion

Adding extra fees to Magento 2 orders and checkout can be a straightforward process with the right approach. Whether you opt for a custom module or a third-party extension, ensure that the solution you choose meets your business requirements and integrates seamlessly with your existing Magento setup. By following this guide, you can efficiently manage extra fees and enhance the functionality of your Magento 2 store.

Andrew Row

Leave a Reply

Your email address will not be published. Required fields are marked *