Based on this post :
- Displaying the lowest variation price of variable products And this topic :
- Display the discounted percentage near sale price in Single product pages for WC 3.0+
I’ve been trying to show everywhere on my website the lowest variation price with the associated discount %, for variable and non variable products.
Here is the current code that I am using:
add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format', 10, 2 ); function bbloomer_variation_price_format( $price, $product ) { // Main Price $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) ); $price = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] ); // Sale Price $prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) ); sort( $prices ); $saleprice = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] ); if ( $price !== $saleprice ) { $price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>'; } return $price; } add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 ); function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) { // Getting the clean numeric prices (without html and currency) $regular_price = floatval( strip_tags($regular_price) ); $sale_price = floatval( strip_tags($sale_price) ); // Percentage calculation and text $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%'; $percentage_txt = __(' Save ', 'woocommerce' ).$percentage; return '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . $percentage_txt . '</ins>'; } add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'mmx_remove_select_text');
If I only keep the second function, it works on every product but not on variable products, which show a price range on shop pages. If I add the first function, it shows instead of a price range the price of the lowest product variation, but without the discount % next to it.
I could add the % code of the second function to the first, but this is messy, is there a way to merge these functions so that I can have the discount % shown everywhere, and also next to the lowest variation when it’s a variable product?
Advertisement
Answer
This can be done changing a little bit the first function that will replace both hooked functions:
add_filter( 'woocommerce_get_price_html', 'custom_price_format', 10, 2 ); add_filter( 'woocommerce_variable_price_html', 'custom_price_format', 10, 2 ); function custom_price_format( $price, $product ) { // Main Price $regular_price = $product->is_type('variable') ? $product->get_variation_regular_price( 'min', true ) : $product->get_regular_price(); $sale_price = $product->is_type('variable') ? $product->get_variation_sale_price( 'min', true ) : $product->get_sale_price(); if ( $regular_price !== $sale_price && $product->is_on_sale()) { // Percentage calculation and text $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%'; $percentage_txt = __(' Save', 'woocommerce' ).' '.$percentage; $price = '<del>' . wc_price($regular_price) . '</del> <ins>' . wc_price($sale_price) . $percentage_txt . '</ins>'; } return $price; }
This code goes in function.php file of your active child theme (or theme).
Tested and works with your customizations made in your previous questions/answer.