WooCommerce Advanced Product Labels, by Jeroen Sormani
Add provided code to functions.php and update Settings.
Calculation option “Use prices modified by other plugins” must be ON.
System option “Suppress other pricing plugins in frontend” must be OFF.
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
remove_filter('wapl_product_label', 'wapl_smart_product_label_filter'); add_filter('wapl_product_label', function($label) { global $product; // This in here for the admin preview to select one random product if ( ! $product ) { $product_posts = get_posts( array( 'post_type' => 'product', 'posts_per_page' => 1 ) ); $product = reset( $product_posts ); } $product = wc_get_product( $product ); $highest_percentage = 0; if ( ! $product ) { // Check to be sure the global $product is set properly trigger_error( 'The global $product is not a valid variable type: ' . gettype( $product ) ); return $label; } if ( $product->is_type( 'composite' ) ) { $regular_price = $product->get_composite_regular_price(); $sale_price = $product->get_composite_price(); $highest_percentage = ( $sale_price !== '' && $regular_price != 0 ) ? ( ( $regular_price - $sale_price ) / $regular_price * 100 ) : $highest_percentage; } elseif ( ! $product->is_type( 'variable' ) ) { $regular_price = $product->get_regular_price(); $sale_price = $product->get_sale_price(); $highest_percentage = ( $sale_price !== '' && $regular_price != 0 ) ? ( ( $regular_price - $sale_price ) / $regular_price * 100 ) : $highest_percentage; } else { // Get the right variable percentage foreach ( $product->get_children() as $child_id ) { $child = wc_get_product($child_id); $price = $child->get_regular_price(); $sale = $child->get_sale_price(); $percentage = $price != 0 && ! empty( $sale ) ? ( ( $price - $sale ) / $price * 100 ) : $highest_percentage; if ( $percentage >= $highest_percentage ) { $highest_percentage = $percentage; $regular_price = $product->get_variation_regular_price( 'min' ); $sale_price = $product->get_variation_sale_price( 'min' ); } } } $label = str_replace( '{percentage}', round( $highest_percentage, apply_filters( 'wapl_filter_discount_round', 1 ) ) . '%', $label ); $label = str_replace( '{discount}', wc_price( (float) $regular_price - (float) $sale_price ), $label ); $label = str_replace( '{price}', wc_price( $regular_price ), $label ); $label = str_replace( '{saleprice}', wc_price( $sale_price ), $label ); $label = str_replace( '{delprice}', '<del>' . wc_price( $regular_price ) . '</del>', $label ); return $label; }); |