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

404 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 * @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 'discount_valid_from' => '',
110 'discount_valid_to' => '',
111 'discount_method' => 'automated',
112 'discount_coupon' => '',
113 'total_discount_limit' => '',
114 'total_sales_limit' => '',
115 'output' => array(),
116 'ui' => array(
117 'product_sale_badge' => '',
118 'product_page' => '',
119 'shop_page' => '',
120 ),
121 );
122
123 $this->config = wp_parse_args( $discount_info, $defaults );
124
125 return $this->config;
126 }
127
128 /**
129 * GET Config
130 *
131 * @return array
132 */
133 public function get_config() {
134 return $this->config;
135 }
136
137 /**
138 * GET FILTERS
139 * Return false if no filter is a set
140 * Return array if filter is set and not empty.
141 *
142 * @return array
143 */
144 public function get_conditions() {
145 if ( empty( $this->config['conditions'] ) ) {
146 return array();
147 }
148
149 $filters = $this->config['conditions'];
150
151 if ( is_string( $this->config['conditions'] ) ) {
152 $filters = json_decode( $this->config['conditions'], false );
153 }
154
155 if ( is_array( $filters ) && ! empty( $filters ) ) {
156 return $filters;
157 }
158
159 return array();
160 }
161
162 /**
163 * Get Discount Intent
164 *
165 * @return string
166 */
167 public function get_discount_intent() {
168 return $this->config['discount_intent'];
169 }
170
171 /**
172 * Get Bogo Type
173 *
174 * @return string
175 */
176 public function get_bogo_type() {
177 return $this->config['bogo_type'];
178 }
179
180 /**
181 * Get Discount Rules
182 * Return false if no discount rule is not set
183 *
184 * @return bool|array Return array if discount rule is set and not empty. Structure of the array as below:
185 */
186 public function get_discount_rules() {
187 $default_rule = array(
188 'id' => '',
189 'min' => '',
190 'max' => '',
191 'get_quantity' => '',
192 // Get quantity.
193 'get_ids' => array(),
194 // Get discount.
195 'discount_type' => 'percent',
196 'discount_value' => 0,
197 'discount_label' => 'Discount',
198 'recursive' => 'no',
199 );
200
201 $discount_rules = $this->config['discount_rules'];
202
203 if ( is_string( $this->config['discount_rules'] ) ) {
204 $discount_rules = json_decode( $this->config['discount_rules'], false );
205 }
206
207 if ( is_array( $discount_rules ) && ! empty( $discount_rules ) ) {
208 foreach ( $discount_rules as $key => $rule ) {
209 $discount_rules[ $key ] = (object) wp_parse_args( (array) $rule, $default_rule );
210 }
211
212 return $discount_rules;
213 }
214
215 return false;
216 }
217
218 /**
219 * Set Discount Rules
220 *
221 * @param array $rules Discount rules.
222 * Structure of the array as below:
223 * array(
224 * array(
225 * 'id' => '',
226 * 'min' => '',
227 * 'max' => '',
228 * 'get_quantity' => '',
229 * 'get_ids' => array(),
230 * 'discount_type' => 'percent',
231 * 'discount_value' => 0,
232 * 'discount_label' => 'Discount',
233 * 'recursive' => 'no',
234 * )
235 * );.
236 * @return void
237 */
238 public function set_discount_rules( $rules ) {
239 $this->config['discount_rules'] = $rules;
240 }
241
242 /**
243 * @return mixed|string
244 */
245 public function get_discount_based_on() {
246 if ( empty( $this->config['discount_based_on'] ) ) {
247 return 'cart_subtotal';
248 }
249
250 return $this->config['discount_based_on'];
251 }
252
253 /**
254 * Get Discount Type
255 *
256 * @return float
257 */
258 public function get_discount_value() {
259 if ( empty( $this->config['discount_value'] ) || ! is_numeric( $this->config['discount_value'] ) ) {
260 return 0;
261 }
262
263 return (float) $this->config['discount_value'];
264 }
265
266 /**
267 * Get Discount Label
268 *
269 * @return string
270 * @throws \Exception If discount label is not set.
271 */
272 public function get_discount_label() {
273 if ( empty( $this->config['discount_label'] ) ) {
274 return esc_html__( 'Discount', 'disco' );
275 }
276
277 return $this->config['discount_label'];
278 }
279
280 /**
281 * Get campaign user id.
282 *
283 * @return array|string
284 */
285 public function get_product_ids() {
286 $ids = array();
287
288 if ( ! isset( $this->config['products'] ) || empty( $this->config['products'] ) ) {
289 return 'all';
290 }
291
292 if ( 'all' === $this->config['products'][0] ) {
293 return 'all';
294 }
295
296 if ( ! empty( $this->config['products'] ) ) {
297 $products = $this->config['products'];
298
299 // if(!is_array($this->config['products'][0])) {
300 // $products = json_decode( $this->config['products'], true );
301 // $products = (array) $products;
302 // }
303
304 foreach ( $products as $product ) {
305 $product = (array) $product;
306
307 if ( isset( $product['id'] ) ) {
308 $ids[] = $product['id'];
309 } else {
310 $ids[] = $product;
311 }
312 }
313 }
314
315 return $ids;
316 }
317
318 /**
319 * Get rule product ids.
320 *
321 * @return array products ids.
322 */
323 public function get_rule_product_ids() {
324 if ( empty( $this->config['discount_rules'] ) ) {
325 return array();
326 }
327
328 $ids = array();
329
330 foreach ( $this->config['discount_rules'] as $rule ) {
331 if ( empty( $rule['get_ids'] ) ) {
332 continue;
333 }
334
335 $ids = array_merge( $ids, $rule['get_ids'] );
336 }
337
338 return array_column( $ids, 'id' );
339 }
340
341 /**
342 * Get Design Block
343 *
344 * @return mixed
345 */
346 public function get_design_block() {
347 return $this->config['design_blocks'];
348 }
349
350 /**
351 * Get Discount Rules
352 *
353 * @return array
354 */
355 public function get_discounts() {
356 return (array) $this->config['discount_rules'];
357 }
358
359 /**
360 * Magic methods
361 *
362 * @param string $name Configurations name.
363 * @return bool
364 */
365 public function __isset( $name ) {
366 return isset( $this->config[ $name ] );
367 }
368
369 /**
370 * Magic methods
371 *
372 * @param string $name Configurations name.
373 * @return mixed
374 */
375 public function __get( $name ) {
376 return $this->config[ $name ];
377 }
378
379 /**
380 * Magic methods
381 * Set config
382 *
383 * @param string $name Configurations name.
384 * @param string $value Configurations value.
385 * @return void
386 */
387 public function __set( $name, $value ) {
388 $this->config[ $name ] = $value;
389 }
390
391 /**
392 * Magic methods
393 * Unset config
394 *
395 * @param string $name Configurations name.
396 * @return void
397 * @since 1.0.0
398 */
399 public function __unset( $name ) {
400 unset( $this->config[ $name ] );
401 }
402
403 }
404