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 / Utility / Config.php

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

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