Categories: How ToMagento 2

How to manage Custom amount with PayPal Express Checkout in Magento 2

Available in more than 200 markets & trusted by 200 million customers around the world, Paypal enables consumers and merchants to send & receive money in more than different currencies, PayPal is a worth considering option when it comes to choosing a payment Gateway for business. And, PayPal Express Checkout offered by PayPal allows the store customer to perform faster checkout in an ecommerce store with an enhanced layer of security. During the checkout, the customer is redirected to the secure PayPal webpage to complete the payment information. And once the transaction is completed, the customer is redirected back to your store. Adding PayPal Express Checkout to your Magento 2 store showcase the familiar PayPal button on your store, which has been reported to increase sales.
But several times it happens that while charging some extra fees on order doesn’t match with PayPal order total and it throws an error and doesn’t allow the customers to proceed further. To fix this issue, you need to manage this custom amount with PayPal Express Checkout by adding a piece of code in your Magento 2 store.
To do the same, first, we need to create an “events.xml” file inside our custom extension folder.
app\code\Vendor\Extension\etc\events.xml

<pre class="lang:default decode:true">
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
 <event name="payment_cart_collect_items_and_amounts">
        <observer name="vendor_extension_payment_collect_total" instance="Vendor\Extension\Observer\AddCustomAmountItem" />
    </event>
</config>
</pre>

Now, we need to create one more file, for adding a custom amount in Paypal Checkout and display in subtotal.
app\code\Vendor\Extension\Observer\AddCustomAmountItem.php

<pre class="lang:default decode:true">
<?php
namespace Vendor\Extension\Observer;
 
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\StoreManagerInterface;
 
class AddCustomAmountItem implements ObserverInterface
{
 public function execute(Observer $observer)
 {
     /** @var \Magento\Payment\Model\Cart $cart */     $cart = $observer->getEvent()->getCart();
        $customAmount = 100;
        $cart->addCustomItem(__('Custom Amount'), 1, -1.00 * $customAmount, 'customfee');
 }
}
</pre>

That’s it! You have successfully manage your custom amount with Paypal Express Checkout.

If you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends.

And, Let us know if you are facing an issue while implementing this code.

Happy Coding!

Click to rate this post!
[Total: 8 Average: 3.6]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • There's a manual amount 100 in $CustomAmount. How to pick variable(X) entered in Extra Fee Amount in STORES>CONFIGURATION>>MAGECOMP>>>EXTRAFEE AMOUNT = X ?

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

17 hours ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

17 hours ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

1 day ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

4 days ago

HYVĂ„ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

4 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

5 days ago