0

I am using the below code to get Sub Total and Grand Total but it is returning total in Display Currency.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 
 
$subTotal = $cart->getQuote()->getSubtotal();
$grandTotal = $cart->getQuote()->getGrandTotal();

How to get the grand Total in Base Currency in case Default Currency is different than Display Currency.

I am able to get the Base Currency Total by below code, but it is not exactly the same as displayed on "You will be charged for" on the payment page.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 

$grandTotal = $cart->getQuote()->getGrandTotal();

$priceCurrencyFactory = $objectManager->get('Magento\Directory\Model\CurrencyFactory');
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
 
$currencyCodeTo = $storeManager->getStore()->getCurrentCurrency()->getCode();
$currencyCodeFrom = $storeManager->getStore()->getBaseCurrency()->getCode();

$rate = $priceCurrencyFactory->create()->load($currencyCodeTo)->getAnyRate($currencyCodeFrom);
$baseTotal = $grandTotal * $rate;

Difference of value is shown in image.

enter image description here

1 Answer 1

0
 public function convertToBaseCurrency($price)
        {
     $Value = $price * $rate;
    
    //   step1 : get currenct currency
         
            $currentCurrencyCode = $this->storeManager
                                    ->getStore()
                                    ->getCurrentCurrency()
                                    ->getCode();
   // Step 2 : get base currency
            $baseCurrencyCode = $this->storeManager
                                 ->getStore()
                                 ->getBaseCurrency()
                                 ->getCode();
   // Step 3 : get rate
            $currencyRate= $this->currencyFactory
                         ->create()
                         ->load($currentCurrencyCode)
                         ->getAnyRate($baseCurrencyCode);
  //  step 4 : get value
            $Value = $price * $currencyRate;
    
            return $Value;
        }
7
  • Check my updated question and I am getting base total through this code but it has some difference of value, when I compare it with You will be charged for value on payment page
    – ishaq
    Commented Nov 25, 2021 at 11:13
  • what's the difference. Share the associated values. It should be more related to some configuration and not a code issue. Commented Nov 25, 2021 at 18:11
  • plz check this image: imgur.com/a/ADNpsla
    – ishaq
    Commented Nov 25, 2021 at 18:30
  • This is due to rounding effect. Commented Nov 26, 2021 at 13:18
  • your rates are 99.7766 and due to rounding its taking a lesser value. hence there is that amount different. In your estimate code you need to applying rounding to 2 decimals. Commented Nov 26, 2021 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.