| 1 |
<?php // phpcs:disable |
| 2 |
/** |
| 3 |
* Disco |
| 4 |
* |
| 5 |
* @package Disco |
| 6 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 7 |
* @link http://domain.tld |
| 8 |
* @license GPL 2.0+ |
| 9 |
* @copyright 2022 WebAppick |
| 10 |
*/ |
| 11 |
|
| 12 |
// Ensure the file is not accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
use Disco\App\Disco; |
| 18 |
use Disco\App\Utility\Settings; |
| 19 |
|
| 20 |
if ( !function_exists( 'disco_get_discounted_price' ) ) { |
| 21 |
|
| 22 |
/** |
| 23 |
* Get the discounted price of a product. |
| 24 |
* |
| 25 |
* @param float $price Product Price. |
| 26 |
* @param \WC_Product $product Product Object. |
| 27 |
* @return float |
| 28 |
*/ |
| 29 |
function disco_get_discounted_price( $price, $product ) { |
| 30 |
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { |
| 31 |
return $price; // Return original price in admin area. |
| 32 |
} |
| 33 |
|
| 34 |
return ( new Disco )->get_product_discounted_price( $price, $product ); |
| 35 |
} |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
if ( ! function_exists( 'disco_discounted_price' ) ) { |
| 40 |
|
| 41 |
/** |
| 42 |
* Applies discounted price logic to product prices (simple + variation). |
| 43 |
* |
| 44 |
* @param float $price Product Price. |
| 45 |
* @param \WC_Product $product Product Object. |
| 46 |
* @return float |
| 47 |
*/ |
| 48 |
function disco_discounted_price( $price, $product ) { |
| 49 |
static $running = false; |
| 50 |
|
| 51 |
if ( $running || ! is_object( $product ) || ! $price ) { |
| 52 |
return $price; |
| 53 |
} |
| 54 |
|
| 55 |
$running = true; |
| 56 |
|
| 57 |
// Remove other filters that might interfere (safety for sale override). |
| 58 |
remove_filter( 'woocommerce_product_get_sale_price', '__return_false' ); |
| 59 |
remove_filter( 'woocommerce_product_get_price', '__return_false' ); |
| 60 |
|
| 61 |
$discounted_price = disco_get_discounted_price( $price, $product ); |
| 62 |
|
| 63 |
// Respect WooCommerce native sale price if it's lower |
| 64 |
if ( $product->get_type() !== 'variable' ) { |
| 65 |
$sale_price = $product->get_sale_price(); |
| 66 |
|
| 67 |
if ( $sale_price && $sale_price < $discounted_price ) { |
| 68 |
$discounted_price = $sale_price; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
$running = false; |
| 73 |
|
| 74 |
return round( $discounted_price, wc_get_price_decimals() ); |
| 75 |
} |
| 76 |
|
| 77 |
// Apply to simple and variation product prices |
| 78 |
add_filter( 'woocommerce_product_get_price', 'disco_discounted_price', PHP_INT_MAX, 2 ); |
| 79 |
add_filter( 'woocommerce_product_get_sale_price', 'disco_discounted_price', PHP_INT_MAX, 2 ); |
| 80 |
add_filter( 'woocommerce_product_variation_get_price', 'disco_discounted_price', PHP_INT_MAX, 2 ); |
| 81 |
add_filter( 'woocommerce_product_variation_get_sale_price', 'disco_discounted_price', PHP_INT_MAX, 2 ); |
| 82 |
} |
| 83 |
|
| 84 |
if ( ! function_exists( 'disco_variable_product_discounted_price_html' ) ) { |
| 85 |
|
| 86 |
/** |
| 87 |
* Show variable price range with discount — includes tax if tax is enabled. |
| 88 |
* |
| 89 |
* @param string $price_html Price HTML. |
| 90 |
* @param \WC_Product $product Product Object. |
| 91 |
* @return string |
| 92 |
*/ |
| 93 |
function disco_variable_product_discounted_price_html( $price_html, $product ) { |
| 94 |
if ( ! $product instanceof WC_Product_Variable ) { |
| 95 |
return $price_html; |
| 96 |
} |
| 97 |
|
| 98 |
$variation_ids = $product->get_children(); |
| 99 |
$regular_prices = array(); |
| 100 |
$discounted_prices = array(); |
| 101 |
|
| 102 |
$display_include_tax = get_option( 'woocommerce_tax_display_shop' ) === 'incl'; |
| 103 |
$tax_enabled = wc_tax_enabled(); |
| 104 |
|
| 105 |
foreach ( $variation_ids as $variation_id ) { |
| 106 |
$variation = wc_get_product( $variation_id ); |
| 107 |
|
| 108 |
if ( ! $variation || ! $variation->is_purchasable() ) { |
| 109 |
continue; |
| 110 |
} |
| 111 |
|
| 112 |
if ( $tax_enabled && $display_include_tax ) { |
| 113 |
$regular_price = wc_get_price_including_tax( $variation, [ 'price' => $variation->get_regular_price() ] ); |
| 114 |
$sale_price = wc_get_price_including_tax( $variation, [ 'price' => $variation->get_price() ] ); |
| 115 |
} else { |
| 116 |
$regular_price = (float) $variation->get_regular_price(); |
| 117 |
$sale_price = (float) $variation->get_price(); |
| 118 |
} |
| 119 |
|
| 120 |
if ( $regular_price <= 0 ) { |
| 121 |
continue; |
| 122 |
} |
| 123 |
|
| 124 |
$regular_prices[] = $regular_price; |
| 125 |
$discounted_prices[] = $sale_price; |
| 126 |
} |
| 127 |
|
| 128 |
if ( empty( $regular_prices ) || empty( $discounted_prices ) ) { |
| 129 |
return $price_html; |
| 130 |
} |
| 131 |
|
| 132 |
$regular_min = min( $regular_prices ); |
| 133 |
$regular_max = max( $regular_prices ); |
| 134 |
$discounted_min = min( $discounted_prices ); |
| 135 |
$discounted_max = max( $discounted_prices ); |
| 136 |
|
| 137 |
$is_discount_applied = ( |
| 138 |
round( $regular_min, 2 ) > round( $discounted_min, 2 ) || |
| 139 |
round( $regular_max, 2 ) > round( $discounted_max, 2 ) |
| 140 |
); |
| 141 |
|
| 142 |
if ( ! $is_discount_applied ) { |
| 143 |
return $price_html; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* If min and max prices are the same, show single price. |
| 148 |
* Otherwise, show price range. |
| 149 |
* Strike-through regular price based on Disco settings. |
| 150 |
*/ |
| 151 |
if ( round( $discounted_min, 2 ) === round( $discounted_max, 2 ) ) { |
| 152 |
if ( ! Disco::is_strike_through_applicable( Disco::get_page_name() ) ) { |
| 153 |
return wc_price( $discounted_min ); |
| 154 |
} |
| 155 |
return '<del>' . wc_price( $regular_min ) . '</del> <ins>' . wc_price( $discounted_min ) . '</ins>'; |
| 156 |
} |
| 157 |
|
| 158 |
$discount_range = wc_price( $discounted_min ) . ' – ' . wc_price( $discounted_max ); |
| 159 |
$regular_range = wc_price( $regular_min ) . ' – ' . wc_price( $regular_max ); |
| 160 |
|
| 161 |
if( ! Disco::is_strike_through_applicable( Disco::get_page_name() ) ) { |
| 162 |
return $discount_range; |
| 163 |
} |
| 164 |
|
| 165 |
return '<del>' . $regular_range . '</del><br><ins>' . $discount_range . '</ins>'; |
| 166 |
} |
| 167 |
|
| 168 |
add_filter( 'woocommerce_variable_price_html', 'disco_variable_product_discounted_price_html', PHP_INT_MAX, 2 ); |
| 169 |
} |
| 170 |
|
| 171 |
if ( ! function_exists( 'disco_discounted_price_html' ) ) { |
| 172 |
|
| 173 |
/** |
| 174 |
* Display Product Sale Price & Regular Price with or without Tax. |
| 175 |
* |
| 176 |
* @param string $price_html Product Price HTML. |
| 177 |
* @param \WC_Product $product Product Object. |
| 178 |
*/ |
| 179 |
function disco_discounted_price_html( string $price_html, WC_Product $product ): string { |
| 180 |
|
| 181 |
if ( ! $product || ! is_a( $product, 'WC_Product' ) ) { |
| 182 |
return $price_html; |
| 183 |
} |
| 184 |
|
| 185 |
if( $product->get_type() === 'variable' ) { |
| 186 |
return $price_html; |
| 187 |
} |
| 188 |
|
| 189 |
$display_include_tax = get_option( 'woocommerce_tax_display_shop' ) === 'incl'; |
| 190 |
$tax_enabled = wc_tax_enabled(); |
| 191 |
|
| 192 |
if ( $tax_enabled && $display_include_tax ) { |
| 193 |
$regular_price = wc_get_price_including_tax( $product, array( 'price' => $product->get_regular_price() ) ); |
| 194 |
$sale_price = wc_get_price_including_tax( $product, array( 'price' => $product->get_price() ) ); |
| 195 |
} else { |
| 196 |
$regular_price = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_regular_price() ) ); |
| 197 |
$sale_price = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_price() ) ); |
| 198 |
} |
| 199 |
|
| 200 |
// $disco_on_sale_status = get_post_meta( $product->get_id(), '_disco_on_sale', true ); |
| 201 |
$page_name = Disco::get_page_name(); |
| 202 |
|
| 203 |
if ( |
| 204 |
! Disco::is_strike_through_applicable( $page_name ) && |
| 205 |
in_array( Settings::get( 'on_sale_badge' ), [ 'show_all', 'do_not_show' ], true ) |
| 206 |
) { |
| 207 |
return wc_price( $sale_price ); |
| 208 |
} |
| 209 |
|
| 210 |
if ( $sale_price < $regular_price ) { |
| 211 |
$price_html = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . '</ins>'; |
| 212 |
} |
| 213 |
|
| 214 |
return $price_html; |
| 215 |
} |
| 216 |
|
| 217 |
add_filter( 'woocommerce_get_price_html', 'disco_discounted_price_html', PHP_INT_MAX, 2 ); |
| 218 |
} |
| 219 |
|
| 220 |
if ( ! function_exists( 'disco_show_product_as_on_sale' ) ) { |
| 221 |
|
| 222 |
/** |
| 223 |
* Show variable product as on sale if any variation is on sale. |
| 224 |
* |
| 225 |
* @param bool $is_on_sale Whether the product is on sale. |
| 226 |
* @param \WC_Product $product Product Object. |
| 227 |
* @return bool |
| 228 |
*/ |
| 229 |
function disco_show_product_as_on_sale( $is_on_sale, $product ) { |
| 230 |
if ( ! $product || ! is_a( $product, 'WC_Product' ) ) { |
| 231 |
return $is_on_sale; |
| 232 |
} |
| 233 |
|
| 234 |
$on_sale_status = Settings::get( 'on_sale_badge' ); |
| 235 |
|
| 236 |
//Show on sale badge based on WooCommerce and Disco settings. |
| 237 |
if ( 'do_not_show' === $on_sale_status ) { |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
if( 'show_all' === $on_sale_status ) { |
| 242 |
if ( $product->is_type( 'variable' ) ) { |
| 243 |
$children = $product->get_children(); |
| 244 |
|
| 245 |
foreach ( $children as $variation_id ) { |
| 246 |
$variation = wc_get_product( $variation_id ); |
| 247 |
|
| 248 |
if ( ! $variation || ! $variation->is_purchasable() ) { |
| 249 |
continue; |
| 250 |
} |
| 251 |
|
| 252 |
$regular_price = (float) $variation->get_regular_price(); |
| 253 |
$current_price = (float) $variation->get_price(); // This includes filters (like discounts) |
| 254 |
|
| 255 |
// If the variation has a price reduction (dynamic or static) |
| 256 |
if ( $regular_price > 0 && $current_price < $regular_price ) { |
| 257 |
return true; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
// For simple products, check if the price is discounted. |
| 263 |
$regular_price = (float) $product->get_regular_price(); |
| 264 |
$current_price = (float) $product->get_price(); // This includes filters (like discounts) |
| 265 |
// If the product has a price reduction (dynamic or static) |
| 266 |
if ( $regular_price > 0 && $current_price < $regular_price ) { |
| 267 |
return true; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
return $is_on_sale; |
| 272 |
} |
| 273 |
|
| 274 |
add_filter( 'woocommerce_product_is_on_sale', 'disco_show_product_as_on_sale', 20, 2 ); |
| 275 |
} |
| 276 |
|