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