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:
If the percentage badge doesn’t display, you need to go to Appearance > Themes > Flatsome > Customize > WooCommerce > Product catalog and enable the “Enable % instead of “Sale!” parameter (at the bottom of the list).
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>'; } |
If the “Update price when user changes qty” parameter doesn’t work in the Divi theme you need to use the following code:
1 2 3 4 5 6 7 8 9 10 11 |
add_action( 'wp_footer', function() { ?> <script> jQuery( document ).ready( function() { setTimeout( function() { dynamicPrices[0].addPriceDestination("#page-container p.price"); }, 1000 ); }); </script> <?php }); |
Problem with the sale badge for the variable products in Kadense and Shoptimizer theme:
Sometimes the sale badge for the variable products doesn’t work and enabling the option “Pricing rules > Settings > Product price > Calculate “On Sale” badge for variable products doesn’t help, then add the code below into the functions.php of your theme or as a new snippet if you are using any code snippets plugin:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
add_filter( 'woocommerce_variation_prices', function($prices_array, $product, $for_display ){ if(!is_shop() AND !is_product_category() AND !is_product() AND !is_product_taxonomy() ) return $prices_array; $processedProduct = adp_functions()->calculateProduct($product, 1); if(! $processedProduct instanceof \ADP\BaseVersion\Includes\PriceDisplay\ProcessedVariableProduct ) return $prices_array; $new_prices_array = array("price"=>[],"regular_price"=>[],"sale_price"=>[]); foreach($processedProduct->getChildren() as $variation){ $idx = $variation->getProduct()->get_id(); $new_prices_array['price'][$idx] = $variation->getPrice(); $new_prices_array['sale_price'][$idx] = $variation->getPrice(); $new_prices_array['regular_price'][$idx] = $variation->getOriginalPrice(); } return $new_prices_array; },10,3); |