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

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

165 lines 4.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 $min = $this->rule['min'] ? (int) $this->rule['min'] : 0; // phpcs:ignore
82 $max = $this->rule['max'] ? (int) $this->rule['max'] : 0; // phpcs:ignore
83 $recursive = isset( $this->rule['recursive'] ) && 'yes' === $this->rule['recursive']; // phpcs:ignore
84 $item_id = $this->item['product_id'];
85 $rule_ids = array_column( $this->rule['get_ids'], 'id' );
86 $discount_qty = $this->rule['get_quantity'] ? (int) $this->rule['get_quantity'] : 0; // phpcs:ignore
87
88 // Default discount for quantities.
89 $discount_for_quantities = $min;
90
91 // Apply for the Bulk Discount when max is set.
92 if ( $max && $quantity >= $max ) {
93 $discount_for_quantities = $max;
94 }
95
96 // If quantity between min and max then apply discount.
97 if ( $quantity >= $min && $quantity <= $max ) {
98 $discount_for_quantities = $quantity;
99 }
100
101 // Apply for the Bundle & BOGO Discount when recursive is set.
102 $multiplier = 1;
103
104 /**
105 * If discount intent is BuyXGetY then set the discount for quantities to 1.
106 * This is to ensure that the discount for selected item for BOGO discount.
107 * Here no need to check for min and max quantity.
108 */
109 if (
110 $this->discount_intent === 'BuyXGetY'
111 && ! empty( $rule_ids )
112 && (
113 in_array( $item_id, $rule_ids, true )
114 || $this->is_in_category( $item_id, $rule_ids )
115 )
116 ) {
117 $discount_for_quantities = $discount_qty;
118 $multiplier = $discount_qty;
119 }
120
121 if ( $recursive ) {
122 if ( $quantity % $min === 0 || $quantity > $discount_for_quantities ) {
123 $multiplier = floor( $quantity / $discount_for_quantities );
124 $discount_for_quantities *= $multiplier;
125 }
126 }
127
128 // Limit discount to Min or Max quantity.
129 if ( $quantity >= $discount_for_quantities ) {
130 $discount_amount = $fixed_discount * $multiplier;
131 }
132
133 $this->discounted_quantities = $discount_for_quantities;
134
135 return apply_filters( 'disco_final_discounted_amount', $discount_amount, 'fixed' );
136 }
137
138 /**
139 * Calculate Price.
140 *
141 * @param float $discount Discount Amount.
142 * @return float Price.
143 */
144 public function calculate_price( $discount ): float {
145 $discounted_subtotal = $this->calculate_line_subtotal( $discount );
146
147 return $discounted_subtotal / $this->item['quantity'];
148 }
149
150 /**
151 * Calculate Line Subtotal.
152 *
153 * @param float $discount Discount Amount.
154 * @return float Line Subtotal.
155 */
156 public function calculate_line_subtotal( $discount ): float {
157 if ( isset( $this->item['line_subtotal'] ) ) {
158 return $this->item['line_subtotal'] - $discount;
159 }
160
161 return 0.0;
162 }
163
164 }
165