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

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