php - 在Woocommerce 3中的销售徽章上显示折扣百分比

标签 php wordpress woocommerce percentage badge

这适用于简单的产品,但对于可变产品却给了我两个错误。在文件中的销售闪存中,我得到NAN%,错误为“遇到非数值”。

我的代码:

add_filter( 'woocommerce_sale_flash', 'add_percentage_to_sale_bubble' );
function add_percentage_to_sale_bubble( $html ) {
    global $product;
    $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
    $output ='<span class="onsale">SALE<br>-'.$percentage.'%</span>';
    return $output;
}

有想法该怎么解决这个吗?

任何帮助都将受到高度赞赏。

最佳答案

2020更新-重新审视了代码并处理了分组产品。
自Woocommerce 3起,您正在使用的代码已过时。请尝试以下方法,它们也可以处理可变产品(和分组产品):

add_filter( 'woocommerce_sale_flash', 'add_percentage_to_sale_badge', 20, 3 );
function add_percentage_to_sale_badge( $html, $post, $product ) {

  if( $product->is_type('variable')){
      $percentages = array();

      // Get all variation prices
      $prices = $product->get_variation_prices();

      // Loop through variation prices
      foreach( $prices['price'] as $key => $price ){
          // Only on sale variations
          if( $prices['regular_price'][$key] !== $price ){
              // Calculate and set in the array the percentage for each variation on sale
              $percentages[] = round( 100 - ( floatval($prices['sale_price'][$key]) / floatval($prices['regular_price'][$key]) * 100 ) );
          }
      }
      // We keep the highest value
      $percentage = max($percentages) . '%';

  } elseif( $product->is_type('grouped') ){
      $percentages = array();

      // Get all variation prices
      $children_ids = $product->get_children();

      // Loop through variation prices
      foreach( $children_ids as $child_id ){
          $child_product = wc_get_product($child_id);

          $regular_price = (float) $child_product->get_regular_price();
          $sale_price    = (float) $child_product->get_sale_price();

          if ( $sale_price != 0 || ! empty($sale_price) ) {
              // Calculate and set in the array the percentage for each child on sale
              $percentages[] = round(100 - ($sale_price / $regular_price * 100));
          }
      }
      // We keep the highest value
      $percentage = max($percentages) . '%';

  } else {
      $regular_price = (float) $product->get_regular_price();
      $sale_price    = (float) $product->get_sale_price();

      if ( $sale_price != 0 || ! empty($sale_price) ) {
          $percentage    = round(100 - ($sale_price / $regular_price * 100)) . '%';
      } else {
          return $html;
      }
  }
  return '<span class="onsale">' . esc_html__( 'SALE', 'woocommerce' ) . ' ' . $percentage . '</span>';
}
代码进入事件子主题(或事件主题)的functions.php文件中。经过测试和工作。

关于php - 在Woocommerce 3中的销售徽章上显示折扣百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52558950/

相关文章:

javascript - 在 WordPress 菜单中调用 Javascript 函数

css - 我的网站右侧有一个白边

php - 在 Woocommerce 上的产品中设置产品类别术语

javascript - 来自 PHP : how to get a dynamically generated javascript image

php - MYSQL语法错误

sql - 将 SQL 查询输出为数组

php - WooCommerce 结帐 - 如何为特定地址提供免费送货服务

javascript - React Native 中的 Woocommerce API per_page 最大限制

php - 将 Unicode 转义序列转换为 UTF-8

javascript - 这个受密码保护的下载脚本安全吗?