PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.17
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.17
1.4.17 1.4.16 1.4.15 1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 All 180 releases
← All changes | app/Disco.php +62 -21 1.4.141.4.17 View file →
@@ -10,8 +10,9 @@
10 10 namespace Disco\App;
11 11
12 12 use Disco\App\Calc\CalcFactory;
13 13 use Disco\App\Features\UserLimit;
14 +use Disco\App\Utility\Config;
14 15 use Disco\App\Utility\Settings;
15 16
16 17 /**
17 18 * Class Disco
@@ -81,10 +82,30 @@
81 82 if ( empty( $discount ) ) {
82 83 continue;
83 84 }
84 85
85 - // Store discount keyed by campaign id so the winning amount maps back to the right campaign.
86 - $discounts[ $intent->campaign->id ] = $discount;
86 + /**
87 + * Normalize each campaign's amount before the amounts compete.
88 + *
89 + * A percent amount is derived from the product price, which a
90 + * currency switcher has already converted, so it arrives in the
91 + * active currency. A fixed amount is the rule value as typed, so
92 + * it arrives in the store's base currency. Comparing the two
93 + * directly picks a winner by exchange rate rather than by size:
94 + * percent scales with the rate and fixed does not, so `max` would
95 + * select percent on almost every product and `min` would select
96 + * fixed. Converting here, with each campaign's own discount type,
97 + * puts every amount in the same currency before min/max runs.
98 + *
99 + * Filtering per campaign also matches what CartIntent and the
100 + * Calc classes already do, so every path now compares like for
101 + * like.
102 + */
103 + $discounts[ $intent->campaign->id ] = (float) apply_filters(
104 + 'disco_final_discounted_amount',
105 + (float) $discount,
106 + $this->campaign_discount_type( $intent->campaign )
107 + );
87 108 }
88 109
89 110 // If no discount is applied, return the original price.
90 111 if ( empty( $discounts ) ) {
@@ -90,20 +111,12 @@
90 111 if ( empty( $discounts ) ) {
91 112 return $price;
92 113 }
93 114
94 - $discount_type = $intent->campaign->discount_rules[0]['discount_type'];
95 -
96 115 // Pick the winning discount amount, then map it back to the campaign that produced it.
97 - $base_discount = $this->min_max_average( array_values( $discounts ) );
98 - $this->applied_campaign_id = (int) array_search( $base_discount, $discounts, true );
116 + $discounted_amount = $this->min_max_average( array_values( $discounts ) );
117 + $this->applied_campaign_id = (int) array_search( $discounted_amount, $discounts, true );
99 118
100 - /**
101 - * Get the min or max discount amount according to plugin settings.
102 - * Apply the filter to modify final discounted amount based on discount types.
103 - */
104 - $discounted_amount = apply_filters( 'disco_final_discounted_amount', $base_discount, $discount_type );
105 -
106 119 if ( $discounted_amount <= $price ) {
107 120 // Get an applied campaign from DiscountLimit class.
108 121 ( new UserLimit )->disco_start_session_on_checkout( $this->applied_campaign_id );
109 122
@@ -257,21 +270,21 @@
257 270 * @param \WC_Cart $cart WooCommerce cart object to calculate discounts for.
258 271 * @return array|false|\WC_Cart Cart object with applied discounts or false if no discounts are applicable.
259 272 */
260 273 public function get_cart_items_discount_for_bogo( $cart ) {//phpcs:ignore
261 - if ( ! $this->cart_is_valid() ) {
262 - return $cart;
263 - }
274 + if ( ! $this->cart_is_valid() ) {
275 + return $cart;
276 + }
264 277
265 - if ( ! defined( 'DOING_AJAX' ) && is_admin() ) {
266 - return $cart;
267 - }
278 + if ( ! defined( 'DOING_AJAX' ) && is_admin() ) {
279 + return $cart;
280 + }
268 281
269 - // Init Cart - Based Intents except Product & Shipping Intent.
270 - $this->intents = $this->prepare_intents( array( 'BOGO' ) );
282 + // Init Cart - Based Intents except Product & Shipping Intent.
283 + $this->intents = $this->prepare_intents( array( 'BOGO' ) );
271 284
272 285 return $this->prepare_item_discounts_bogo_free( $this->intents, $cart );
273 - }
286 + }
274 287
275 288 /**
276 289 * Apply the discount to shipping.
277 290 *
@@ -441,7 +454,35 @@
441 454 return false;
442 455 }
443 456
444 457 return in_array( $page_name, $pages, true );
458 + }
459 +
460 + /**
461 + * Resolve a campaign's discount type.
462 + *
463 + * Read through get_discount_rules() rather than the raw config property.
464 + * That accessor is what the intents use, so the type reported here is the
465 + * one the discount was actually calculated with: it decodes a rules payload
466 + * stored as JSON and fills in the defaults, neither of which the magic
467 + * property does. The property is also undeclared, so PHPStan cannot see it.
468 + *
469 + * @param \Disco\App\Utility\Config $campaign Campaign config.
470 + * @return string Discount type, or an empty string when it cannot be determined.
471 + */
472 + private function campaign_discount_type( Config $campaign ): string {
473 + $rules = $campaign->get_discount_rules();
474 +
475 + if ( ! is_array( $rules ) || ! isset( $rules[0] ) || ! is_object( $rules[0] ) ) {
476 + return '';
477 + }
478 +
479 + $discount_type = $rules[0]->discount_type ?? '';
480 +
481 + if ( ! is_scalar( $discount_type ) ) {
482 + return '';
483 + }
484 +
485 + return (string) $discount_type;
445 486 }
446 487
447 488 }