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
disco / app / Calc / CalcFixedPrice.php

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

210 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignoreFile
2
3 namespace Disco\App\Calc;
4
5 /**
6 * Class CalcFixedPrice
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 CalcFixedPrice extends CalcAbstract {
16
17 /**
18 * @var array
19 */
20 private $rule;
21
22 /**
23 * @var array
24 */
25 private $item;
26
27 /**
28 * @var object $cart Cart.
29 * @phpstan-ignore-next-line
30 */
31 private $cart;
32
33 private int $discounted_quantities; // phpcs:ignore
34
35 /**
36 * CalcFixedPrice constructor.
37 *
38 * @param array $rule Campaign Rule.
39 * @param array $item Cart Item.
40 * @param \WC_Cart $cart Cart.
41 */
42 public function __construct( $rule, $item, $cart ) {
43 // Constructor...
44 $this->rule = $rule;
45 $this->item = $item;
46 $this->cart = $cart;
47 }
48
49 /**
50 * Calculate Discount.
51 *
52 * @return array $discount The discount amount.
53 */
54 public function calculate(): array {
55 $discount = $this->calculate_discount();
56
57 return array(
58 'price' => $this->calculate_price( $discount ),
59 'discount' => $discount,
60 'line_subtotal' => $this->calculate_line_subtotal( $discount ),
61 'discounted_quantities' => $this->discounted_quantities,
62 );
63 }
64
65 /**
66 * Calculate Discount.
67 *
68 * @return float Discount Amount.
69 */
70 public function calculate_discount(): float { // phpcs:ignore
71 $discount_amount = 0;
72 $fixed_discount = $this->rule['discount_value'];
73 $price = $this->item['data']->get_price(); // phpcs:ignore
74 $quantity = (int) $this->item['quantity']; // phpcs:ignore
75
76 // "Count Quantity As" combined / variations: eligibility gates the flat price discount.
77 if ( isset( $this->item['disco_forced_qty'] ) ) {
78 $forced = max( 0, (int) $this->item['disco_forced_qty'] );
79 $this->discounted_quantities = $forced;
80 $amount = 0.0;
81
82 if ( $forced > 0 ) {
83 $amount = (float) $fixed_discount * $this->pooled_bundle_count( $forced );
84 }
85
86 return apply_filters( 'disco_final_discounted_amount', $amount, 'fixed_price' );
87 }
88 $min = $this->rule['min'] ? (int) $this->rule['min'] : 0; // phpcs:ignore
89 $max = $this->rule['max'] ? (int) $this->rule['max'] : 0; // phpcs:ignore
90 $recursive = isset( $this->rule['recursive'] ) && 'yes' === $this->rule['recursive']; // phpcs:ignore
91
92 // Default discount for quantities.
93 $discount_for_quantities = $min;
94
95 // Apply for the Bulk Discount when max is set.
96 if ( $max > 0 && ( $quantity >= $min && $quantity <= $max ) ) {
97 $discount_for_quantities = $quantity;
98 }
99
100 // Apply for the Bundle Discount when recursive is set.
101 if ( $recursive && $quantity % $min === 0 ) {
102 $discount_for_quantities = $quantity;
103 }
104
105 // Limit discount to Min or Max quantity.
106 if ( $quantity >= $discount_for_quantities ) {
107 $discount_amount = $fixed_discount;
108 }
109
110 $this->discounted_quantities = $discount_for_quantities;
111
112 return apply_filters( 'disco_final_discounted_amount', $discount_amount, 'fixed_price' );
113 }
114
115 /**
116 * Calculate Price.
117 *
118 * @param float $discount Discount Amount.
119 * @return float Price.
120 */
121 public function calculate_price( $discount ): float {
122 $discounted_subtotal = $this->calculate_line_subtotal( $discount );
123
124 return $discounted_subtotal / $this->item['quantity'];
125 }
126
127 /**
128 * Calculate Line Subtotal.
129 *
130 * @param float $discount Discount Amount.
131 * @return float $total_price Total Price.
132 */
133 public function calculate_line_subtotal( $discount ): float {
134 return $this->item['line_subtotal'] - $discount;
135 }
136
137 // public function calculate_discount(): float {
138 // return $this->rule['discount_value'];
139 // }
140 //
141 // public function calculate_price( $discount ): float {
142 // $quantity = $this->item['quantity'];
143 // $price = $this->item['data']->get_price();
144 // $discount_for_quantities = $this->rule['min'] ?? 0;
145 //
146 // if ( $quantity >= $discount_for_quantities ) {
147 // $discounted_quantity = min( $quantity, $discount_for_quantities );
148 // $remaining_quantity = $quantity - $discounted_quantity;
149 //
150 // $discounted_price = $discount;
151 // $remaining_price = $remaining_quantity * $price;
152 //
153 // $total_price = $discounted_price + $remaining_price;
154 // } else {
155 // $total_price = $this->item['line_subtotal'];
156 // }
157 //
158 // return $total_price;
159 // }
160 //
161 // public function calculate_line_subtotal( $discount ): float {
162 // $quantity = $this->item['quantity'];
163 // $price = $this->item['data']->get_price();
164 // $discount_for_quantities = $this->rule['min'] ?? 0;
165 //
166 // if ( $quantity >= $discount_for_quantities ) {
167 // $discounted_quantity = min( $quantity, $discount_for_quantities );
168 // $remaining_quantity = $quantity - $discounted_quantity;
169 //
170 // $discounted_price = $discount;
171 // $remaining_price = $remaining_quantity * $price;
172 //
173 // $total_price = $discounted_price + $remaining_price;
174 // } else {
175 // $total_price = $this->item['line_subtotal'];
176 // }
177 //
178 // return $total_price;
179 // }
180
181 /**
182 * How many times a flat amount is charged for a pooled group.
183 *
184 * The pooled path hands this class the whole qualifying quantity on a single
185 * line, so the bundle count comes from that quantity rather than from a per
186 * line multiplier. A non recursive rule is charged once however large the
187 * pool; a recursive one is charged once per whole bundle in it.
188 *
189 * @param int $pooled_quantity Qualifying quantity across the pooled lines.
190 */
191 private function pooled_bundle_count( int $pooled_quantity ): int {
192 if ( ! isset( $this->rule['recursive'] ) || 'yes' !== $this->rule['recursive'] ) {
193 return 1;
194 }
195
196 $minimum = 0;
197
198 if ( isset( $this->rule['min'] ) ) {
199 $minimum = absint( $this->rule['min'] );
200 }
201
202 if ( $minimum < 1 ) {
203 return 1;
204 }
205
206 return max( 1, (int) floor( $pooled_quantity / $minimum ) );
207 }
208
209 }
210