Compatibility with themes
Some themes doesn’t support Woocommerce hooks, so you should add shortcodes [adp_category_bulk_rules_table] and [adp_product_bulk_rules_table] to necessary page templates (or directly to product description) OR you can submit new ticket to helpdesk.
If you use paid plugin/theme — please, upload it (as.zip file) to the ticket!
Flatsome:
1 2 3 4 5 6 7 |
add_filter( 'woocommerce_product_is_on_sale', function ( $on_sale, $product ) { if ( $on_sale && function_exists( 'flatsome_percentage_clear' ) ) { flatsome_percentage_clear( $product ); } return $on_sale; }, 1000, 2 ); |
Divi:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
add_filter( 'woocommerce_sale_flash', 'add_percentage_to_sale_badge', 20, 3 ); function add_percentage_to_sale_badge( $html, $post, $product ) { if ( $product->is_type( 'grouped' ) || $product->is_type( 'variable' ) ) { $percentages = array(); foreach ( $product->get_children() as $childId ) { $childProduct = wc_get_product( $childId ); $regularPrice = floatval( $childProduct->get_regular_price() ); if ( $regularPrice === floatval( 0 ) ) { $percentages[] = floatval( 100 ); break; } $salePrice = $childProduct->get_sale_price() !== "" ? floatval( $childProduct->get_sale_price() ) : null; if ( $salePrice !== null ) { // Calculate and set in the array the percentage for each child on sale $percentages[] = round( 100 - ( $salePrice / $regularPrice * 100 ) ); } } // We keep the highest value $percentage = count( $percentages ) > 0 ? max( $percentages ) . '%' : ""; } else { $regularPrice = floatval( $product->get_regular_price() ); if ( $regularPrice !== floatval( 0 ) ) { $salePrice = $product->get_sale_price() !== "" ? floatval( $product->get_sale_price() ) : null; if ( $salePrice !== null ) { $percentage = round( 100 - ( $salePrice / $regularPrice * 100 ) ) . '%'; } else { return $html; } } else { $percentage = floatval( 100 ) . '%'; } } return '<span class="onsale">' . esc_html__( 'SALE', 'woocommerce' ) . ' ' . $percentage . '</span>'; } |