sivajik34 / Custom-Fee-Magento2

extra fee :Using this extension you can add custom extra fee such as insurance fee,delivery sign fee etc at checkout.
40 stars 27 forks source link

Custom Fee adding Twice. #6

Open ashokshrestha2012 opened 6 years ago

ashokshrestha2012 commented 6 years ago

In the backend, I have set the Custom Fee Amount for 10. Then I added product of value 100 and with shipping charge 5. In the checkout page, i have checked custom fee so that extra fee will be added in the order. But In payment page custom fee is adding twice. The total value should be 115, but displaying 125.When i remove the custom fee the calculation is fine.

dhrumin44 commented 6 years ago

Yes, I have the same issue but I found solution,

Update grandTotal in bellow files

app/code/Sivajik34/CustomFee/Model/Total/Fee.php

app/code/Sivajik34/CustomFee/Model/Quote/Total/Fee.php

 Change 
 $total->setGrandTotal($total->getGrandTotal() + $fee);
 $total->setBaseGrandTotal($total->getBaseGrandTotal() + $fee);

TO
 $total->setGrandTotal($total->getGrandTotal());
 $total->setBaseGrandTotal($total->getBaseGrandTotal());

because it's already update in this file,

app/code/Sivajik34/CustomFee/Plugin/Checkout/Model/ShippingInformationManagement.php

Clean cache and check, hope it helps you.

ketantambekar commented 5 years ago

I have checked your code and still double additional fees issue occur. Please let us know anything remaining from our end.

Thanks

bineshdobhal commented 5 years ago

Delete the cart items and add items again and check.

nitaipanda commented 5 years ago

PayPal gateway has rejected request. The totals of the cart item amounts do not match order amounts (#10413: Transaction refused because of an invalid argument. See additional error messages for details).

dhrumin44 commented 5 years ago

Hello @nitaipanda It's magento paypal default issue, paypal doesn't support extra cost in cart, this module added additional fee as your requirement.

Now if in an order you added some custom fee, and want to pay the payment by magento’s default paypal payment method, then it throws the error that, amount is not same as order amount.

solve this issue you have to manage that custom feefor the paypal process below step.

Add an observer in your module:

file path: app/code/Sivajik34/CustomFee/etc/events.xml

<?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="Sivajik34_CustomFee_payment_collect_total" instance="Sivajik34\CustomFee\Observer\AddCustomAmountItem" />
    </event>
</config>

Now define this observer, at path:

app/code/Sivajik34/CustomFee/Observer/AddCustomAmountItem.php

<?php

namespace Sivajik34\CustomFee\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
 * Add Weee item to Payment Cart amount.
 */
class AddCustomAmountItem implements ObserverInterface
{
    /**
     * Add custom amount as custom item to payment cart totals.
     *
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        /** @var \Magento\Payment\Model\Cart $cart */
        $cart = $observer->getEvent()->getCart();
        $customAmount = 246;
        $cart->addCustomItem(__('Custom Fee'), 1, -1.00 * $customAmount, 'customfee');
    }
}

Here, addCustomItem() function is added your custom amount field in cart, and then paypal manages the amount as well.

in this, first parameter is, the field name, i.e. amount field name.

second parameter is, quantity for the field.

third parameter is, amount, which you want to add, in example i added (-1.00 *), because i want to pass negative value.

forth parameter is, identifier, i.e. id of your custom amount field.

After this code your order will get placed using paypal payment method without any error.

Hope this solution will help you.