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 / Intents / CartIntent.php

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

217 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Disco
4 * @subpackage \App\Intents
5 */
6
7 namespace Disco\App\Intents;
8
9 use Disco\App\Calc\CalcFactory;
10 use Disco\App\DropDown\DropDown;
11
12 /**
13 * Class Cart
14 *
15 * @package Disco
16 * @subpackage \App\Intents
17 * @author Ohidul Islam <wahid0003@gmail.com>
18 * @link https://webappick.com
19 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
20 * @category Intention
21 */
22 class CartIntent extends Intent {
23
24 /**
25 * Intent constructor.
26 *
27 * @param \Disco\App\Utility\Config $campaign Campaign Config.
28 * @param array $settings Global Settings.
29 */
30 public function __construct( $campaign, $settings ) {
31 parent::__construct( $campaign, $settings );
32
33 $this->campaign = $campaign;
34 $this->settings = $settings;
35 }
36
37 /**
38 * Apply Cart intention.
39 * Cart Discount Configuration.
40 *
41 * @param array $items Discount Applicable Cart Items.
42 * @param \WC_Cart $cart Cart.
43 * @return float|int
44 * @throws \Exception If settings not found.
45 */
46 public function get_discounts( $items, $cart ) { // phpcs:ignore
47
48 $discount = 0;
49
50 if ( empty( $items ) ) {
51 return $discount;
52 }
53
54 $rules = $this->campaign->get_discount_rules();
55
56 if ( ! empty( $rules ) && isset( $rules[0] ) ) {
57 $rule = $rules[0];
58
59 if ( is_object( $rule ) ) {
60 $rule = (array) $rule;
61 }
62
63 $discount_value = ( $rule['discount_value'] ? abs( $rule['discount_value'] ) : 0 );
64 $discount_type = $rule['discount_type'];
65
66 $campaign_products = $this->campaign->get_product_ids();
67
68 // $cost = CalcFactory::get_cart_subtotal( $cart );
69 //
70 // if ( $campaign_products !== 'all' ) {
71 // $cost = $this->get_items_subtotal( $items );
72 // }
73
74 // $cost = $this->get_items_subtotal( $items );
75 $cost = $this->get_items_subtotal_based_on_settings( $items );
76
77 // Discount should be less than item price
78 if ( $discount_type === 'fixed_per_product' ) {
79 // TODO: Check there is no free products in the cart before counting the discount
80 $item_prices = array();
81
82 foreach ( $items as $item ) {
83 $price = CalcFactory::get_price( $item );
84 $qty = $item['quantity'];
85 $min_value = min( $discount_value, $price );
86 $item_prices[] = $min_value * $qty;
87 }
88
89 $discount = array_sum( $item_prices );
90 } else {
91 $discount = $this->calculate_discount( $cost, $discount_type, $discount_value );
92 }
93 }
94
95 // @phpstan-ignore-next-line
96 return apply_filters( 'disco_final_discounted_amount', $discount, $discount_type );
97 }
98
99
100 /**
101 * Get Cart Item Quantities.
102 *
103 * @param array $items Discount Applicable Cart Items.
104 * @return int Cart Item Quantities.
105 */
106 public function get_cart_item_quantities( $items ) {
107 $quantities = 0;
108
109 foreach ( $items as $item ) {
110 $quantities += $item['quantity'];
111 }
112
113 return $quantities;
114 }
115
116 /**
117 * Get Cart Items Subtotal.
118 *
119 * @param array $items Discount Applicable Cart Items.
120 * @return int Cart Item Quantities.
121 */
122 public function get_items_subtotal( $items ) {
123 $subtotal = 0;
124
125 foreach ( $items as $item ) {
126 $subtotal += $item['line_subtotal'];
127 }
128
129 return $subtotal;
130 }
131
132 /**
133 * Get items subtotal based on settings.
134 *
135 * @param array $items Discount Applicable Cart Items.
136 * @return float
137 */
138 public function get_items_subtotal_based_on_settings( $items ) {
139 $subtotal = 0;
140
141 foreach ( $items as $item ) {
142 $price = CalcFactory::get_price( $item );
143 $subtotal += $price * $item['quantity'];
144 }
145
146 return $subtotal;
147 }
148
149 /**
150 * Get offers for Cart intention.
151 *
152 * @param array $items Discount Applicable Cart Items.
153 * @param \WC_Cart $cart Cart.
154 * @return array|bool
155 */
156 public function get_offers( $items, $cart ) { //phpcs:ignore
157 if ( $cart->is_empty() ) {
158 return false;
159 }
160
161 $offers = array();
162 $rules = $this->campaign->get_discount_rules();
163
164 if ( ! empty( $rules ) && isset( $rules[0] ) ) {
165 $rule = $rules[0];
166 $discount_value = abs( $rule->discount_value );
167 $discount_type = $rule->discount_type;
168 $conditions = $this->campaign->get_conditions();
169 $conditions_string = '';
170 $double_space = ' ';
171
172 if ( is_array( $conditions ) && ! empty( $conditions ) ) {
173 $conditions_string .= '<b>IF</b>' . "\n";
174
175 foreach ( $conditions as $condition_key => $condition ) {
176 if ( $condition_key > 0 ) {
177 $conditions_string .= '<b>' . ucfirst( $condition['base_operator'] ) . "</b>\n";
178 }
179
180 if ( empty( $condition['base_filters'] ) ) {
181 continue;
182 }
183
184 foreach ( $condition['base_filters'] as $filter_key => $filter ) {
185 $filter = (object) $filter;
186
187 if ( $filter_key > 0 ) {
188 $conditions_string .= $double_space . ' <b>' . ucfirst( $filter->operator ) . "</b>\n";
189 }
190
191 $compare_with = ucwords( str_replace( '_', ' ', $filter->compare_with ) );
192 $condition = DropDown::conditions( null, $filter->condition );
193 $compare = $filter->compare;
194 $conditions_string .= "$double_space $compare_with $compare \n";
195 }
196 }
197
198 $conditions_string .= '<b>END IF</b>' . "\n";
199 }
200
201 if ( 'percent' === $discount_type ) {
202 $discount = $discount_value . '% off';
203 } else {
204 $discount = wc_price( $discount_value ) . ' off';
205 }
206
207 $offers[] = array(
208 'condition' => $conditions_string,
209 'discount' => '<b>' . $discount . '</b>',
210 );
211 }
212
213 return $offers;
214 }
215
216 }
217