| 1 |
<?php |
| 2 |
// phpcs:disable |
| 3 |
|
| 4 |
// Ensure the file is not accessed directly. |
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Disco Compatibility |
| 11 |
* |
| 12 |
* Excludes products from Disco discounts when they are managed by other plugins. |
| 13 |
* To exclude a product, hook into `disco_exclude_product_from_discount` and return true. |
| 14 |
* |
| 15 |
* Example: |
| 16 |
* add_filter( 'disco_exclude_product_from_discount', function( $exclude, $product_id, $product ) { |
| 17 |
* return my_plugin_is_special_product( $product_id ) ? true : $exclude; |
| 18 |
* }, 10, 3 ); |
| 19 |
*/ |
| 20 |
|
| 21 |
/** |
| 22 |
* Woo Donations Pro — exclude donation products from Disco discounts. |
| 23 |
* |
| 24 |
* The donation product has a base price of 0 in the database; its real price |
| 25 |
* is set dynamically at runtime via set_price(). Applying a Disco discount |
| 26 |
* would zero it out or produce an incorrect amount. |
| 27 |
*/ |
| 28 |
|
| 29 |
/** |
| 30 |
* WooCommerce TM Extra Product Options — preserve add-on price through Disco's filter. |
| 31 |
* |
| 32 |
* Root cause: disco_discounted_price() (priority 999 on woocommerce_product_get_price) |
| 33 |
* calls CalcFactory::get_product_price() which always fetches the base price from post |
| 34 |
* meta (_price), ignoring the runtime price TM EPO set via set_price() to include extra |
| 35 |
* options. This strips the options cost from every get_price() call in the cart. |
| 36 |
* |
| 37 |
* Fix: after TM EPO populates cart items from session we record each product object's |
| 38 |
* extra options price keyed by object hash. A filter at priority 1000 then adds it back |
| 39 |
* after Disco has applied its discount to the base price. |
| 40 |
*/ |
| 41 |
if ( defined( 'THEMECOMPLETE_EPO_PLUGIN_FILE' ) ) { |
| 42 |
|
| 43 |
add_filter( 'woocommerce_get_cart_item_from_session', 'disco_compat_tm_epo_track_options_price', 99999, 1 ); |
| 44 |
|
| 45 |
/** |
| 46 |
* Record the TM EPO extra options price for this cart item's product object. |
| 47 |
* |
| 48 |
* @param array $cart_item Cart item data (already processed by TM EPO at priority 9999). |
| 49 |
* @return array Unchanged cart item. |
| 50 |
*/ |
| 51 |
function disco_compat_tm_epo_track_options_price( array $cart_item ): array { |
| 52 |
global $disco_tm_epo_options_prices; |
| 53 |
|
| 54 |
if ( empty( $cart_item['tmcartepo'] ) || ! isset( $cart_item['data'] ) ) { |
| 55 |
return $cart_item; |
| 56 |
} |
| 57 |
|
| 58 |
$options_price = isset( $cart_item['tm_epo_options_prices'] ) |
| 59 |
? (float) $cart_item['tm_epo_options_prices'] |
| 60 |
: 0.0; |
| 61 |
|
| 62 |
if ( $options_price !== 0.0 ) { |
| 63 |
$disco_tm_epo_options_prices[ spl_object_hash( $cart_item['data'] ) ] = $options_price; |
| 64 |
} |
| 65 |
|
| 66 |
return $cart_item; |
| 67 |
} |
| 68 |
|
| 69 |
add_filter( 'woocommerce_product_get_price', 'disco_compat_tm_epo_restore_options_price', 1000, 2 ); |
| 70 |
add_filter( 'woocommerce_product_variation_get_price', 'disco_compat_tm_epo_restore_options_price', 1000, 2 ); |
| 71 |
|
| 72 |
/** |
| 73 |
* Add TM EPO's extra options price back after Disco's filter (priority 999) has run. |
| 74 |
* |
| 75 |
* Disco returns the discounted base price; we add the options price on top so the |
| 76 |
* cart total correctly reflects base-price discount + full options cost. |
| 77 |
* |
| 78 |
* @param float|string $price Price value after all earlier filters. |
| 79 |
* @param \WC_Product $product Product object being priced. |
| 80 |
* @return float|string |
| 81 |
*/ |
| 82 |
function disco_compat_tm_epo_restore_options_price( $price, WC_Product $product ) { |
| 83 |
global $disco_tm_epo_options_prices; |
| 84 |
|
| 85 |
if ( empty( $disco_tm_epo_options_prices ) ) { |
| 86 |
return $price; |
| 87 |
} |
| 88 |
|
| 89 |
$hash = spl_object_hash( $product ); |
| 90 |
|
| 91 |
if ( isset( $disco_tm_epo_options_prices[ $hash ] ) ) { |
| 92 |
$price = (float) $price + $disco_tm_epo_options_prices[ $hash ]; |
| 93 |
} |
| 94 |
|
| 95 |
return $price; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
if ( function_exists( 'wdpgk_get_wc_donation_setting' ) ) { |
| 100 |
|
| 101 |
add_filter( 'disco_exclude_product_from_discount', 'disco_compat_exclude_woo_donations_product', 10, 3 ); |
| 102 |
|
| 103 |
/** |
| 104 |
* Exclude Woo Donations Pro products from Disco discounts. |
| 105 |
* |
| 106 |
* @param bool $exclude Whether the product is already excluded. |
| 107 |
* @param int $product_id The product ID being evaluated |
| 108 |
* |
| 109 |
* @return bool True if the product should be excluded, false otherwise. |
| 110 |
*/ |
| 111 |
function disco_compat_exclude_woo_donations_product( bool $exclude, int $product_id ): bool { |
| 112 |
|
| 113 |
if ( $exclude ) { |
| 114 |
return $exclude; |
| 115 |
} |
| 116 |
|
| 117 |
static $donation_product_id = null; |
| 118 |
|
| 119 |
if ( $donation_product_id === null ) { |
| 120 |
$options = wdpgk_get_wc_donation_setting(); |
| 121 |
$donation_product_id = isset( $options['Product'] ) ? (int) $options['Product'] : 0; |
| 122 |
} |
| 123 |
|
| 124 |
if ( $donation_product_id > 0 && $product_id === $donation_product_id ) { |
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
if ( get_post_meta( $product_id, '_donatable', true ) === 'yes' ) { |
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
|