-1

I am modifying the order using aroundPlaceOrder Hook, in same hook I am trying to update the totals .My currency selection is INR so I want to convert it to base currency and update the value.

        $address->setBaseSubtotal($finalPrice);
        $address->setSubtotal($finalPrice);
        $address->setDiscountAmount($discount);
        $address->setTaxAmount($tax);
        $address->setBaseTaxAmount($tax);
        $address->setBaseGrandTotal($grandTotal);
        $address->setGrandTotal($grandTotal);

Currenct results : enter image description here Required Results : enter image description here

1 Answer 1

0

I could Achieve this using following code . just pass the value in convertPrice() and get the base currency converted code and save the same to base currency:

            $address->setBaseSubtotal($baseFinalPrice);
            $address->setSubtotal($finalPrice);
            $address->setDiscountAmount($discount);
            $address->setBaseDiscountAmount($baseDiscount);
            $address->setTaxAmount($tax);
            $address->setBaseTaxAmount($baseTax);
            $address->setBaseGrandTotal($baseGrandTotal);
            $address->setGrandTotal($grandTotal);

Conversion functions

/**
 * @var \Magento\Store\Model\StoreManagerInterface
 */
protected $storeManager;

/**
 * @var \Magento\Directory\Model\CurrencyFactory
 */
protected $currencyFactory;

/**
 * @param \Magento\Store\Model\StoreManagerInterface $storeManager
 * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
 */
public function __construct(
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Directory\Model\CurrencyFactory $currencyFactory
) {
    $this->storeManager = $storeManager;
    $this->currencyFactory = $currencyFactory;
}

/**
 * Convert base price value to store price value
 *
 * @param $amountValue
 * @return float
 */
public function convertPrice($amountValue)
{
    $currentCurrency = $this->storeManager->getStore()->getCurrentCurrency()->getCode();
    $baseCurrency = $this->storeManager->getStore()->getBaseCurrency()->getCode();
    if ($currentCurrency != $baseCurrency) {
        $rate = $this->currencyFactory->create()->load($currentCurrency)->getAnyRate($baseCurrency);
        $amountValue = $amountValue * $rate;
    }

    return $amountValue;
}

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.