Add Discount percentage to Wolmart theme sale badge
Display discount percentages on Wolmart theme sale badges.
add_action('wp_loaded', function() {
remove_all_filters('woocommerce_sale_flash');
add_filter('woocommerce_sale_flash', function($html, $post, $product) {
if (function_exists('wolmart_wc_get_loop_prop') && function_exists('wolmart_get_option')) {
$html = '';
$show_labels = wolmart_wc_get_loop_prop( 'show_labels', array( 'hot', 'stock', 'sale', 'new' ) );
if ( is_array( $show_labels ) && count( $show_labels ) ) {
// Featured label
if ( $product->is_featured() && in_array( 'hot', $show_labels ) ) {
$html .= '<label class="product-label label-featured">' . esc_html__( 'Hot', 'wolmart' ) . '</label>';
}
// New label
if ( in_array( 'new', $show_labels ) && strtotime( $product->get_date_created() ) > strtotime( '-' . (int) wolmart_get_option( 'new_product_period' ) . ' day' ) ) {
$html .= '<label class="product-label label-new">' . esc_html__( 'New', 'wolmart' ) . '</label>';
}
// Sale label
if (in_array( 'sale', $show_labels ) ) {
$percentage = 0;
if ($wdr_discount_percentage = apply_filters('advanced_woo_discount_rules_get_product_discount_percentage', 0, $product)) {
$percentage = $wdr_discount_percentage;
} elseif ($product->is_on_sale()) {
if ( $product->get_regular_price() ) {
$percentage = round( ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100 );
} elseif ( 'variable' == $product->get_type() && $product->get_variation_regular_price() ) {
$percentage = round( ( ( $product->get_variation_regular_price() - $product->get_variation_sale_price() ) / $product->get_variation_regular_price() ) * 100 );
}
}
if ( $percentage ) {
/* translators: %d represents sale rate. */
$html .= '<label class="product-label label-sale">' . sprintf( esc_html__( '%d%% off', 'wolmart' ), $percentage ) . '</label>';
}
}
// Out of stock label
if ( in_array( 'stock', $show_labels ) && 'outofstock' == $product->get_stock_status() ) {
$html .= '<label class="product-label label-stock">' . esc_html__( 'Out of Stock', 'wolmart' ) . '</label>';
}
}
// Show custom labels
$custom_labels = json_decode( get_post_meta( get_the_ID(), 'wolmart_custom_labels', true ), true );
if ( is_array( $custom_labels ) ) {
foreach ( $custom_labels as $custom_label ) {
if ( $custom_label['type'] ) {
if ( isset( $custom_label['img_id'] ) ) {
$html .= '<label class="product-label label-img">';
$att_id = $custom_label['img_id'];
$html .= wp_get_attachment_image( $att_id );
$html .= '</label>';
}
} else {
$style_escaped = ' style="';
if ( ! empty( $custom_label['color'] ) ) {
$style_escaped .= 'color:' . esc_attr( $custom_label['color'] ) . ';';
} else {
$style_escaped .= 'color: #fff;';
}
if ( ! empty( $custom_label['bgColor'] ) ) {
$style_escaped .= 'background-color:' . esc_attr( $custom_label['bgColor'] );
} else {
$style_escaped .= 'background-color: ' . esc_attr( wolmart_get_option( 'primary_color' ) );
}
$style_escaped .= '"';
$html .= '<label class="product-label"' . $style_escaped . '>';
$html .= wolmart_strip_script_tags( $custom_label['label'] );
$html .= '</label>';
}
}
}
if ( $html ) {
$html = '<div class="product-label-group' . esc_attr( apply_filters( 'wolmart_product_label_group_class', '' ) ) . '">' . $html . '</div>';
}
}
return $html;
}, 1000, 3);
}, 1000);