Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

如何在Magento商店中通过JavaScript动态更改产品价格的货币格式?

在此博客中,我们将告诉您根据本地价格格式化产品价格的编码过程 (即更改货币的过程)格式,同时由于用户数量的变化而更新价格。

为什么?

在某些情况下,更改数量时,产品的总价也应立即更新。在更新价格时,我们应在显示之前根据本地价格格式(货币格式)对其进行格式化。

在magento中,“ js / varien /”目录中有一个名为js.js的文件。在此文件中,我们有一个“ formatCurrency”函数,该函数用于通过javascript格式化magento商店中的价格。

js中的函数=> formatCurrency(price,format,showPlus)

它具有3个参数。

1.价格 -(要转换为整数/小数的价格)。
2.格式 –(根据区域设置,JSON包含模式/小数等)将类似于[JSON(模式,十进制,十进制Delimeter,groupsDelimeter)]
3. ShowPlus – true(始终显示“ +”或“-”),false(从不即使数字为负也显示'-'--null(如果数字为负则显示'-')

下面提供的代码将有助于获取Magento中的JS价格格式(具有模式/小数等)。

      Mage::app()->getLocale()->getJsPriceFormat();



And we can write the code given below in Block file to return the price format as JSON to .phtml.

   public function getCurrentPriceFormat(){
        /* Return the current price format as JSON */
        return Mage::helper('core')->jsonEncode(
                Mage::app()->getLocale()->getJsPriceFormat() );
   }


In .phtml file initialize variable with the value from getCurrentPriceFormat ().

   	<script>
   	    /* Save the returned JSON in variable to use it further */
	    var currentPriceFormat = <?php echo $this->getCurrentPriceFormat();?>
	</script>

Now we are very close towards getting the price format.

	<script>
	    /* Calculate the price may be price with quantity */
	    var newPrice =  parseFloat( price ) * parseFloat( quantity );
	    /* Save the price format returned by formatCurrency to use */
	    var priceToDisplay = formatCurrency( price, currentPriceFormat );
	    /* replace formatted price in priceArea HTML element */
	    document.getElementById( 'priceArea' ).innerHTML = priceToDisplay;
	</script>

就是这样,所需的结果将放置在priceArea HTML元素中。

请按照以下说明使用JavaScript更改价格格式。希望代码有所帮助。

如有任何疑问或其他想法,请在下面的评论部分中写信给我们。我们很高兴阅读您的建议

版权属于: sbboke版权所有。

转载时必须以链接形式注明作者和原始出处及本声明。

张贴在magento2教程标签:

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论