Magento 自定义价格值不通过更改货币进行转换

Magento custom price value not converting by changing currency

以下代码用于设置简单产品的自定义价格。根据需要在购物车中设置自定义价格,但当我切换货币时,自定义价格值与当前货币符号保持相同。

 $item->setCustomPrice($customPrice);
            $item->setOriginalCustomPrice($customPrice);
            $item->getProduct()->setIsSuperMode(true);

有什么方法可以设置与货币转换一起使用的自定义价格。

使用下面的代码希望对你有帮助...

第一步:

//you need to find base currency code and then find current currency code...
$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode(); 
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
$price = $customPrice;

第二步:

//convert price from base currency to current currency
$customPrice = $Current_currency_price = Mage::helper('directory')->currencyConvert($price, $baseCurrencyCode, $currentCurrencyCode); 

第三步:

那么您可以使用您的代码:

$item->setCustomPrice($customPrice);
$item->setOriginalCustomPrice($customPrice);
$item->getProduct()->setIsSuperMode(true);

我找到了解决方法。

第一步:

使用@Ashish Raj 建议的以下代码添加项目以自定义价格报价

$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode(); 
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
$price = $customPrice;

$customPrice = $Current_currency_price = Mage::helper('directory')->currencyConvert($price, $baseCurrencyCode, $currentCurrencyCode);

$item->setCustomPrice($customPrice);
$item->setOriginalCustomPrice($customPrice);
$item->getProduct()->setIsSuperMode(true);

第二步:

第二步是通过在模块 config.xml 文件中添加以下代码来创建控制器 post 调度观察器

<events>
        <controller_action_postdispatch>
            <observers>
                <frontend_currency_change>
                    <class>modulename/observer</class>
                    <method>hookToControllerActionPostDispatch</method>
                </frontend_currency_change>
            </observers>
        </controller_action_postdispatch>
    </events>

并向观察者添加以下代码class

    public function hookToControllerActionPostDispatch($observer) {
            if ($observer->getEvent()->getControllerAction()->getFullActionName() == 'directory_currency_switch') {
                $quote = Mage::getSingleton('checkout/session')->getQuote();
                if ($quote && $quote->hasItems()) {
                    foreach ($quote->getAllVisibleItems() as $item):
                        //add condition for target item
                        $customPrice = 23;//use custom price logic
                        $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
                        $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
                        $customPrice = Mage::helper('directory')->currencyConvert($customPrice, $baseCurrencyCode, $currentCurrencyCode);
                        $item->setCustomPrice($customPrice);
                        $item->setOriginalCustomPrice($customPrice);
                        $item->getProduct()->setIsSuperMode(true);
                        $quote->collectTotals()->save();
                    endforeach;
                }
            }
        }

这对我有用。希望这对遇到同样问题的人有所帮助。 如果有人有更好的解决方案,我会更喜欢。 谢谢