PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.2.18
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.2.18
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 1.3.47 1.3.46 All 178 releases
disco / app / Calc / CalcFixedPrice.php

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

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