【问题标题】:Display the discount percentage on the sale badge in Woocommerce 3在 Woocommerce 3 中的销售徽章上显示折扣百分比
【发布时间】:2019-03-04 15:57:33
【问题描述】:

这适用于简单的产品,但给我两个可变产品的错误。在存档的促销闪存中,我得到 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;
}

关于如何解决这个问题的任何想法?

非常感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce percentage badge


    【解决方案1】:

    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 文件中。经过测试并且可以工作。

    【讨论】:

    • 在计算可变产品的百分比时存在一个问题,下面的代码将解决它$percentages[] = ( floatval( $prices['regular_price'][ $key ] ) - floatval( $price ) ) / floatval( $prices['regular_price'][ $key ] ) * 100; 因为 WooCommerce 将 '.00' 附加到销售价格并导致问题。
    • 这在 wordpress 5.4.4 和 woocommerce 版本 3.9.2 上对我有用
    猜你喜欢
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    相关资源
    最近更新 更多