| 1 |
<?php |
| 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 |
|
| 19 |
if ( !function_exists( 'disco_get_discounted_price' ) ) { |
| 20 |
|
| 21 |
/** |
| 22 |
* Get the discounted price of a product. |
| 23 |
* |
| 24 |
* @param float $price Product Price. |
| 25 |
* @param \WC_Product $product Product Object. |
| 26 |
* @return float |
| 27 |
*/ |
| 28 |
function disco_get_discounted_price( $price, $product ) { |
| 29 |
return ( new Disco )->get_product_discounted_price( $price, $product ); |
| 30 |
} |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
if ( ! function_exists( 'disco_update_product_price_by_campaigns' ) ) { |
| 35 |
|
| 36 |
/** |
| 37 |
* Run the Product Intent campaigns and get the sale price before product. |
| 38 |
* |
| 39 |
* @param string $price Product Price. |
| 40 |
* @param \WC_Product $product Product Object. |
| 41 |
* @return float |
| 42 |
*/ |
| 43 |
function disco_discounted_price( $price, $product ) { |
| 44 |
static $running = false; |
| 45 |
|
| 46 |
if ( $running ) { |
| 47 |
return $price; |
| 48 |
} |
| 49 |
|
| 50 |
$running = true; |
| 51 |
|
| 52 |
// Make below filters active to get the sale price. |
| 53 |
remove_filter( 'woocommerce_product_get_sale_price', '__return_false' ); |
| 54 |
remove_filter( 'woocommerce_product_get_price', '__return_false' ); |
| 55 |
remove_filter( 'woocommerce_product_variation_get_price', '__return_false' ); |
| 56 |
remove_filter( 'woocommerce_variation_prices_sale_price', '__return_false' ); |
| 57 |
|
| 58 |
$discounted_price = disco_get_discounted_price( $price, $product ); |
| 59 |
|
| 60 |
$running = false; |
| 61 |
|
| 62 |
return $discounted_price; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
add_filter( 'woocommerce_product_get_price', 'disco_discounted_price', PHP_INT_MAX, 2 ); |
| 67 |
add_filter( 'woocommerce_product_get_sale_price', 'disco_discounted_price', PHP_INT_MAX, 2 ); |
| 68 |
add_filter( 'woocommerce_product_variation_get_price', 'disco_discounted_price', PHP_INT_MAX, 2 ); |
| 69 |
add_filter( 'woocommerce_variation_prices_sale_price', 'disco_discounted_price', PHP_INT_MAX, 2 ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( ! function_exists( 'disco_variable_product_discounted_price' ) ) { |
| 73 |
|
| 74 |
/** |
| 75 |
* Run the Product Intent campaigns and get the min max price of variable product before product. |
| 76 |
* |
| 77 |
* @param string $price_html Product Price. |
| 78 |
* @param \WC_Product $product Product Object. |
| 79 |
* @return string |
| 80 |
*/ |
| 81 |
function disco_variable_product_discounted_price( $price_html, $product ) { |
| 82 |
// Get the minimum and maximum regular prices of variations |
| 83 |
$regular_price_min = $product->get_variation_price( 'min', true ); |
| 84 |
$regular_price_max = $product->get_variation_price( 'max', true ); |
| 85 |
// Get variable min and max price. |
| 86 |
$formatted_min_price = wc_price( disco_get_discounted_price( $regular_price_min, $product ) ); |
| 87 |
$formatted_max_price = wc_price( disco_get_discounted_price( $regular_price_max, $product ) ); |
| 88 |
|
| 89 |
// If min and max prices are equal then return min price. |
| 90 |
if ( $formatted_min_price === $formatted_max_price ) { |
| 91 |
return $formatted_min_price; |
| 92 |
} |
| 93 |
|
| 94 |
// Display price range in frontend |
| 95 |
return $formatted_min_price . ' - ' . $formatted_max_price; |
| 96 |
} |
| 97 |
|
| 98 |
add_filter( 'woocommerce_variable_price_html', 'disco_variable_product_discounted_price', PHP_INT_MAX, 2 ); |
| 99 |
} |
| 100 |
|
| 101 |
if ( ! function_exists( 'disco_discounted_price_html' ) ) { |
| 102 |
|
| 103 |
/** |
| 104 |
* Display Product Sale Price & Regular Price. |
| 105 |
* |
| 106 |
* @param string $price_html Product Price HTML. |
| 107 |
* @param \WC_Product $product Product Object. |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
function disco_discounted_price_html( $price_html, $product ) { |
| 111 |
$regular_price = $product->get_regular_price(); |
| 112 |
$sale_price = $product->get_price(); |
| 113 |
|
| 114 |
if ( $sale_price < $regular_price ) { |
| 115 |
// Format the sale price HTML |
| 116 |
$price_html = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . '</ins>'; |
| 117 |
} |
| 118 |
|
| 119 |
return $price_html; |
| 120 |
} |
| 121 |
|
| 122 |
add_filter( 'woocommerce_get_price_html', 'disco_discounted_price_html', PHP_INT_MAX, 2 ); |
| 123 |
} |
| 124 |
|