PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.21
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.21
1.4.21 1.4.20 1.4.19 1.4.18 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 All 184 releases
← All changes | app/Disco.php +86 -20 1.4.13 → 1.4.21 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,37 @@
90 111 if ( empty( $discounts ) ) {
91 112 return $price;
92 113 }
93 114
94 - $discount_type = $intent->campaign->discount_rules[0]['discount_type'];
115 + /**
116 + * Only amounts the product can actually carry take part in the choice.
117 + *
118 + * A flat amount larger than the price cannot be applied, and it used
119 + * to win the min/max selection anyway and then be rejected by the
120 + * guard below, leaving the product at full price even though another
121 + * campaign offered a discount that would have fitted. With a 70%
122 + * campaign and a 50.00 campaign, everything priced under 50.00 was
123 + * discounted by nothing at all while a 50.00 product went free.
124 + *
125 + * Dropping the ones that do not fit first means `max` picks the
126 + * largest discount that can be given rather than the largest that was
127 + * offered, which is what asking for the maximum discount means.
128 + */
129 + $applicable = array_filter(
130 + $discounts,
131 + static function ( $amount ) use ( $price ) {
132 + return (float) $amount <= (float) $price;
133 + }
134 + );
95 135
136 + if ( empty( $applicable ) ) {
137 + return $price;
138 + }
139 +
96 140 // 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 );
141 + $discounted_amount = $this->min_max_average( array_values( $applicable ) );
142 + $this->applied_campaign_id = (int) array_search( $discounted_amount, $applicable, true );
99 143
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 144 if ( $discounted_amount <= $price ) {
107 145 // Get an applied campaign from DiscountLimit class.
108 146 ( new UserLimit )->disco_start_session_on_checkout( $this->applied_campaign_id );
109 147
@@ -257,21 +295,21 @@
257 295 * @param \WC_Cart $cart WooCommerce cart object to calculate discounts for.
258 296 * @return array|false|\WC_Cart Cart object with applied discounts or false if no discounts are applicable.
259 297 */
260 298 public function get_cart_items_discount_for_bogo( $cart ) {//phpcs:ignore
261 - if ( ! $this->cart_is_valid() ) {
262 - return $cart;
263 - }
299 + if ( ! $this->cart_is_valid() ) {
300 + return $cart;
301 + }
264 302
265 - if ( ! defined( 'DOING_AJAX' ) && is_admin() ) {
266 - return $cart;
267 - }
303 + if ( ! defined( 'DOING_AJAX' ) && is_admin() ) {
304 + return $cart;
305 + }
268 306
269 - // Init Cart - Based Intents except Product & Shipping Intent.
270 - $this->intents = $this->prepare_intents( array( 'BOGO' ) );
307 + // Init Cart - Based Intents except Product & Shipping Intent.
308 + $this->intents = $this->prepare_intents( array( 'BOGO' ) );
271 309
272 310 return $this->prepare_item_discounts_bogo_free( $this->intents, $cart );
273 - }
311 + }
274 312
275 313 /**
276 314 * Apply the discount to shipping.
277 315 *
@@ -441,7 +479,35 @@
441 479 return false;
442 480 }
443 481
444 482 return in_array( $page_name, $pages, true );
483 + }
484 +
485 + /**
486 + * Resolve a campaign's discount type.
487 + *
488 + * Read through get_discount_rules() rather than the raw config property.
489 + * That accessor is what the intents use, so the type reported here is the
490 + * one the discount was actually calculated with: it decodes a rules payload
491 + * stored as JSON and fills in the defaults, neither of which the magic
492 + * property does. The property is also undeclared, so PHPStan cannot see it.
493 + *
494 + * @param \Disco\App\Utility\Config $campaign Campaign config.
495 + * @return string Discount type, or an empty string when it cannot be determined.
496 + */
497 + private function campaign_discount_type( Config $campaign ): string {
498 + $rules = $campaign->get_discount_rules();
499 +
500 + if ( ! is_array( $rules ) || ! isset( $rules[0] ) || ! is_object( $rules[0] ) ) {
501 + return '';
502 + }
503 +
504 + $discount_type = $rules[0]->discount_type ?? '';
505 +
506 + if ( ! is_scalar( $discount_type ) ) {
507 + return '';
508 + }
509 +
510 + return (string) $discount_type;
445 511 }
446 512
447 513 }