PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.3.49
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.3.49
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 / CalcFixedPerProduct.php

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

178 lines 4.9 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 CalcPercentPerProduct
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 CalcFixedPerProduct extends CalcAbstract {
16
17 use \Disco\App\Intents\IntentHelper;
18
19 /**
20 * @var array
21 */
22 private $rule;
23
24 /**
25 * @var array
26 */
27 private $item;
28
29 /**
30 * @var object $cart Cart.
31 * @phpstan-ignore-next-line
32 */
33 private $cart;
34
35 /**
36 * @var string $discount_intent
37 */
38 private $discount_intent;
39
40 /**
41 * @var float
42 */
43 private $discounted_quantities;
44
45 /**
46 * CalcFixedPerProduct constructor.
47 *
48 * @param array $rule Campaign Rule.
49 * @param array $item Cart Item.
50 * @param object $cart Cart.
51 * @param object $campaign Campaign.
52 */
53 public function __construct( $rule, $item, $cart, $campaign ) {
54 // Constructor...
55 $this->rule = $rule;
56 $this->item = $item;
57 $this->cart = $cart;
58
59 // @phpstan-ignore-next-line
60 $this->discount_intent = $campaign->get_discount_intent(); // phpcs:ignore
61 }
62
63 /**
64 * Calculate Discount.
65 * This function calculates the discount amount based on the discount type, value and item quantity.
66 *
67 * @return array $discount
68 */
69 public function calculate(): array {
70 $discount = $this->calculate_discount();
71
72 return array(
73 'price' => $this->calculate_price( $discount ),
74 'discount' => $discount,
75 'line_subtotal' => $this->calculate_line_subtotal( $discount ),
76 'discounted_quantities' => $this->discounted_quantities,
77 );
78 }
79
80 /**
81 * Calculate Discount.
82 *
83 * @return float Discount Amount.
84 */
85 public function calculate_discount(): float { // phpcs:ignore
86 $discount_amount = 0;
87 $fixed_per_product = $this->rule['discount_value'];
88 $quantity = (int) $this->item['quantity'];
89 $min = $this->rule['min'] ? (int) $this->rule['min'] : 0; //phpcs:ignore
90 $max = ! empty( $this->rule['max'] ) ? (int) $this->rule['max'] : ( $this->discount_intent === 'Bulk' ? PHP_INT_MAX : 0 ); // phpcs:ignore
91 $recursive = ! empty( $this->rule['recursive'] ) && $this->rule['recursive'] === 'yes'; // phpcs:ignore
92 $item_id = $this->item['product_id'];
93 $rule_ids = array_column( $this->rule['get_ids'], 'id' );
94 $discount_qty = $this->rule['get_quantity'] ? (int) $this->rule['get_quantity'] : 0; // phpcs:ignore
95
96 // Default discount for quantities.
97 $discount_for_quantities = $min;
98
99 // Apply for the Bulk Discount when max is set.
100 if ( $max && $quantity >= $max ) {
101 $discount_for_quantities = $max;
102 }
103
104 // If quantity between min and max then apply discount.
105 if ( $quantity >= $min && $quantity <= $max ) {
106 $discount_for_quantities = $quantity;
107 }
108
109 /**
110 * If discount intent is BuyXGetY then set the discount for quantities to 1.
111 * This is to ensure that the discount for selected item for BOGO discount.
112 * Here no need to check for min and max quantity.
113 */
114 if (
115 $this->discount_intent === 'BuyXGetY'
116 && ! empty( $rule_ids )
117 && (
118 in_array( $item_id, $rule_ids, true )
119 || $this->is_in_category( $item_id, $rule_ids )
120 )
121 ) {
122 $discount_for_quantities = $discount_qty;
123 }
124
125 // Apply for the Bundle & BOGO Discount when recursive is set.
126 $multiplier = 1;
127
128 if ( $recursive ) {
129 if ( $quantity % $min === 0 || $quantity > $discount_for_quantities ) {
130 $multiplier = floor( $quantity / $discount_for_quantities );
131 $discount_for_quantities *= $multiplier;
132 }
133 }
134
135 // Limit discount to Min or Max quantity.
136 if ( $quantity >= $discount_for_quantities ) {
137 $discount_for_quantities = min( $quantity, $discount_for_quantities );
138 // Set the discounted quantity for BOGO.
139 if ( ! empty( $this->rule['get_quantity'] ) ) { // phpcs:ignore
140 $discount_for_quantities = (int) $this->rule['get_quantity'];
141 $discount_for_quantities *= $multiplier;
142 }
143 $discount_amount = $discount_for_quantities * $fixed_per_product;
144 }
145
146 $this->discounted_quantities = $discount_for_quantities;
147
148 return apply_filters( 'disco_final_discounted_amount', $discount_amount, 'fixed_per_product' );
149 }
150
151 /**
152 * Calculate Price.
153 *
154 * @param float $discount Discount Amount.
155 * @return float Price.
156 */
157 public function calculate_price( $discount ): float {
158 $discounted_subtotal = $this->calculate_line_subtotal( $discount );
159
160 return $discounted_subtotal / $this->item['quantity'];
161 }
162
163 /**
164 * Calculate Line Subtotal.
165 *
166 * @param float $discount Discount Amount.
167 * @return float Line Subtotal.
168 */
169 public function calculate_line_subtotal( $discount ): float {
170 if ( isset( $this->item['line_subtotal'] ) ) {
171 return $this->item['line_subtotal'] - $discount;
172 }
173
174 return 0.0;
175 }
176
177 }
178