PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.7
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.7
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 / Utility / Config.php

Config.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.7, at app/Utility/Config.php

434 lines 10.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Campaign Config Utility
4 *
5 * @package Disco
6 * @subpackage App\Utility
7 * @since 1.0.0
8 * @category Utility
9 */
10
11 namespace Disco\App\Utility;
12
13 /**
14 * Class Config
15 *
16 * @package Disco
17 * @subpackage App\Utility
18 * @author Ohidul Islam <wahid0003@gmail.com>
19 * @link https://webappick.com
20 * @property false|mixed|string $filter
21 * @property string $discount_valid_from
22 * @property string $discount_valid_to
23 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
24 * @category Utility
25 */
26 class Config {
27
28 /**
29 * Config
30 *
31 * @var array
32 */
33 private $config;
34
35 /**
36 * Config constructor.
37 *
38 * @param array|mixed $discount_info Discount info.
39 */
40 public function __construct( $discount_info = array() ) {
41 $this->set_config( (array) $discount_info );
42 }
43
44 /**
45 * Check if the product id is applicable for this campaign.
46 *
47 * @param int $id Product id.
48 * @return bool
49 */
50 public function product_is_applicable( $id ) { // phpcs:ignore
51 if ( empty( $this->config['products'] ) ) {
52 return false;
53 }
54
55 if ( $this->config['products'][0] === 'all' ) {
56 return true;
57 }
58
59 if ( ! empty( $this->config['products'] ) ) {
60 $parent_id = null;
61 $variation = wc_get_product( $id );
62
63 if ( $variation && $variation->is_type( 'variation' ) ) {
64 $parent_id = $variation->get_parent_id();
65 }
66
67 foreach ( $this->config['products'] as $product ) {
68 if ( is_object( $product ) ) {
69 $product = (array) $product;
70 }
71
72 if ( ! isset( $product['id'] ) ) {
73 continue;
74 }
75
76 if ( (int) $product['id'] === (int) $id || ( $parent_id && (int) $product['id'] === (int) $parent_id ) ) {
77 return true;
78 }
79 }
80 }
81
82 return false;
83 }
84
85 /**
86 * Config
87 *
88 * @param array $discount_info Discount info.
89 * @return array
90 */
91 public function set_config( $discount_info ) {
92 $defaults = array(
93 'id' => '',
94 'name' => '',
95 'discount_intent' => '', /* Product, Cart, Shipping, Bulk, Bundle, BuyXGetX, BuyXGetY */
96 'priority' => '',
97 'status' => '',
98 'created_by' => '',
99 'created_date' => '',
100 'modified_by' => '',
101 'modified_date' => '',
102 'products' => array( 'all' ),
103 'conditions' => array(),
104 'discount_rules' => array(),
105 'discount_based_on' => '',
106 'discount_label' => '',
107 'discount_max_user' => '',
108 'bogo_type' => '',
109 'count_quantity_as' => 'separate',
110 'free_item_selection' => 'cart_order',
111 'discount_valid_from' => '',
112 'discount_valid_to' => '',
113 'discount_method' => 'automated',
114 'discount_coupon' => '',
115 'total_discount_limit' => '',
116 'total_sales_limit' => '',
117 'output' => array(),
118 'ui' => array(
119 'product_sale_badge' => '',
120 'product_page' => '',
121 'shop_page' => '',
122 ),
123 );
124
125 $this->config = wp_parse_args( $discount_info, $defaults );
126
127 return $this->config;
128 }
129
130 /**
131 * GET Config
132 *
133 * @return array
134 */
135 public function get_config() {
136 return $this->config;
137 }
138
139 /**
140 * GET FILTERS
141 * Return false if no filter is a set
142 * Return array if filter is set and not empty.
143 *
144 * @return array
145 */
146 public function get_conditions() {
147 if ( empty( $this->config['conditions'] ) ) {
148 return array();
149 }
150
151 $filters = $this->config['conditions'];
152
153 if ( is_string( $this->config['conditions'] ) ) {
154 $filters = json_decode( $this->config['conditions'], false );
155 }
156
157 if ( is_array( $filters ) && ! empty( $filters ) ) {
158 return $filters;
159 }
160
161 return array();
162 }
163
164 /**
165 * Get Discount Intent
166 *
167 * @return string
168 */
169 public function get_discount_intent() {
170 return $this->config['discount_intent'];
171 }
172
173 /**
174 * Get Bogo Type
175 *
176 * @return string
177 */
178 public function get_bogo_type() {
179 return $this->config['bogo_type'];
180 }
181
182 /**
183 * Get Count Quantity As.
184 * How item quantities aggregate toward the tier min/max.
185 *
186 * @return string One of: separate, variations, filters.
187 */
188 public function get_count_quantity_as(): string {
189 if ( empty( $this->config['count_quantity_as'] ) ) {
190 return 'separate';
191 }
192
193 return $this->config['count_quantity_as'];
194 }
195
196 /**
197 * Get Free Item Selection (BOGO).
198 * Which qualifying item receives the free / discounted reward.
199 *
200 * @return string One of: cart_order, lowest, highest.
201 */
202 public function get_free_item_selection(): string {
203 if ( empty( $this->config['free_item_selection'] ) ) {
204 return 'cart_order';
205 }
206
207 return $this->config['free_item_selection'];
208 }
209
210 /**
211 * Get Discount Rules
212 * Return false if no discount rule is not set
213 *
214 * @return bool|array Return array if discount rule is set and not empty. Structure of the array as below:
215 */
216 public function get_discount_rules() {
217 $default_rule = array(
218 'id' => '',
219 'min' => '',
220 'max' => '',
221 'get_quantity' => '',
222 // Get quantity.
223 'get_ids' => array(),
224 // Get discount.
225 'discount_type' => 'percent',
226 'discount_value' => 0,
227 'discount_label' => 'Discount',
228 'recursive' => 'no',
229 );
230
231 $discount_rules = $this->config['discount_rules'];
232
233 if ( is_string( $this->config['discount_rules'] ) ) {
234 $discount_rules = json_decode( $this->config['discount_rules'], false );
235 }
236
237 if ( is_array( $discount_rules ) && ! empty( $discount_rules ) ) {
238 foreach ( $discount_rules as $key => $rule ) {
239 $discount_rules[ $key ] = (object) wp_parse_args( (array) $rule, $default_rule );
240 }
241
242 return $discount_rules;
243 }
244
245 return false;
246 }
247
248 /**
249 * Set Discount Rules
250 *
251 * @param array $rules Discount rules.
252 * Structure of the array as below:
253 * array(
254 * array(
255 * 'id' => '',
256 * 'min' => '',
257 * 'max' => '',
258 * 'get_quantity' => '',
259 * 'get_ids' => array(),
260 * 'discount_type' => 'percent',
261 * 'discount_value' => 0,
262 * 'discount_label' => 'Discount',
263 * 'recursive' => 'no',
264 * )
265 * );.
266 * @return void
267 */
268 public function set_discount_rules( $rules ) {
269 $this->config['discount_rules'] = $rules;
270 }
271
272 /**
273 * @return mixed|string
274 */
275 public function get_discount_based_on() {
276 if ( empty( $this->config['discount_based_on'] ) ) {
277 return 'cart_subtotal';
278 }
279
280 return $this->config['discount_based_on'];
281 }
282
283 /**
284 * Get Discount Type
285 *
286 * @return float
287 */
288 public function get_discount_value() {
289 if ( empty( $this->config['discount_value'] ) || ! is_numeric( $this->config['discount_value'] ) ) {
290 return 0;
291 }
292
293 return (float) $this->config['discount_value'];
294 }
295
296 /**
297 * Get Discount Label
298 *
299 * @return string
300 * @throws \Exception If discount label is not set.
301 */
302 public function get_discount_label() {
303 if ( empty( $this->config['discount_label'] ) ) {
304 return esc_html__( 'Discount', 'disco' );
305 }
306
307 return $this->config['discount_label'];
308 }
309
310 /**
311 * Get campaign user id.
312 *
313 * @return array|string
314 */
315 public function get_product_ids() {
316 $ids = array();
317
318 if ( ! isset( $this->config['products'] ) || empty( $this->config['products'] ) ) {
319 return 'all';
320 }
321
322 if ( 'all' === $this->config['products'][0] ) {
323 return 'all';
324 }
325
326 if ( ! empty( $this->config['products'] ) ) {
327 $products = $this->config['products'];
328
329 // if(!is_array($this->config['products'][0])) {
330 // $products = json_decode( $this->config['products'], true );
331 // $products = (array) $products;
332 // }
333
334 foreach ( $products as $product ) {
335 $product = (array) $product;
336
337 if ( isset( $product['id'] ) ) {
338 $ids[] = $product['id'];
339 } else {
340 $ids[] = $product;
341 }
342 }
343 }
344
345 return $ids;
346 }
347
348 /**
349 * Get rule product ids.
350 *
351 * @return array products ids.
352 */
353 public function get_rule_product_ids() {
354 if ( empty( $this->config['discount_rules'] ) ) {
355 return array();
356 }
357
358 $ids = array();
359
360 foreach ( $this->config['discount_rules'] as $rule ) {
361 if ( empty( $rule['get_ids'] ) ) {
362 continue;
363 }
364
365 $ids = array_merge( $ids, $rule['get_ids'] );
366 }
367
368 return array_column( $ids, 'id' );
369 }
370
371 /**
372 * Get Design Block
373 *
374 * @return mixed
375 */
376 public function get_design_block() {
377 return $this->config['design_blocks'];
378 }
379
380 /**
381 * Get Discount Rules
382 *
383 * @return array
384 */
385 public function get_discounts() {
386 return (array) $this->config['discount_rules'];
387 }
388
389 /**
390 * Magic methods
391 *
392 * @param string $name Configurations name.
393 * @return bool
394 */
395 public function __isset( $name ) {
396 return isset( $this->config[ $name ] );
397 }
398
399 /**
400 * Magic methods
401 *
402 * @param string $name Configurations name.
403 * @return mixed
404 */
405 public function __get( $name ) {
406 return $this->config[ $name ];
407 }
408
409 /**
410 * Magic methods
411 * Set config
412 *
413 * @param string $name Configurations name.
414 * @param string $value Configurations value.
415 * @return void
416 */
417 public function __set( $name, $value ) {
418 $this->config[ $name ] = $value;
419 }
420
421 /**
422 * Magic methods
423 * Unset config
424 *
425 * @param string $name Configurations name.
426 * @return void
427 * @since 1.0.0
428 */
429 public function __unset( $name ) {
430 unset( $this->config[ $name ] );
431 }
432
433 }
434