To fix sale badges are not shown while using jaroti v6.4.2
Fix sale badges not displaying with Jaroti v6.4.2.
if (!function_exists('jaroti_child_template_loop_product_label')) {
function jaroti_child_template_loop_product_label(){
global $product;
if (function_exists('jaroti_get_theme_options')) {
$theme_options = jaroti_get_theme_options();
?>
<div class="product-label">
<?php
if( $product->is_in_stock() ){
/* New label */
if( $theme_options['ts_product_show_new_label'] ){
$now = current_time( 'timestamp', true );
$post_date = get_post_time('U', true);
$num_day = (int)( ( $now - $post_date ) / ( 3600*24 ) );
$num_day_setting = absint( $theme_options['ts_product_show_new_label_time'] );
if( $num_day <= $num_day_setting ){
echo '<span class="new"><span>'.esc_html($theme_options['ts_product_new_label_text']).'</span></span>';
}
}
/* Hot label */
if( $product->is_featured() ){
echo '<span class="featured"><span>'.esc_html($theme_options['ts_product_feature_label_text']).'</span></span>';
}
/* Sale label */
if( $product->is_on_sale() ){
if( $theme_options['ts_show_sale_label_as'] != 'text' ) {
$discount_percentage = apply_filters('advanced_woo_discount_rules_get_product_discount_percentage', 0, $product);
if (!empty($discount_percentage)) {
$percentage = round($discount_percentage) . '%';
if ($product->get_type() == 'variable') {
$regular_price = $product->get_variation_regular_price('max');
$sale_price = $product->get_variation_sale_price('min');
$percentages = array();
// Get all variation prices.
$prices = $product->get_variation_prices();
// Loop through variation prices.
foreach ($prices['price'] as $key => $price) {
// Only on sale variations.
if ($prices['regular_price'][$key] !== $price && $prices['regular_price'][$key] > 0.005) {
// Calculate and set in the array the percentage for each variation on sale.
$percentages[] = round(100 - ($prices['sale_price'][$key] / $prices['regular_price'][$key] * 100));
}
}
// Keep the highest value.
if (!empty($percentages)) {
$percentage = max($percentages) . '%';
}
} else {
$sale_price = $product->get_price();
$percentage = 0;
$regular_price = (float)$product->get_regular_price();
if ($regular_price > 0.005) {
$sale_price = (float)$product->get_price();
$percentage = round(100 - ($sale_price / $regular_price * 100), 0) . '%';
}
}
if ($regular_price) {
if ($theme_options['ts_show_sale_label_as'] == 'number') {
$_off_price = round($regular_price - $sale_price, wc_get_price_decimals());
$price_display = '-' . sprintf(get_woocommerce_price_format(), get_woocommerce_currency_symbol(), $_off_price);
echo '<span class="onsale amount" data-original="' . $price_display . '"><span>' . $price_display . '</span></span>';
}
if ($theme_options['ts_show_sale_label_as'] == 'percent') {
echo '<span class="onsale percent"><span>-' . $percentage . '</span></span>';
}
}
} else {
if( $product->get_type() == 'variable' ){
$regular_price = $product->get_variation_regular_price('max');
$sale_price = $product->get_variation_sale_price('min');
}
else{
$regular_price = $product->get_regular_price();
$sale_price = $product->get_price();
}
if( $regular_price ){
if( $theme_options['ts_show_sale_label_as'] == 'number' ){
$_off_price = round($regular_price - $sale_price, wc_get_price_decimals());
$price_display = '-' . sprintf(get_woocommerce_price_format(), get_woocommerce_currency_symbol(), $_off_price);
echo '<span class="onsale amount" data-original="'.$price_display.'"><span>'.$price_display.'</span></span>';
}
if( $theme_options['ts_show_sale_label_as'] == 'percent' ){
echo '<span class="onsale percent"><span>-'.jaroti_calc_discount_percent($regular_price, $sale_price).'%</span></span>';
}
}
}
}
else{
echo '<span class="onsale"><span>'.esc_html($theme_options['ts_product_sale_label_text']).'</span></span>';
}
}
}
else{ /* Out of stock */
echo '<span class="out-of-stock"><span>'.esc_html($theme_options['ts_product_out_of_stock_label_text']).'</span></span>';
}
?>
</div>
<?php
}
}
add_action('after_setup_theme', function () {
remove_action('woocommerce_after_shop_loop_item_title', 'jaroti_template_loop_product_label', 1);
remove_action('woocommerce_product_thumbnails', 'jaroti_template_loop_product_label', 99);
add_action('woocommerce_before_shop_loop_item_title', 'jaroti_child_template_loop_product_label', 1);
add_action('woocommerce_single_product_summary', 'jaroti_child_template_loop_product_label', 77);
});
}