Okay
  Public Ticket #2765423
Sales Badge
Closed

Comments

  •  4
    kuanhwa14 started the conversation

    Hi support,

    Can I change the sales badge to percentage instead of the text "Sales"?

    For example like -30%, -25%, which it auto-calculated based on the discounted price.

  •  1,149
    Art replied

    Hi kuanhwa14,

    Currently this feature is not possible in Kalium, we are completely redesigning the shop pages and this feature will be included soon, in the meantime if you want to implement it now add this feature now add this code under functions.php of the Kalium - Child Theme:

    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
    }
    

    If you don't know how to add custom code to functions.php file, follow this guide from our documentation.

    Sources:

    https://wpsimplehacks.com/how-to-display-the-discount-percentage-on-the-sale-badge/

    https://divi.space/wordpress-and-divi-code-snippets/change-woocommerce-sale-badge-display-percentage-discount/