Last active
April 20, 2022 05:34
-
-
Save AnanthFlycart/7bedb36ede0c03a7012dfffeaf278491 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Fix - Woodmart theme sale badge issue | |
| if ( ! function_exists( 'woodmart_child_product_label' ) ) { | |
| function woodmart_child_product_label() { | |
| global $product; | |
| if ( function_exists( 'woodmart_get_product_attributes_label' ) && function_exists( 'woodmart_get_opt' ) && function_exists( 'woodmart_is_new_label_needed' ) ) { | |
| $output = array(); | |
| $product_attributes = woodmart_get_product_attributes_label(); | |
| $percentage_label = woodmart_get_opt( 'percentage_label' ); | |
| if ( $product->is_on_sale() ) { | |
| $percentage = ''; | |
| if ( $product->get_type() == 'variable' && $percentage_label ) { | |
| $available_variations = $product->get_variation_prices(); | |
| $max_percentage = 0; | |
| foreach ( $available_variations['regular_price'] as $key => $regular_price ) { | |
| $sale_price = $available_variations['sale_price'][ $key ]; | |
| if ( function_exists( 'wc_get_product' ) ) { | |
| $product_variation = wc_get_product( $key ); | |
| $discount = apply_filters( 'advanced_woo_discount_rules_get_product_discount_price_from_custom_price', false, $product_variation, 1, 0, 'discounted_price', true, false ); | |
| if ( $discount !== false ) { | |
| $sale_price = $discount; | |
| } | |
| } | |
| if ( $sale_price < $regular_price ) { | |
| $percentage = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 ); | |
| if ( $percentage > $max_percentage ) { | |
| $max_percentage = $percentage; | |
| } | |
| } | |
| } | |
| $percentage = $max_percentage; | |
| } elseif ( ( $product->get_type() == 'simple' || $product->get_type() == 'external' || $product->get_type() == 'variation' ) && $percentage_label ) { | |
| $discount = apply_filters( 'advanced_woo_discount_rules_get_product_discount_price_from_custom_price', false, $product, 1, 0, 'all', true, false ); | |
| if ($discount !== false && is_array( $discount ) && isset( $discount['initial_price'] ) && isset( $discount['discounted_price'] ) ) { | |
| $percentage = round( ( ( $discount['initial_price'] - $discount['discounted_price'] ) / $discount['initial_price'] ) * 100 ); | |
| } elseif ($product->get_sale_price() != '') { | |
| $percentage = round( ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100 ); | |
| } | |
| } | |
| if ( $percentage > 0 ) { | |
| $output[] = '<span class="onsale product-label">-' . $percentage . '%' . '</span>'; | |
| } | |
| } | |
| if ( ! $product->is_in_stock() ) { | |
| $output[] = '<span class="out-of-stock product-label">' . esc_html__( 'Sold out', 'woodmart' ) . '</span>'; | |
| } | |
| if ( $product->is_featured() && woodmart_get_opt( 'hot_label' ) ) { | |
| $output[] = '<span class="featured product-label">' . esc_html__( 'Hot', 'woodmart' ) . '</span>'; | |
| } | |
| if ( woodmart_get_opt( 'new_label' ) && woodmart_is_new_label_needed( get_the_ID() ) ) { | |
| $output[] = '<span class="new product-label">' . esc_html__( 'New', 'woodmart' ) . '</span>'; | |
| } | |
| if ( $product_attributes ) { | |
| foreach ( $product_attributes as $attribute ) { | |
| $output[] = $attribute; | |
| } | |
| } | |
| $output = apply_filters( 'woodmart_product_label_output', $output ); | |
| if ( $output ) { | |
| echo '<div class="product-labels labels-' . woodmart_get_opt( 'label_shape' ) . '">' . implode( '', $output ) . '</div>'; | |
| } | |
| } | |
| } | |
| add_action( 'after_setup_theme', function() { | |
| remove_all_filters( 'woocommerce_sale_flash' ); | |
| add_filter( 'woocommerce_sale_flash', 'woodmart_child_product_label', 10 ); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment