【问题标题】:Magento custom price value not converting by changing currencyMagento 自定义价格值不通过更改货币转换
【发布时间】:2016-06-15 05:57:10
【问题描述】:

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

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

是否有任何方法可以设置与货币转换一起使用的自定义价格。

【问题讨论】:

    标签: php magento cart price


    【解决方案1】:

    我找到了解决方案。

    第一步:

    使用@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 dispatch 观察者

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

    并将以下代码添加到观察者类

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

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

    【讨论】:

      【解决方案2】:

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

      第一步:

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

      【讨论】:

      • 感谢您的回复。但我没有面临当前货币的问题。我已经在购物车中添加了自定义价格的商品,这很好。现在,如果我在购物车页面上切换货币,那么该商品的价格值将保持不变。
      • 嗨@gulshan maurya,您找到上述任务的解决方案了吗?请在这里告诉我
      • 我还没有找到任何解决方案,相关任务暂时搁置。如果我能得到更好的解决方案,我会在这里更新。
      • 嗨@AshishRaj,这对你有用吗?我试过了,但没用。
      猜你喜欢
      • 2014-05-13
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 2015-06-08
      • 2019-10-04
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多