【问题标题】:Change WooCommerce color of product percentage discount更改产品百分比折扣的 WooCommerce 颜色
【发布时间】:2021-07-14 23:51:22
【问题描述】:

我正在尝试将每种产品的百分比折扣框颜色从绿色更改为红色。我从reddit复制了我使用的功能,所以我不明白如何正确更改它。我之前尝试过,但最终导致整个网站变砖(致命错误) 这是代码及其外观:

add_action( 'woocommerce_sale_flash', 'sale_badge_percentage', 25 );
 
function sale_badge_percentage() {
   global $product;
   if ( ! $product->is_on_sale() ) return;
   if ( $product->is_type( 'simple' ) ) {
      $max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
   } elseif ( $product->is_type( 'variable' ) ) {
      $max_percentage = 0;
      foreach ( $product->get_children() as $child_id ) {
         $variation = wc_get_product( $child_id );
         $price = $variation->get_regular_price();
         $sale = $variation->get_sale_price();
         if ( $price != 0 && ! empty( $sale ) ) $percentage = ( $price - $sale ) / $price * 100;
         if ( $percentage > $max_percentage ) {
            $max_percentage = $percentage;
         }
      }
   }
   if ( $max_percentage > 0 ) echo "<span class='onsale'>-" . round($max_percentage) . "%</span>"; // If you would like to show -40% off then add text after % sign
}

看起来像这样: Discount % in my website

编辑:使用@MrSandyWilly 评论的内容似乎完美无缺

【问题讨论】:

    标签: html css wordpress woocommerce


    【解决方案1】:

    我没有使用 woocommerce 的经验,但请尝试更改此代码:

    &lt;span class='onsale'&gt;&lt;span class='onsale' style='background-color: #f00;'&gt;

    这应该覆盖 woocommerce 提供的默认样式并将颜色设置为红色。

    【讨论】:

    • 谢谢!!!效果很好,就像我想要的那样。
    猜你喜欢
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 2019-03-18
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    相关资源
    最近更新 更多