Magento 2 : Currency change frontend event
-
10-10-2020 - |
Pergunta
I want to update the quote item price on switching the currency from the frontend.
Is there any event to track currency switch option and give me the previous currency and requested currency?
Solução
I would recommend to write a Plugin on the following Controller Action :
Magento\Directory\Controller\Currency\SwitchAction
For example:
<?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\Directory\Controller\Currency\Switch">
<plugin name="yournamespace_yourmodule_directory_currency_switch_plugin"
type="YourNamespace\YourModule\Plugin\Magento\Direcotry\Controller\Currency\SwitchAction"
sortOrder="10"
disabled="false"/>
</type>
</config>
And then you could use the following code:
<?php
namespace YourNamespace\YourModule\Plugin\Magento\Directory\Controller\Currency;
class SwitchAction
{
protected $storeManager;
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->storeManager= $storeManager;
}
public function beforeExecute(
\Magento\Directory\Controller\Currency\SwitchAction $subject,
$result
) {
$currentCurrency = $this->storeManager->getStore()->getCurrentCurrencyCode();
$newCurrency = (string)$this->getRequest()->getParam('currency');
// your code here
}
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange