0

I am creating orders programmatically in Magento 2 and want to change the order base currency as well as current currency to my custom currency, irrespective to currency available in Store as I don't want to have any impact 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);
  • "but failed" please elaborate. – Philipp Sander Jul 19 at 7:47
  • The currency I set on Quote are ignored and store currency is applied. – Milind Singh Jul 19 at 8:00
  • you're missing a saving of the quote – Philipp Sander Jul 19 at 8:57
  • Let me check. Also, all contribution to StackOverflow in under MIT, adding the profile link is not compulsory. – Milind Singh Jul 19 at 8:58
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, privacy policy and cookie policy

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