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; });
WooCommerce Advanced Product Labels, by Jeroen Sormani Print
Created by: Alex Prokaev
Modified on: Tue, 28 Apr, 2020 at 6:25 PM
Did you find it helpful? Yes No
Send feedbackSorry we couldn't be helpful. Help us improve this article with your feedback.