I have 2 currencies in my Magento 2 website. USD is the base currency and SAR (SAR) is the second currency. In my website header there is a drop down where the user can select the currency of the store. When the user visits the website he can see every product value in USD, and if the user changes the currency in the drop down to riyal, then he can see every product value in SAR.
Now I have a requirement for a user who is visiting the website from Saudi. He needs to see the store in SAR currency. For that I have written a module and it is working fine.
My problem is that if a user visits and tries to change the store currency in the dropdown, the page reloads and the drop down is reset to SAR and SAR currency is shown on the products.
This is my code in my-module/observer/changecurrency.php
if($country=='saudi'){
$currency = 'SAR';
$this->storeManager->getStore()->setCurrentCurrencyCode($currency);
}
Here I want to check whether the user selected the USD currency or not. If the user selects USD, I have to show products in USD currency, otherwise I have to show the products in SAR currency.