2016-06-15 93 views
1

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

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

有没有什么方法可以设置自定义价格与货币切换一起工作。

回答

1

我找到了一个解决方案,做到这一点。

第一步:

添加项目使用自定义的价格由@Ashish拉吉建议下面的代码来引用

$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文件中添加以下代码来控制员发布观察者

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

这是为我工作。希望这可以帮助有同样问题的人。 如果有人有更好的解决方案,我会更喜欢。 谢谢。

0

使用下面的代码希望它会帮助你...

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


// and then 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); 
+0

感谢您的回复。但我并不面临当前货币的问题。我已添加自定义价格的购物车中的项目是好的。现在,如果我在购物车页面上切换货币,那么价格价值保持不变,该项目。 –

+0

嗨@gulshan maurya,你发现上述任务的决议?请让我知道 –

+0

我还没有找到任何解决方案,相关任务暂时搁置。我会在这里更新,如果我会得到更好的解决方案。 –