Created
February 10, 2023 11:21
-
-
Save sarancartrabbit/47401c055ad6f607cf558c99610b862e to your computer and use it in GitHub Desktop.
Discount rules v2: To show the both sale badges
This file contains 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
<?php | |
/** | |
* Product loop sale flash | |
* | |
* This template can be overridden by copying it to yourtheme/woocommerce/loop/sale-flash.php. | |
* | |
* HOWEVER, on occasion WooCommerce will need to update template files and you | |
* (the theme developer) will need to copy the new files to your theme to | |
* maintain compatibility. We try to do this as little as possible, but it does | |
* happen. When this occurs the version of the template file will be bumped and | |
* the readme will list any important changes. | |
* | |
* @see https://docs.woocommerce.com/document/template-structure/ | |
* @package WooCommerce/Templates | |
* @version 1.6.4 | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly | |
} | |
if ( 'widget' === wc_get_loop_prop( 'product_type' ) || 'rotate' == riode_get_single_product_layout() ) { | |
return; | |
} | |
$show_labels = wc_get_loop_prop( 'show_labels', array( 'top', 'stock', 'sale', 'new', 'custom' ) ); | |
if ( ! is_array( $show_labels ) || 0 == count( $show_labels ) ) { | |
return; | |
} | |
if ( ! riode_wc_show_info_for_role( 'label' ) ) { | |
return; | |
} | |
global $post, $product; | |
$html = ''; | |
// Featured Product | |
if ( $product->is_featured() && in_array( 'top', $show_labels ) ) { | |
$top_label = riode_get_option( 'product_top_label' ); | |
$html .= '<label class="product-label label-top" title="' . esc_html__( 'Featured Product', 'riode' ) . '">' . $top_label . '</label>'; | |
} | |
// Sale Product | |
if ( $product->is_on_sale() && in_array( 'sale', $show_labels ) ) { | |
if ($product->get_type() == 'variable') { | |
$max_percentage = 0; | |
$available_variations = $product->get_variation_prices(); | |
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, 'all', true, false); | |
if ($discount !== false && is_array($discount) && isset($discount['initial_price']) && isset($discount['discounted_price'])) { | |
$regular_price = $discount['initial_price']; | |
$sale_price = $discount['discounted_price']; | |
} | |
} | |
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') { | |
$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); | |
} | |
} | |
$sale_label = riode_get_option( 'product_sale_label' ); | |
if ( ! empty( $percentage ) && ! empty( $sale_label ) && false !== strpos( $sale_label, '%percent%' ) ) { | |
$percentage .= '%'; | |
$sale_html = str_replace( '%percent%', $percentage, $sale_label ); | |
$html .= '<label class="product-label label-sale" title="' . esc_html__( 'On-Sale Product', 'riode' ) . '">' . $sale_html . '</label>'; | |
} | |
} | |
// Out of Stock | |
if ( in_array( 'stock', $show_labels ) && 'outofstock' == $product->get_stock_status() ) { | |
$stock_label = riode_get_option( 'product_stock_label' ); | |
$html .= '<label class="product-label label-stock" title="' . esc_html__( 'Out-of-Stock Product', 'riode' ) . '">' . $stock_label . '</label>'; | |
} | |
// New Product | |
if ( in_array( 'new', $show_labels ) && strtotime( $product->get_date_created() ) > strtotime( apply_filters( 'riode_product_period', '-' . (int) riode_get_option( 'product_period' ) . ' day' ) ) ) { | |
$period_label = riode_get_option( 'product_new_label' ); | |
$html .= '<label class="product-label label-new" title="' . esc_html__( 'New Product', 'riode' ) . '">' . $period_label . '</label>'; | |
} | |
// Custom Labels | |
if ( in_array( 'custom', $show_labels ) && riode_get_option( 'product_custom_label' ) ) { | |
$custom_labels = get_post_meta( get_the_ID(), 'riode_custom_labels', true ); | |
if ( is_array( $custom_labels ) && count( $custom_labels ) ) { | |
foreach ( $custom_labels as $custom_label ) { | |
$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( riode_get_option( 'primary_color' ) ); | |
} | |
$style_escaped .= '"'; | |
$html .= '<label class="product-label"' . $style_escaped . '>'; | |
$html .= $custom_label['label']; | |
$html .= '</label>'; | |
} | |
} | |
} | |
// Video & 360 Thumbnail Label | |
$label_video = ( riode_get_option( 'product_video_thumbnail' ) && ! riode_is_product() ) && ( ! empty( get_post_meta( $product->get_ID(), 'riode_product_video_thumbnails' ) ) || get_post_meta( $product->get_ID(), 'riode_product_video_thumbnail_shortcode', true ) ); | |
$label_360 = ( riode_get_option( 'product_360_thumbnail' ) && ! riode_is_product() ) && ( ! empty( get_post_meta( $product->get_ID(), 'riode_product_360_gallery' ) ) ); | |
if ( $label_video || $label_360 ) { | |
$html .= '<div class="product-thumb-type-labels">'; | |
if ( $label_video ) { | |
$html .= '<label class="product-label label-video" title="' . esc_html__( 'Has Video Thumbnail', 'riode' ) . '"><i class="d-icon-play-solid"></i></label>'; | |
} | |
if ( $label_360 ) { | |
$html .= '<label class="product-label label-360" title="' . esc_html__( 'Has 360 Degree Thumbnail', 'riode' ) . '"><i class="fas fa-sync"></i></label>'; | |
} | |
$html .= '</div>'; | |
} | |
// Finally, print labels | |
if ( $html ) { | |
$html = '<div class="product-label-group' . esc_attr( apply_filters( 'riode_product_label_group_class', '' ) ) . '">' . $html . '</div>'; | |
echo apply_filters( 'woocommerce_sale_flash', $html, $post, $product ); | |
} | |
/* Omit closing PHP tag at the end of PHP files to avoid "headers already sent" issues. */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment