Home › Forums › WoodMart support forum › Code Snippet to add Savings Amount and Savings Percentage to all products on SAL › Reply To: Code Snippet to add Savings Amount and Savings Percentage to all products on SAL
December 26, 2023 at 4:11 pm
#524660

Aizaz Imtiaz Awan
Keymaster
Hello,
Remove the previous code and add the following PHP code in the function.php of the child theme. This code is for both simple products and variable products.
// For product variations
add_filter( 'woocommerce_available_variation', 'custom_variation_price_saving_percentage', 10, 3 ); function custom_variation_price_saving_percentage( $data, $product, $variation ) { $active_price = $data['display_price']; $regular_price = $data['display_regular_price']; if( $active_price !== $regular_price ) { $saving_percentage = round( 100 - ( $active_price / $regular_price * 100 ), 1 ) . '%'; $data['price_html'] .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage ); } return $data; }
// For simple products
add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 ); function change_displayed_sale_price_html( $price, $product ) { // Only on sale products on frontend and excluding min/max price on variable products if( $product->is_on_sale() && ! is_admin() && $product->is_type('simple') ){ // Get product prices $regular_price = (float) $product->get_regular_price(); // Regular price $sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale) // "Saving Percentage" calculation and formatting $precision = 1; // Max number of decimals $saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), $precision ) . '%'; // Append to the formated html price $price .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage ); } return $price; }
Best Regards.