0

I am creating orders programmatically in Magento 2. I wanted to change the order base currency. The current currency is my custom currency, irrespective of currency available in Store as I don't want to have any impact on to frontend.

I have tried the following on Quote but failed.

    /** @var \Magento\Quote\Model\Quote $quote */
    $quote = $this->cartRepositoryInterface->get($cartId);
    $quote->setStore($store);
    /** @var \Magento\Quote\Model\Cart\Currency  */
    $currency = $this->currencyFactory->create()
        ->setGlobalCurrencyCode($order->getCurrencyCode())
        ->setBaseCurrencyCode($order->getCurrencyCode())
        ->setStoreCurrencyCode($order->getCurrencyCode())
        ->setQuoteCurrencyCode($order->getCurrencyCode())
        ->setStoreToBaseRate(1.00)
        ->setStoreToQuoteRate(1.00)
        ->setBaseToGlobalRate(1.00)
        ->setBaseToQuoteRate(1.00);
    $quote->setCurrency($currency);
4
  • "but failed" please elaborate. Commented Jul 19, 2019 at 7:47
  • The currency I set on Quote are ignored and store currency is applied. Commented Jul 19, 2019 at 8:00
  • you're missing a saving of the quote Commented Jul 19, 2019 at 8:57
  • Let me check. Also, all contribution to StackOverflow in under MIT, adding the profile link is not compulsory. Commented Jul 19, 2019 at 8:58

1 Answer 1

0

I did a lot of trial and error and was able to override the base_currency.

            /** @var \Magento\Quote\Model\Cart\Currency $quoteCurrency */
            $quoteCurrency = $this->quoteCurrencyFactory->create()
                ->setGlobalCurrencyCode("GBP")
                ->setBaseCurrencyCode("GBP")
                ->setStoreCurrencyCode("GBP")
                ->setQuoteCurrencyCode("GBP")
                ->setStoreToBaseRate(1.00)
                ->setStoreToQuoteRate(1.00)
                ->setBaseToGlobalRate(1.00)
                ->setBaseToQuoteRate(1.00);

            /** @var \Magento\Directory\Model\Currency $directoryCurrency */
            $directoryCurrency = $this->directoryCurrencyFactory->create()
                ->setGlobalCurrencyCode("GBP")
                ->setBaseCurrencyCode("GBP")
                ->setStoreCurrencyCode("GBP")
                ->setQuoteCurrencyCode("GBP")
                ->setStoreToBaseRate(1.00)
                ->setStoreToQuoteRate(1.00)
                ->setBaseToGlobalRate(1.00)
                ->setBaseToQuoteRate(1.00);
            // Overrriding base currency on Store
            /** @var \Magento\Store\Model\Store $store */
            $store->setBaseCurrency($directoryCurrency);

            // Creating an Empty Cart
            $cartId = $this->cartManagementInterface->createEmptyCart();
            /** @var \Magento\Quote\Model\Quote $quote */
            $quote = $this->cartRepositoryInterface->get($cartId);
            $quote->setStore($store);

            // forcefully changing the currency for quote
            $quote->setForcedCurrency($directoryCurrency);
            $quote->setBaseCurrencyCode("GBP");
            $quote->setGlobalCurrencyCode("GBP");
            $quote->setStoreCurrencyCode("GBP");
            $quote->setQuoteCurrencyCode("GBP");
            $quote->setCurrency($quoteCurrency);

            // Add Products
            // TODO: Add Products

            // Collect total and Save the qoute
            $quote->collectTotals()->save();

            // forcefully changing the currency for quote again as collect total made it "null"
            $quote->setForcedCurrency($directoryCurrency);
            $quote->setBaseCurrencyCode("GBP");
            $quote->setGlobalCurrencyCode("GBP");
            $quote->setStoreCurrencyCode("GBP");
            $quote->setQuoteCurrencyCode("GBP");
            $quote->setCurrency($quoteCurrency);

            // Submi the order, and done.
            /** @var \Magento\Sales\Model\Order $magentoOrder */
            $magentoOrder = $this->cartManagementInterface->submit($quote)

The above code did the job but If anyone can suggest a better way to do the job, it will be highly appreciated.

Also, this may not be the standard way.

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.