2

I am trying to change the quote currency before placing any order.

I have used below event for that.

Vendor\Module\etc\frontend\events.xml

<event name="sales_model_service_quote_submit_before">
    <observer name="change_currency_before_place_order" instance="Vendor\Module\Observer\BeforePlaceOrder" />
</event>

Vendor\Module\Observer\BeforePlaceOrder.php

<?php

namespace Vendor\Module\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class BeforePlaceOrder implements ObserverInterface
{    

 protected $_checkoutSession;
 private $logger;
/**
 * @var \Magento\Catalog\Model\Session
 */
 protected $catalogSession;

public function __construct(
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Catalog\Model\Session $catalogSession,
    \Psr\Log\LoggerInterface $logger
)
{   
    $this->_checkoutSession = $checkoutSession;
    $this->catalogSession = $catalogSession;
    $this->logger = $logger;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{   
   $quote = $observer->getEvent()->getQuote(); 
   $this->logger->info('inside place order observer');
   //$this->storeManager->getStore()->setCurrentCurrencyCode('EUR'); 
   $quote->setQuoteCurrencyCode("EUR"); 
   $quote->collectTotals();
  }
}

The above event is not firing for me, Is there any other way to implement this functionality?

Note: The accepted answer working well but once the invoice is created, its changing to default currency again.

Can anyone advise me please why that event is not firing even after placing globally?.

  • Have you tried "sales_order_place_before" event of Magento? – Sumit Aug 29 '19 at 10:25
  • @Sumit, No not tried it.I'll try that but the functionality will work? – Manjunath Aug 29 '19 at 10:26
  • Yes, it'll work with the Quote. You can try it and please confirm. – Sumit Aug 29 '19 at 10:28
  • @Sumit, No That is also not working, – Manjunath Aug 29 '19 at 10:31
  • Hav you tried the following even checkout_submit_before – Dava Gordon Aug 29 '19 at 10:32
1

Give a try with below code.

events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_save_commit_after">
    <observer name="sumit_custom_order_currency" instance="Vendor\Module\Observer\OrderSaveCommitAfter" />
</event>
</config>

OrderSaveCommitAfter.php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class OrderSaveCommitAfter implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /* @var $order Order */
        $order = $observer->getEvent()->getOrder();

        if (!$order->getId()) {
            //order not saved in the database
            return;
        }
        $order->setBaseCurrencyCode('EUR');
        $order->setGlobalCurrencyCode('EUR');
        $order->setOrderCurrencyCode('EUR');
        $order->setStoreCurrencyCode('EUR');
        $order->save();
    }
}

Hope it helps!!!

| improve this answer | |
  • hello, @Sumit can u update the code to set quote currency here – Manjunath Aug 29 '19 at 10:49
  • I've updated my code, please check. – Sumit Aug 29 '19 at 10:56
  • thanks but unfortunately the event is not firing for me – Manjunath Aug 29 '19 at 10:57
  • Can you please share the scenario where you want to trigger this code. – Sumit Aug 29 '19 at 10:59
  • When customer or guest places the order, also pls let me know when that event will trigger? – Manjunath Aug 29 '19 at 11:01
2

This is a work around use Plugin instead of Observer.

Vendor/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<type name="Magento\Sales\Api\OrderRepositoryInterface">
    <plugin name="Module_OrderAfterSave" type="Vendor\Module\Plugin\OrderAfterSave" sortOrder="10" disabled="false" />
</type>

Then Vendor\Module\Plugin\OrderAfterSave.php

<?php
namespace Vendor\Module\Plugin;

use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Framework\ObjectManagerInterface;

class OrderAfterSave
{
private $logger;
protected $objectManager;
protected $orderRepositoryInterface;
protected $_coreSession;

public function __construct(
    ObjectManagerInterface $objectManager,
    OrderRepositoryInterface $orderRepositoryInterface,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\Session\SessionManagerInterface $coreSession
) {
    $this->objectManager = $objectManager;
    $this->orderRepositoryInterface = $orderRepositoryInterface;
    $this->logger = $logger;
    $this->_coreSession = $coreSession;
}

public function afterSave(\Magento\Sales\Api\OrderRepositoryInterface $orderRepo, $order)
{
    $currencyCode = 'EUR';

    $order->setBaseCurrencyCode($currencyCode);
    $order->setGlobalCurrencyCode($currencyCode);
    $order->setOrderCurrencyCode($currencyCode);
    $order->setStoreCurrencyCode($currencyCode);
    $order->save();

    return $order;
}

I hope this will help you!!

| improve this answer | |

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.