// ========================================================================= // SHOW THE PERCENTAGE VALUE OF DISCOUNT PRODUCTS // ========================================================================= function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) { global $product; if( $product->has_child() ) { $var_regular_price = $product->get_variation_regular_price( 'min', true ); $var_sale_price = $product->get_variation_price( 'min', true ); $percentage = round( ( $var_regular_price - $var_sale_price ) / $var_regular_price * 100 ).'%'; $percentage_txt = ' ' . __('<span class="sale-percentage"> -', 'woocommerce' , '</span>') . $percentage; $price = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del> <ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) . $percentage_txt : $sale_price . $percentage_txt ) . '</ins>'; return $price; } else{ $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%'; $percentage_txt = ' ' . __('<span class="sale-percentage"> -', 'woocommerce' , '</span>') . $percentage; $price = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del> <ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) . $percentage_txt : $sale_price . $percentage_txt ) . '</ins>'; return $price; } } add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
Leave a Reply