Magento 2 Extra fee

share link

by mariaparker1453@gmail.com dot icon Updated: Sep 11, 2023

technology logo
technology logo

Guide Kit Guide Kit  

There are two ways to add a fee to order totals in Magento 2: Using the Magento 2 Extra Fee extension


The Magento 2 Extra Fee extension is a free and open-source extension that allows you to add unlimited fees to your products or orders. You can set the fee to be a fixed amount or a percentage, and you can also set conditions for when the fee should be applied.


To add a fee to order totals using the Magento 2 Extra Fee extension, follow these steps:


1. Install the Magento 2 Extra Fee extension.

2. Go to Stores > Configuration > Sales > Extra Fee.

3. Click Add New Fee.

4. In the Fee Name field, enter a name for the fee.

5. In the Fee Type field, select the type of fee you want to add.

6. In the Fee Amount field, enter the amount of the fee.

7. In the Fee Calculation Method field, select how the fee should be calculated.

8. In the Fee Display field, select where you want the fee to be displayed.

9. Click Save.


The fee will now be added to all orders.


Using custom code


You can also add a fee to order totals using custom code. To do this, you will need to create a new file in the `app/code/Vendor/Module/etc/di.xml` directory. In this file, you will need to define a new `custom_fee` service. The service should have the following attributes:


* `name`: The name of the fee.

* `type`: The type of fee, which can be either `fixed` or `percentage`.

* `amount`: The amount of the fee.

* `calculation_method`: The method used to calculate the fee.

* `display_location`: Where the fee should be displayed.


Once you have defined the `custom_fee` service, you will need to inject it into the `Magento\Checkout\Model\Cart` class. To do this, you will need to modify the `etc/config.xml` file. In this file, you will need to add the following code to the `default` section:


```

<preference for="Magento\Checkout\Model\Cart" type="Vendor\Module\Model\Cart" />

```


Once you have made these changes, you will need to redeploy your Magento store. After the store has been redeployed, the fee will be added to all orders.


Here are some additional tips for adding a fee to order totals in Magento 2:


* Be clear about the fee that you are charging. Make sure that customers are aware of the fees before they make a purchase.

* Set reasonable fees. Don't overcharge your customers for the extra services that you offer.

* Communicate the fees to your customers. Let customers know about the fees when they check out.

* Track your fees. Keep track of the fees that you are charging so that you can ensure that they are being applied correctly.


I hope this helps!