Fix Martfury theme sale badge issue
Resolve the sale badge display issue in the Martfury theme.
// Fix - Martfury theme sale badge issue
if ( ! function_exists( 'martfury_child_product_ribbons' ) ) {
function martfury_child_product_ribbons() {
global $product;
if ( function_exists( 'martfury_get_option' ) ) {
$output = array();
$badges = (array) martfury_get_option( 'badges' );
// Change the default sale ribbon
$custom_badges = maybe_unserialize( get_post_meta( $product->get_id(), 'custom_badges_text', true ) );
if ( $custom_badges ) {
$output[] = '<span class="custom ribbon">' . esc_html( $custom_badges ) . '</span>';
} else {
if ( ! $product->is_in_stock() && in_array( 'outofstock', $badges ) ) {
$outofstock = martfury_get_option( 'outofstock_text' );
if ( ! $outofstock ) {
$outofstock = esc_html__( 'Out Of Stock', 'martfury' );
}
$output[] = '<span class="out-of-stock ribbon">' . esc_html( $outofstock ) . '</span>';
} elseif ( $product->is_on_sale() && in_array( 'sale', $badges ) ) {
$percentage = 0;
$save = 0;
if ( $product->get_type() == 'variable' ) {
$available_variations = $product->get_available_variations();
$percentage = 0;
$save = 0;
for ( $i = 0; $i < count( $available_variations ); $i ++ ) {
$variation_id = $available_variations[ $i ]['variation_id'];
$variable_product = new WC_Product_Variation( $variation_id );
$regular_price = $variable_product->get_regular_price();
$sales_price = $variable_product->get_sale_price();
$discount = apply_filters( 'advanced_woo_discount_rules_get_product_discount_price_from_custom_price', false, $variable_product, 1, 0, 'discounted_price', true, false );
if ( $discount !== false ) {
$sales_price = $discount;
}
if ( empty( $sales_price ) ) {
continue;
}
$max_percentage = $regular_price ? round( ( ( ( $regular_price - $sales_price ) / $regular_price ) * 100 ) ) : 0;
$max_save = $regular_price ? $regular_price - $sales_price : 0;
if ( $percentage < $max_percentage ) {
$percentage = $max_percentage;
}
if ( $save < $max_save ) {
$save = $max_save;
}
}
} elseif ( $product->get_type() == 'simple' || $product->get_type() == 'external' ) {
$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 );
$save = $discount['initial_price'] - $discount['discounted_price'];
} else {
$percentage = round( ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100 );
$save = $product->get_regular_price() - $product->get_sale_price();
}
}
if ( martfury_get_option( 'sale_type' ) == '2' ) {
if ( $save > 0 ) {
$output[] = '<span class="onsale ribbon sale-text"><span class="onsep">' . esc_html( martfury_get_option( 'sale_save_text' ) ) . '</span>' . ' ' . wc_price( $save ) . '</span>';
}
} else {
if ( $percentage > 0 ) {
$output[] = '<span class="onsale ribbon"><span class="onsep">-</span>' . $percentage . '<span class="per">%</span>' . '</span>';
}
}
} elseif ( $product->is_featured() && in_array( 'hot', $badges ) ) {
$hot = martfury_get_option( 'hot_text' );
if ( ! $hot ) {
$hot = esc_html__( 'Hot', 'martfury' );
}
$output[] = '<span class="featured ribbon">' . esc_html( $hot ) . '</span>';
} elseif ( ( time() - ( 60 * 60 * 24 * martfury_get_option( 'product_newness' ) ) ) < strtotime( get_the_time( 'Y-m-d' ) ) && in_array( 'new', $badges ) ||
get_post_meta( $product->get_id(), '_is_new', true ) == 'yes'
) {
$new = martfury_get_option( 'new_text' );
if ( ! $new ) {
$new = esc_html__( 'New', 'martfury' );
}
$output[] = '<span class="newness ribbon">' . esc_html( $new ) . '</span>';
}
}
if ( $output ) {
printf( '<span class="ribbons ok">%s</span>', implode( '', $output ) );
}
}
}
add_action( 'after_setup_theme', function() {
if ( function_exists( 'martfury_get_option' ) && function_exists( 'martfury_child_product_ribbons' ) ) {
remove_all_actions( 'woocommerce_after_product_gallery' );
if ( intval( martfury_get_option( 'single_product_badges' ) ) ) {
add_action( 'woocommerce_after_product_gallery', 'martfury_child_product_ribbons', 100 );
}
remove_all_actions( 'martfury_after_product_loop_thumbnail' );
if ( intval( martfury_get_option( 'show_badges' ) ) ) {
add_action( 'martfury_after_product_loop_thumbnail', 'martfury_child_product_ribbons', 100 );
}
}
});
}