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.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 All 179 releases
disco / app / Calc / CalcPercent.php

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

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