[Support request] Displays the percentage discount on the product

Home Forums Support [Support request] Displays the percentage discount on the product

Home Forums Support Displays the percentage discount on the product

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1097613
    Roi

    Currently, the only option in this template is to display the word “Sale” on products. I want the percentage of the product discount to be displayed instead of the word sale. Is there such a possibility?

    my domain is: bombasale.com

    #1097851
    David
    Staff
    Customer Support

    Hi there,

    try this PHP Snippet:

    function woocommerce_custom_sale_savings() {
       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 ) {
         return  '<span class="onsale">-' . round($max_percentage) . '%</span>';
       } 
    }
    
    add_filter('woocommerce_sale_flash', 'woocommerce_custom_sale_savings', 10, 3);
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.