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

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

177 lines 4.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
81 return $forced > 0 ? (float) $fixed_discount : 0;
82 }
83 $min = $this->rule['min'] ? (int) $this->rule['min'] : 0; // phpcs:ignore
84 $max = $this->rule['max'] ? (int) $this->rule['max'] : 0; // phpcs:ignore
85 $recursive = isset( $this->rule['recursive'] ) && 'yes' === $this->rule['recursive']; // phpcs:ignore
86
87 // Default discount for quantities.
88 $discount_for_quantities = $min;
89
90 // Apply for the Bulk Discount when max is set.
91 if ( $max > 0 && ( $quantity >= $min && $quantity <= $max ) ) {
92 $discount_for_quantities = $quantity;
93 }
94
95 // Apply for the Bundle Discount when recursive is set.
96 if ( $recursive && $quantity % $min === 0 ) {
97 $discount_for_quantities = $quantity;
98 }
99
100 // Limit discount to Min or Max quantity.
101 if ( $quantity >= $discount_for_quantities ) {
102 $discount_amount = $fixed_discount;
103 }
104
105 $this->discounted_quantities = $discount_for_quantities;
106
107 return $discount_amount;
108 }
109
110 /**
111 * Calculate Price.
112 *
113 * @param float $discount Discount Amount.
114 * @return float Price.
115 */
116 public function calculate_price( $discount ): float {
117 $discounted_subtotal = $this->calculate_line_subtotal( $discount );
118
119 return $discounted_subtotal / $this->item['quantity'];
120 }
121
122 /**
123 * Calculate Line Subtotal.
124 *
125 * @param float $discount Discount Amount.
126 * @return float $total_price Total Price.
127 */
128 public function calculate_line_subtotal( $discount ): float {
129 return $this->item['line_subtotal'] - $discount;
130 }
131
132 // public function calculate_discount(): float {
133 // return $this->rule['discount_value'];
134 // }
135 //
136 // public function calculate_price( $discount ): float {
137 // $quantity = $this->item['quantity'];
138 // $price = $this->item['data']->get_price();
139 // $discount_for_quantities = $this->rule['min'] ?? 0;
140 //
141 // if ( $quantity >= $discount_for_quantities ) {
142 // $discounted_quantity = min( $quantity, $discount_for_quantities );
143 // $remaining_quantity = $quantity - $discounted_quantity;
144 //
145 // $discounted_price = $discount;
146 // $remaining_price = $remaining_quantity * $price;
147 //
148 // $total_price = $discounted_price + $remaining_price;
149 // } else {
150 // $total_price = $this->item['line_subtotal'];
151 // }
152 //
153 // return $total_price;
154 // }
155 //
156 // public function calculate_line_subtotal( $discount ): float {
157 // $quantity = $this->item['quantity'];
158 // $price = $this->item['data']->get_price();
159 // $discount_for_quantities = $this->rule['min'] ?? 0;
160 //
161 // if ( $quantity >= $discount_for_quantities ) {
162 // $discounted_quantity = min( $quantity, $discount_for_quantities );
163 // $remaining_quantity = $quantity - $discounted_quantity;
164 //
165 // $discounted_price = $discount;
166 // $remaining_price = $remaining_quantity * $price;
167 //
168 // $total_price = $discounted_price + $remaining_price;
169 // } else {
170 // $total_price = $this->item['line_subtotal'];
171 // }
172 //
173 // return $total_price;
174 // }
175
176 }
177