使用wc_price WooCommerce挂钩时遇到的格式不正确的数值 [英] A non well formed numeric value encountered while using wc_price WooCommerce hook

查看:37
本文介绍了使用wc_price WooCommerce挂钩时遇到的格式不正确的数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的功能曾经可以工作,几年前我就上网了.它使我可以手动更改EUR(€,Euro)的货币兑换率.

I have a simple function that used to work that I got online somewhere a few years back. It allows me to manually change the currency conversion rate for EUR (€, Euro).

现在的问题是这个:

Notice: A non well formed numeric value encountered in /wp-content/themes/theme/functions.php on line 82

此处的通知涉及此行:

$new_price = $price * $conversion_rate;

这就是我需要修复的帮助.

This is what I need help fixing.

这是完整的代码:

function manual_currency_conversion( $price ) {

    $conversion_rate = 1.25;

    $new_price = $price * $conversion_rate;

    return number_format( $new_price, 2, '.', '' );
}

add_filter( 'wc_price', 'manual_currency_conversion_display', 10, 3 );
function manual_currency_conversion_display( $formatted_price, $price, $args ) {

    $price_new = manual_currency_conversion( $price );

    $currency = 'EUR';

    $currency_symbol = get_woocommerce_currency_symbol( $currency );

    $price_new = $currency_symbol . $price_new;

    $formatted_price_new = "<span class='price-new'> ($price_new)</span>";

        return $formatted_price . $formatted_price_new;
}

推荐答案

  • 自WooCommerce 3.2.0起, wc_price 过滤器挂钩包含4个参数,甚至包含WooCommerce 5.0.0中的5个参数

    • Since WooCommerce 3.2.0 the wc_price filter hook contains 4 params and even 5 from WooCommerce 5.0.0

      第二个参数 $ price 是一个字符串.因此,您将收到错误消息,因为您正在使用它进行计算.解决方案是将其转换为浮点数

      The 2nd param $price, is a string. Hence the error message you get because you are doing calculations with it. The solution is to convert this to a float

      所以您得到:

      function manual_currency_conversion( $price ) { 
          $conversion_rate = (float) 1.25;
          
          $new_price = (float) $price * $conversion_rate;
      
          return number_format( $new_price, 2, '.', '' );
      }
       
      /**
       * Filters the string of price markup.
       *
       * @param string       $return            Price HTML markup.
       * @param string       $price             Formatted price.
       * @param array        $args              Pass on the args.
       * @param float        $unformatted_price Price as float to allow plugins custom formatting. Since 3.2.0.
       * @param float|string $original_price    Original price as float, or empty string. Since 5.0.0.
       */
      function filter_wc_price( $return, $price, $args, $unformatted_price, $original_price = null ) {
          // Call function
          $price_new = manual_currency_conversion( $price );
      
          $currency = 'EUR';
      
          $currency_symbol = get_woocommerce_currency_symbol( $currency );
      
          $price_new = $currency_symbol . $price_new;
      
          $formatted_price_new = "<span class='price-new'> ($price_new) </span>";
      
          return $return . $formatted_price_new;
      }
      add_filter( 'wc_price', 'filter_wc_price', 10, 5 );
      

      这篇关于使用wc_price WooCommerce挂钩时遇到的格式不正确的数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆