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?

extra-fee-checkout-magento-2

Adding extra charge 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, the 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 are really convenient for customers when shopping on your website.

Configure Magento 2 Add Extra Fee In Checkout Programmatically

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.

Magento 2 Extra Fee extension - Add Extra Fee in Order – Mageplaza

Currently, in the market, there are a few 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

READ MORE to know if you should or should not use Magento 2 One Step Checkout!

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.

We hope this is helpful and good luck to you!

BSS Commerce is one of the leading Magento extension providers and web development services in the world. With experienced and certified Magento developers, we commit to bringing high-quality products and services to optimize your business effectively. Furthermore, we offer FREE Installation – FREE 1-year Support and FREE Lifetime Update for every Magento extension.

CONTACT NOW to let us know your problems. We are willing to support you every time.

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.