How to Configure Magento 2 Add Extra Fee in Checkout

How To Configure Magento 2 Add Extra Fee In Checkout?

by admin

If you are looking for a way to configure Magento 2 Add Extra Fee In Checkout in your store, this article is the answer you need. We will show you two solutions for adding custom charges on the checkout page. One is to use code. The other is to use third-party extensions.

Let’s get going!

>>> EXPLORE HERE 10 Vital Practices Of Magento 2 Checkout!

Why Do You Need To Add Extra Fee in Magento Checkout?

extra-fee-checkout-magento-2

Adding extra charges is an extremely useful feature to store owners, especially when they provide additional services (such as one-day delivery, gift wrapping, extended warranty, surcharge, etc.) in their store. Unfortunately, Magento only supports several fees by default, like free shipping, table rate, or a flat rate. Because of that, when an additional fee is incurred, store owners usually have to add it directly to product prices.

There are two problems with this solution:

  • Customers might not be aware of it.
  • It would increase the product’s price significantly, which usually increases the shopping cart abandonment rate.

That is why Magento 2 Add Extra Fee in Checkout really is a beneficial feature. With this function, you can add extra fees to the checkout so your customers will be more aware.

And you can also display those fees on the storefront when customers add items to their shopping cart, which is really convenient for customers when shopping on your website.

>>> If you need a full Magento guide from scratch, let’s explore this post: [2024 Update] Magento 2 Tutorial for Developers & Merchants

2 Ways to Magento 2 Add Extra Fee in Checkout

Configure Magento 2 Add Extra Fee Programmatically in Checkout

If you are familiar with coding and want to find a free solution, you can configure Magento 2 Add Extra Fee in Checkout programmatically.

First, you create sales.xml in your module etc. folder.

<?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>

Now, define app\code\Webkul\Test\Model\Total\Customfee.php.

Here you can enable updating your total according to your custom 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');
    }
}

And that’s it. With only two steps, you have added a custom fee to your checkout page.

Magento 2 Extensions To Add Extra Fee In Checkout

If you don’t have any coding knowledge, it would be better to use a third-party extension.

Currently, in the market, there are a few Magento eCommerce extensions that can help you add the custom fee to checkout:

  • Extra Fee for Magento 2 by Amasty
  • Magento 2 Extra Fee by Meetanshi
  • Extra Fee in Magento 2 by MageAnts
  • Add Extra Fee by Mageprince

>>> Explore this post for full reviews of the top 7 best Magento 2 extra fee extensions in the marketplace.

Conclusion

In this article, we have shown you two different solutions for Magento 2 Add Extra Fee in Checkout. You can choose the most suitable solution to add an extra fee to your checkout. Additionally, BSS Commerce provides a Magento plugin store and Magento support services to address all your problems and set your eCommerce store apart from competitors. CONTACT US NOW!

>>> Read our latest post: How to Solve Magento 2 product images not showing in frontend or backend

Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.