Magento – Magento 2: Change quote base currency programmatically

currencymagento2ordersprogrammaticallyquote

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);

Best Answer

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.

Related Topic