How to configure Extra Fee in Magento 2
Vinh Jacker | 11-11-2024
The Magento 2 extra fee extension enables e-commerce businesses to apply additional charges for services like expedited shipping, gift wrapping, installation fees, and more.
If you want to add extra fees at checkout in Magento 2, this article provides two solutions: adding custom charges through code or using third-party extensions.
Why Need to Add Extra Fee at Magento Checkout?
Adding extra charges is a valuable feature for store owners who offer additional services, such as one-day delivery or gift wrapping. However, Magento only supports a few default fees, like free shipping or flat rates, often leading store owners to include these charges in product prices.
This approach has two significant drawbacks:
- Customers may not be aware of the additional costs
- It can significantly raise product prices, increasing the likelihood of shopping cart abandonment.
The Magento 2 Add Extra Fee in Checkout feature addresses these issues by allowing store owners to add extra fees during checkout, making customers more aware of them. It also displays these fees on the storefront when items are added to the cart, enhancing the shopping experience.
2 Methods to Add Extra Fee at Checkout in Magento 2
Method 1: Add Extra Fee Programmatically
If you have coding experience and prefer a free solution, you can customize Magento 2 to add an extra fee at checkout by following these steps:
Step 1: Create a new file named sales.xml
in the etc
folder of your Magento module.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd">
<section name="quote">
<group name="totals">
<item name="customfee" instance="Webkul\Test\Model\Total\Customfee" sort_order="150"/>
</group>
</section>
</config>
Step 2: Create the Customfee.php
file within the app/code/Webkul/Test/Model/Total
directory.
This file will contain the PHP code that calculates and applies the extra fee to the order total.
Step 3: Update the total according to custom fee
In this step, you’ll set up your Magento configuration to automatically update the order total based on your defined extra fee.
class Customfee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
/**
* Collect grand total address amount
*
* @param \Magento\Quote\Model\Quote $quote
* @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
* @param \Magento\Quote\Model\Quote\Address\Total $total
* @return $this
*/
protected $quoteValidator = null;
public function __construct(\Magento\Quote\Model\QuoteValidator $quoteValidator)
{
$this->quoteValidator = $quoteValidator;
}
public function collect(
\Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
\Magento\Quote\Model\Quote\Address\Total $total
) {
parent::collect($quote, $shippingAssignment, $total);
$exist_amount = 0; //$quote->getCustomfee();
$customfee = 100; //enter amount which you want to set
$balance = $customfee - $exist_amount;//final amount
$total->setTotalAmount('customfee', $balance);
$total->setBaseTotalAmount('customfee', $balance);
$total->setCustomfee($balance);
$total->setBaseCustomfee($balance);
$total->setGrandTotal($total->getGrandTotal() + $balance);
$total->setBaseGrandTotal($total->getBaseGrandTotal() + $balance);
return $this;
}
protected function clearValues(Address\Total $total)
{
$total->setTotalAmount('subtotal', 0);
$total->setBaseTotalAmount('subtotal', 0);
$total->setTotalAmount('tax', 0);
$total->setBaseTotalAmount('tax', 0);
$total->setTotalAmount('discount_tax_compensation', 0);
$total->setBaseTotalAmount('discount_tax_compensation', 0);
$total->setTotalAmount('shipping_discount_tax_compensation', 0);
$total->setBaseTotalAmount('shipping_discount_tax_compensation', 0);
$total->setSubtotalInclTax(0);
$total->setBaseSubtotalInclTax(0);
}
/**
* @param \Magento\Quote\Model\Quote $quote
* @param Address\Total $total
* @return array|null
*/
/**
* Assign subtotal amount and label to address object
*
* @param \Magento\Quote\Model\Quote $quote
* @param Address\Total $total
* @return array
*/
public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
{
return [
'code' => 'customfee',
'title' => 'Custom Fee',
'value' => 100
];
}
/**
* Get Subtotal label
*
* @return \Magento\Framework\Phrase
*/
public function getLabel()
{
return __('Custom Fee');
}
}
Following these two simple steps, you’ve successfully added a custom fee to your Magento 2 checkout page.
Method 2: Add Extra Fee using Magento 2 Extension
If you don’t have coding experience, using a third-party extension is best. Fortunately, several Magento eCommerce extensions can help you add custom fees to the checkout process. Now, we will guide you on how to add an extra fee with the Extra Fee extension by Mageplaza in a very simple way.
Diversify extra services to your Magento 2 checkout page with the Extra Fee extension
Check it out!Step 1: Enter the full information in the General section
- Navigate to
Mageplaza > Extra Fee > Manage Rules > Add New Rule
- Name: Enter a descriptive name for this extra fee rule.
- Status: Select “Enabled” to activate this rule and apply the extra fee to orders.
- Description: Provide a brief explanation of the extra fee, such as its purpose or the criteria for applying it.
- Store Views: Choose which specific stores should have this extra fee applied. You can select multiple stores.
- Customer Groups: Determine which customer groups will see this extra fee. You can select multiple groups.
- Priority: Set the order in which this rule should be applied if multiple rules are active. A higher priority means the rule will be applied first.
- Message title: Enter messages/ reason for this extra fee
- Allow customers notes: Select yes to show the message section
Step 2: Enter the full information in the Condition section
Here, you can customize the conditions for different sections like Cart, Product, Customer, and more. For example, you can set rules based on the total amount spent, the number of items in the cart, product availability, customer details, and other factors.
Sep 3. Enter information in the Action section
There are 2 options to choose Apply Type: Automatic and Manual.
Option 1: Apply Type = Automatic
- Apply for: If you choose Each Product in Cart instead of Whole Cart, you need to enter the conditions based on product attributes like color, climate, and category.
- Fee Type: Choose the service Fee Type
Percentage of Cart Total: The fee will be a percentage of the total cart value. Fixed Amount for the Whole Cart: The fee will be a fixed amount applied to the entire cart when customers make a purchase. Fixed Amount for Each Item: The fee will be a fixed amount applied to each item in the cart.
-
Amount: Enter the amount or percentage based on the payment type you select
-
Enter the remaining fields: Tax, sort order, and refundable.
Option 2: Apply Type = Manual
-
Apply for: Configure similar to the Automatic option
-
Display Area: There are three options to choose from: Payment Method, Shipping Method, and Cart Summary.
-
Display type:
Checkbox: Allows a user to choose one or more options from a list of fees. Each option can be chosen independently or deselected. Radio: Allows customers to select only one option from a list of fees. Once an option is selected, the other options become deselected.
- Configure the remaining fields in a way that is similar to the Automatic Option.
Step 4. Config Label & Options (for Apply Type = Manual)
Unlike the automatic option, with the manual option, you need to configure the Manage Label and Options section.
- Click “Add option” to add unlimited fees and enter all needed information for each fee type.
- Click “Save” to finish.
Congratulations! You have completed all steps to add an extra fee.
Final Words
To provide the most relevant advice, please share more details about the extra fee you want to add. Specifically, please indicate the nature of the fee, the conditions under which it should be applied, any existing Magento customizations or extensions that might influence the implementation, and your level of technical familiarity with Magento. This information will help me tailor my response to your unique needs.