PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.14
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.14
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 1.3.47 1.3.46 1.3.45 All 177 releases
disco / app / Calc / CalcFixed.php

CalcFixed.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.14, at app/Calc/CalcFixed.php

187 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Disco\App\Calc;
4
5 /**
6 * Class CalcPercent
7 *
8 * @package Disco
9 * @subpackage Disco\App\Calc
10 * @author Ohidul Islam <wahid0003@gmail.com>
11 * @link https://webappick.com
12 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
13 * @category Plugin
14 */
15 class CalcFixed extends CalcAbstract {
16
17 use \Disco\App\Intents\IntentHelper;
18
19 /**
20 * @var object $cart Cart.
21 * @phpstan-ignore-next-line
22 */
23 private $cart;
24
25 /**
26 * @var float
27 */
28 private $discounted_quantities;
29
30 /**
31 * @var string $discount_intent
32 */
33 private $discount_intent;
34
35 /**
36 * @var array $rule Campaign Rule.
37 */
38 private $rule;
39
40 /**
41 * @var array $item Cart Item.
42 */
43 private $item;
44
45 /**
46 * CalcFixed constructor.
47 *
48 * @param array $rule Campaign Rule.
49 * @param array $item Cart Item.
50 * @param \WC_Cart $cart Cart.
51 * @param object $campaign Campaign.
52 */
53 public function __construct( $rule, $item, $cart, $campaign ) {
54 $this->item = $item;
55 $this->rule = $rule;
56 $this->cart = $cart;
57 // @phpstan-ignore-next-line
58 $this->discount_intent = $campaign->get_discount_intent();
59 }
60
61 public function calculate(): array {
62 $discount = $this->calculate_discount();
63
64 return array(
65 'price' => $this->calculate_price( $discount ),
66 'discount' => $discount,
67 'line_subtotal' => $this->calculate_line_subtotal( $discount ),
68 'discounted_quantities' => $this->discounted_quantities,
69 );
70 }
71
72 /**
73 * Calculate Discount.
74 *
75 * @return float Discount Amount.
76 */
77 public function calculate_discount(): float { // phpcs:ignore
78 $discount_amount = 0;
79 $fixed_discount = $this->rule['discount_value'];
80 $quantity = (int) $this->item['quantity'];
81
82 /**
83 * "Count Quantity As" combined / variations: a fixed amount is a flat
84 * line discount, so eligibility (any eligible unit) gates it once rather
85 * than scaling with the eligible unit count.
86 */
87 if ( isset( $this->item['disco_forced_qty'] ) ) {
88 $forced = max( 0, (int) $this->item['disco_forced_qty'] );
89 $this->discounted_quantities = $forced;
90
91 return apply_filters( 'disco_final_discounted_amount', $forced > 0 ? (float) $fixed_discount : 0, 'fixed' );
92 }
93
94 $min = $this->rule['min'] ? (int) $this->rule['min'] : 0; // phpcs:ignore
95 $max = $this->rule['max'] ? (int) $this->rule['max'] : 0; // phpcs:ignore
96 $recursive = isset( $this->rule['recursive'] ) && 'yes' === $this->rule['recursive']; // phpcs:ignore
97 $item_id = $this->item['product_id'];
98 $rule_ids = array_column( $this->rule['get_ids'], 'id' );
99 $discount_qty = $this->rule['get_quantity'] ? (int) $this->rule['get_quantity'] : 0; // phpcs:ignore
100
101 // Default discount for quantities.
102 $discount_for_quantities = $min;
103
104 /**
105 * Bulk / range capping — only when NOT recursive. Recursive rules ignore
106 * max: the bundle size is always min, so the qualifying quantity must not
107 * be capped to max here, otherwise floor( qty / base ) would divide by
108 * max instead of min and undercount the bundles.
109 */
110 if ( ! $recursive && $max && $quantity >= $max ) {
111 $discount_for_quantities = $max;
112 }
113
114 // If quantity between min and max then apply discount.
115 if ( ! $recursive && $quantity >= $min && $quantity <= $max ) {
116 $discount_for_quantities = $quantity;
117 }
118
119 // Apply for the Bundle & BOGO Discount when recursive is set.
120 $multiplier = 1;
121
122 /**
123 * If discount intent is BuyXGetY then set the discount for quantities to 1.
124 * This is to ensure that the discount for selected item for BOGO discount.
125 * Here no need to check for min and max quantity.
126 */
127 if (
128 $this->discount_intent === 'BuyXGetY'
129 && ! empty( $rule_ids )
130 && (
131 in_array( $item_id, $rule_ids, true )
132 || $this->is_in_category( $item_id, $rule_ids )
133 )
134 ) {
135 $discount_for_quantities = $discount_qty;
136 $multiplier = $discount_qty;
137 }
138
139 if ( $recursive && $min > 0 && $quantity >= $min ) {
140 /**
141 * Recursive: the number of bundles is floor( qty / min ). Divide by
142 * min, never by $discount_for_quantities — for BuyXGetY the latter
143 * holds the reward quantity (get_quantity) and would inflate the
144 * multiplier (e.g. buy 2 get 1 with 2 in cart → 1 bundle, not 2).
145 */
146 $multiplier = (int) floor( $quantity / $min );
147 $discount_for_quantities *= $multiplier;
148 }
149
150 // Limit discount to Min or Max quantity.
151 if ( $quantity >= $discount_for_quantities ) {
152 $discount_amount = $fixed_discount * $multiplier;
153 }
154
155 $this->discounted_quantities = $discount_for_quantities;
156
157 return apply_filters( 'disco_final_discounted_amount', $discount_amount, 'fixed' );
158 }
159
160 /**
161 * Calculate Price.
162 *
163 * @param float $discount Discount Amount.
164 * @return float Price.
165 */
166 public function calculate_price( $discount ): float {
167 $discounted_subtotal = $this->calculate_line_subtotal( $discount );
168
169 return $discounted_subtotal / $this->item['quantity'];
170 }
171
172 /**
173 * Calculate Line Subtotal.
174 *
175 * @param float $discount Discount Amount.
176 * @return float Line Subtotal.
177 */
178 public function calculate_line_subtotal( $discount ): float {
179 if ( isset( $this->item['line_subtotal'] ) ) {
180 return $this->item['line_subtotal'] - $discount;
181 }
182
183 return 0.0;
184 }
185
186 }
187