Discount Rules for WooCommerce - Version 1.3

Version Description

Download this release

Release Info

Developer flycart
Plugin Icon 128x128 Discount Rules for WooCommerce
Version 1.3
Comparing to
See all releases

Code changes from version 1.2 to 1.3

helper/general-helper.php CHANGED
@@ -59,7 +59,7 @@ if ( ! class_exists( 'woo_dicount_rules_generalHelper' ) ) {
59
  */
60
  public function getUserList()
61
  {
62
- $result = [];
63
  foreach (get_users() as $user) {
64
  $result[$user->ID] = '#' . $user->ID . ' ' . $user->user_email;
65
  }
@@ -89,7 +89,7 @@ if ( ! class_exists( 'woo_dicount_rules_generalHelper' ) ) {
89
  */
90
  public function getProductList()
91
  {
92
- $products = [];
93
  $posts_raw = get_posts(array(
94
  'posts_per_page' => -1,
95
  'post_type' => 'product',
@@ -114,9 +114,9 @@ if ( ! class_exists( 'woo_dicount_rules_generalHelper' ) ) {
114
  $countries = new WC_Countries();
115
 
116
  if ($countries && is_array($countries->countries)) {
117
- return array_merge([], $countries->countries);
118
  } else {
119
- return [];
120
  }
121
  }
122
 
@@ -162,7 +162,7 @@ if ( ! class_exists( 'woo_dicount_rules_generalHelper' ) ) {
162
  }
163
  }
164
 
165
- return array_merge([], $capabilities);
166
  }
167
 
168
  /**
@@ -176,7 +176,7 @@ if ( ! class_exists( 'woo_dicount_rules_generalHelper' ) ) {
176
  $wp_roles = new WP_Roles();
177
  }
178
 
179
- return array_merge([], $wp_roles->get_names());
180
  }
181
 
182
  /**
@@ -196,7 +196,7 @@ if ( ! class_exists( 'woo_dicount_rules_generalHelper' ) ) {
196
  */
197
  public function getCategoryList()
198
  {
199
- $result = [];
200
 
201
  $post_categories_raw = get_terms(array('product_cat'), array('hide_empty' => 0));
202
  $post_categories_raw_count = count($post_categories_raw);
@@ -329,7 +329,7 @@ if ( ! class_exists( 'woo_dicount_rules_generalHelper' ) ) {
329
  */
330
  public static function reArrangeArray(&$rules)
331
  {
332
- $result = [];
333
  foreach ($rules as $index => $item) {
334
  foreach ($item as $id => $value) {
335
  $result[$id] = $value;
@@ -349,7 +349,7 @@ if ( ! class_exists( 'woo_dicount_rules_generalHelper' ) ) {
349
  $value = self::delete_all_between('<?', '?>', $value);
350
  $value = self::delete_all_between('<?=', '?>', $value);
351
  $value = self::delete_all_between('<%', '%>', $value);
352
- $value = str_replace(['<?php', '<?', '<?=', '<%', '?>'], '', $value);
353
  }
354
 
355
 
@@ -394,4 +394,4 @@ if ( ! class_exists( 'woo_dicount_rules_generalHelper' ) ) {
394
  return $html;
395
  }
396
  }
397
- }
59
  */
60
  public function getUserList()
61
  {
62
+ $result = array();
63
  foreach (get_users() as $user) {
64
  $result[$user->ID] = '#' . $user->ID . ' ' . $user->user_email;
65
  }
89
  */
90
  public function getProductList()
91
  {
92
+ $products = array();
93
  $posts_raw = get_posts(array(
94
  'posts_per_page' => -1,
95
  'post_type' => 'product',
114
  $countries = new WC_Countries();
115
 
116
  if ($countries && is_array($countries->countries)) {
117
+ return array_merge(array(), $countries->countries);
118
  } else {
119
+ return array();
120
  }
121
  }
122
 
162
  }
163
  }
164
 
165
+ return array_merge(array(), $capabilities);
166
  }
167
 
168
  /**
176
  $wp_roles = new WP_Roles();
177
  }
178
 
179
+ return array_merge(array(), $wp_roles->get_names());
180
  }
181
 
182
  /**
196
  */
197
  public function getCategoryList()
198
  {
199
+ $result = array();
200
 
201
  $post_categories_raw = get_terms(array('product_cat'), array('hide_empty' => 0));
202
  $post_categories_raw_count = count($post_categories_raw);
329
  */
330
  public static function reArrangeArray(&$rules)
331
  {
332
+ $result = array();
333
  foreach ($rules as $index => $item) {
334
  foreach ($item as $id => $value) {
335
  $result[$id] = $value;
349
  $value = self::delete_all_between('<?', '?>', $value);
350
  $value = self::delete_all_between('<?=', '?>', $value);
351
  $value = self::delete_all_between('<%', '%>', $value);
352
+ $value = str_replace(array('<?php', '<?', '<?=', '<%', '?>'), '', $value);
353
  }
354
 
355
 
394
  return $html;
395
  }
396
  }
397
+ }
includes/cart-rules.php CHANGED
@@ -74,9 +74,9 @@ if ( ! class_exists( 'woo_dicount_rules_cartRules' ) ) {
74
  global $woocommerce;
75
 
76
  $this->postData = \FlycartInput\FInput::getInstance();
77
- $this->cart_items = (isset($woocommerce->cart->cart_contents) ? $woocommerce->cart->cart_contents : []);
78
  $this->calculateCartSubtotal();
79
- $this->coupon_list = (isset($woocommerce->cart->applied_coupons) ? $woocommerce->cart->applied_coupons : []);
80
 
81
  // Check for Remove Coupon Request.
82
  if (!is_null($this->postData->get('remove_coupon', null))) $this->removeWoocommerceCoupon($this->postData->get('remove_coupon'));
@@ -117,27 +117,27 @@ if ( ! class_exists( 'woo_dicount_rules_cartRules' ) ) {
117
  $slug = str_replace(' ', '-', $slug);
118
 
119
  if ($id) {
120
- $post = [
121
  'ID' => $id,
122
  'post_title' => $title,
123
  'post_name' => $slug,
124
  'post_content' => 'New Rule',
125
  'post_type' => $this->post_type,
126
  'post_status' => 'publish'
127
- ];
128
  wp_update_post($post);
129
  } else {
130
- $post = [
131
  'post_title' => $title,
132
  'post_name' => $slug,
133
  'post_content' => 'New Rule',
134
  'post_type' => $this->post_type,
135
  'post_status' => 'publish'
136
- ];
137
  $id = wp_insert_post($post);
138
  }
139
 
140
- $form = [
141
  'rule_name',
142
  'rule_descr',
143
  'date_from',
@@ -147,7 +147,7 @@ if ( ! class_exists( 'woo_dicount_rules_cartRules' ) ) {
147
  'to_discount',
148
  'discount_rule',
149
  'rule_order'
150
- ];
151
 
152
  foreach ($request['discount_rule'] as $index => $value) {
153
  $request['discount_rule'][$index] = woo_dicount_rules_generalHelper::makeString($value);
@@ -255,10 +255,10 @@ if ( ! class_exists( 'woo_dicount_rules_cartRules' ) ) {
255
  {
256
  if (is_null($this->coupon_code) || empty($this->coupon_code)) return false;
257
 
258
- $discount_log = [
259
  'coupon_name' => $this->coupon_code,
260
  'discount' => $this->discount_total,
261
- ];
262
  WC()->session->set('woo_cart_discount', json_encode($discount_log));
263
  }
264
 
@@ -441,7 +441,7 @@ if ( ! class_exists( 'woo_dicount_rules_cartRules' ) ) {
441
 
442
  if (is_null($rules) || !isset($rules)) return false;
443
  // Start with empty set.
444
- $rule_set = [];
445
 
446
  foreach ($rules as $index => $rule) {
447
  $status = (isset($rule->status) ? $rule->status : false);
@@ -478,7 +478,7 @@ if ( ! class_exists( 'woo_dicount_rules_cartRules' ) ) {
478
  {
479
  if (empty($this->rules)) return false;
480
 
481
- $ordered_rules = [];
482
 
483
  // Make associative array with Order ID.
484
  foreach ($this->rules as $index => $rule) {
@@ -500,7 +500,7 @@ if ( ! class_exists( 'woo_dicount_rules_cartRules' ) ) {
500
  public function generateRuleSets()
501
  {
502
  global $woocommerce;
503
- $rule_sets = [];
504
 
505
  if (!isset($this->rules)) return false;
506
 
@@ -530,7 +530,7 @@ if ( ! class_exists( 'woo_dicount_rules_cartRules' ) ) {
530
  public function getDiscountAmount()
531
  {
532
  $discount = 0;
533
- $discounts = [];
534
  if (!isset($this->rule_sets)) return false;
535
 
536
  // Processing the Totals.
@@ -565,7 +565,7 @@ if ( ! class_exists( 'woo_dicount_rules_cartRules' ) ) {
565
  public function validateCart($rules)
566
  {
567
  $this->calculateCartSubtotal();
568
- $rules = (is_string($rules) ? json_decode($rules, true) : []);
569
  // Simple array helper to re-arrange the structure.
570
  woo_dicount_rules_generalHelper::reArrangeArray($rules);
571
  foreach ($rules as $index => $rule) {
74
  global $woocommerce;
75
 
76
  $this->postData = \FlycartInput\FInput::getInstance();
77
+ $this->cart_items = (isset($woocommerce->cart->cart_contents) ? $woocommerce->cart->cart_contents : array());
78
  $this->calculateCartSubtotal();
79
+ $this->coupon_list = (isset($woocommerce->cart->applied_coupons) ? $woocommerce->cart->applied_coupons : array());
80
 
81
  // Check for Remove Coupon Request.
82
  if (!is_null($this->postData->get('remove_coupon', null))) $this->removeWoocommerceCoupon($this->postData->get('remove_coupon'));
117
  $slug = str_replace(' ', '-', $slug);
118
 
119
  if ($id) {
120
+ $post = array(
121
  'ID' => $id,
122
  'post_title' => $title,
123
  'post_name' => $slug,
124
  'post_content' => 'New Rule',
125
  'post_type' => $this->post_type,
126
  'post_status' => 'publish'
127
+ );
128
  wp_update_post($post);
129
  } else {
130
+ $post = array(
131
  'post_title' => $title,
132
  'post_name' => $slug,
133
  'post_content' => 'New Rule',
134
  'post_type' => $this->post_type,
135
  'post_status' => 'publish'
136
+ );
137
  $id = wp_insert_post($post);
138
  }
139
 
140
+ $form = array(
141
  'rule_name',
142
  'rule_descr',
143
  'date_from',
147
  'to_discount',
148
  'discount_rule',
149
  'rule_order'
150
+ );
151
 
152
  foreach ($request['discount_rule'] as $index => $value) {
153
  $request['discount_rule'][$index] = woo_dicount_rules_generalHelper::makeString($value);
255
  {
256
  if (is_null($this->coupon_code) || empty($this->coupon_code)) return false;
257
 
258
+ $discount_log = array(
259
  'coupon_name' => $this->coupon_code,
260
  'discount' => $this->discount_total,
261
+ );
262
  WC()->session->set('woo_cart_discount', json_encode($discount_log));
263
  }
264
 
441
 
442
  if (is_null($rules) || !isset($rules)) return false;
443
  // Start with empty set.
444
+ $rule_set = array();
445
 
446
  foreach ($rules as $index => $rule) {
447
  $status = (isset($rule->status) ? $rule->status : false);
478
  {
479
  if (empty($this->rules)) return false;
480
 
481
+ $ordered_rules = array();
482
 
483
  // Make associative array with Order ID.
484
  foreach ($this->rules as $index => $rule) {
500
  public function generateRuleSets()
501
  {
502
  global $woocommerce;
503
+ $rule_sets = array();
504
 
505
  if (!isset($this->rules)) return false;
506
 
530
  public function getDiscountAmount()
531
  {
532
  $discount = 0;
533
+ $discounts = array();
534
  if (!isset($this->rule_sets)) return false;
535
 
536
  // Processing the Totals.
565
  public function validateCart($rules)
566
  {
567
  $this->calculateCartSubtotal();
568
+ $rules = (is_string($rules) ? json_decode($rules, true) : array());
569
  // Simple array helper to re-arrange the structure.
570
  woo_dicount_rules_generalHelper::reArrangeArray($rules);
571
  foreach ($rules as $index => $rule) {
includes/discount-base.php CHANGED
@@ -25,7 +25,7 @@ class woo_dicount_rules_WooDiscountBase
25
  /**
26
  * @var array
27
  */
28
- private $instance = [];
29
 
30
  /**
31
  * woo_dicount_rules_WooDiscountBase constructor.
@@ -90,8 +90,8 @@ class woo_dicount_rules_WooDiscountBase
90
  */
91
  public function storeLog($order_id)
92
  {
93
- $log['price_discount'] = WC()->session->get('woo_price_discount', []);
94
- $log['cart_discount'] = WC()->session->get('woo_cart_discount', []);
95
 
96
  add_post_meta($order_id, 'woo_discount_log', json_encode($log));
97
 
@@ -217,9 +217,9 @@ class woo_dicount_rules_WooDiscountBase
217
  {
218
  $link = $this->fieldLink();
219
 
220
- $discount_list = [];
221
  // Here, Eliminating the Cart's rule with duplicates.
222
- $discount_rule = (isset($cart_rules['discount_rule']) ? $cart_rules['discount_rule'] : []);
223
  if ($isCartRules) {
224
  foreach ($discount_rule as $index => $value) {
225
 
@@ -245,7 +245,7 @@ class woo_dicount_rules_WooDiscountBase
245
  public function fieldLink()
246
  {
247
  // TODO: Check Subtotal Link
248
- return [
249
  'products_atleast_one' => 'product_to_apply',
250
  'products_not_in' => 'product_to_apply',
251
 
@@ -255,7 +255,7 @@ class woo_dicount_rules_WooDiscountBase
255
  'users_in' => 'users_to_apply',
256
  'roles_in' => 'user_roles_to_apply',
257
  'shipping_countries_in' => 'countries_to_apply'
258
- ];
259
  }
260
 
261
  // ----------------------------------------- CART RULES END --------------------------------------------------------
@@ -270,7 +270,7 @@ class woo_dicount_rules_WooDiscountBase
270
  {
271
  $postData = \FlycartInput\FInput::getInstance();
272
  $request = $postData->getArray();
273
- $params = [];
274
  if (isset($request['data'])) {
275
  parse_str($request['data'], $params);
276
  }
@@ -301,7 +301,7 @@ class woo_dicount_rules_WooDiscountBase
301
  {
302
  $option = get_option($this->default_option);
303
  if (!$option || is_null($option)) {
304
- return [];
305
  } else {
306
  return $option;
307
  }
@@ -380,11 +380,11 @@ class woo_dicount_rules_WooDiscountBase
380
  */
381
  public function formList()
382
  {
383
- return [
384
  'pricing_rules',
385
  'cart_rules',
386
  'settings'
387
- ];
388
  }
389
 
390
  /**
@@ -414,7 +414,7 @@ class woo_dicount_rules_WooDiscountBase
414
  $html = '';
415
  // File Check.
416
  if (file_exists($path)) {
417
- $data = [];
418
  $this->fetchData($tab, $data);
419
  // Processing View.
420
  $html = $generalHelper->processBaseView($path, $data);
@@ -526,7 +526,7 @@ class woo_dicount_rules_WooDiscountBase
526
  break;
527
 
528
  default:
529
- $data = [];
530
 
531
  break;
532
  }
@@ -538,7 +538,7 @@ class woo_dicount_rules_WooDiscountBase
538
  */
539
  public function adminPages()
540
  {
541
- return [
542
  $this->default_page => WOO_DISCOUNT_DIR . '/view/pricing-rules.php',
543
  'cart-rules' => WOO_DISCOUNT_DIR . '/view/cart-rules.php',
544
  'settings' => WOO_DISCOUNT_DIR . '/view/settings.php',
@@ -550,7 +550,7 @@ class woo_dicount_rules_WooDiscountBase
550
  // Edit Rules
551
  'pricing-rules-view' => WOO_DISCOUNT_DIR . '/view/view-pricing-rules.php',
552
  'cart-rules-view' => WOO_DISCOUNT_DIR . '/view/view-cart-rules.php'
553
- ];
554
  }
555
 
556
  /**
25
  /**
26
  * @var array
27
  */
28
+ private $instance = array();
29
 
30
  /**
31
  * woo_dicount_rules_WooDiscountBase constructor.
90
  */
91
  public function storeLog($order_id)
92
  {
93
+ $log['price_discount'] = WC()->session->get('woo_price_discount', array());
94
+ $log['cart_discount'] = WC()->session->get('woo_cart_discount', array());
95
 
96
  add_post_meta($order_id, 'woo_discount_log', json_encode($log));
97
 
217
  {
218
  $link = $this->fieldLink();
219
 
220
+ $discount_list = array();
221
  // Here, Eliminating the Cart's rule with duplicates.
222
+ $discount_rule = (isset($cart_rules['discount_rule']) ? $cart_rules['discount_rule'] : array());
223
  if ($isCartRules) {
224
  foreach ($discount_rule as $index => $value) {
225
 
245
  public function fieldLink()
246
  {
247
  // TODO: Check Subtotal Link
248
+ return array(
249
  'products_atleast_one' => 'product_to_apply',
250
  'products_not_in' => 'product_to_apply',
251
 
255
  'users_in' => 'users_to_apply',
256
  'roles_in' => 'user_roles_to_apply',
257
  'shipping_countries_in' => 'countries_to_apply'
258
+ );
259
  }
260
 
261
  // ----------------------------------------- CART RULES END --------------------------------------------------------
270
  {
271
  $postData = \FlycartInput\FInput::getInstance();
272
  $request = $postData->getArray();
273
+ $params = array();
274
  if (isset($request['data'])) {
275
  parse_str($request['data'], $params);
276
  }
301
  {
302
  $option = get_option($this->default_option);
303
  if (!$option || is_null($option)) {
304
+ return array();
305
  } else {
306
  return $option;
307
  }
380
  */
381
  public function formList()
382
  {
383
+ return array(
384
  'pricing_rules',
385
  'cart_rules',
386
  'settings'
387
+ );
388
  }
389
 
390
  /**
414
  $html = '';
415
  // File Check.
416
  if (file_exists($path)) {
417
+ $data = array();
418
  $this->fetchData($tab, $data);
419
  // Processing View.
420
  $html = $generalHelper->processBaseView($path, $data);
526
  break;
527
 
528
  default:
529
+ $data = array();
530
 
531
  break;
532
  }
538
  */
539
  public function adminPages()
540
  {
541
+ return array(
542
  $this->default_page => WOO_DISCOUNT_DIR . '/view/pricing-rules.php',
543
  'cart-rules' => WOO_DISCOUNT_DIR . '/view/cart-rules.php',
544
  'settings' => WOO_DISCOUNT_DIR . '/view/settings.php',
550
  // Edit Rules
551
  'pricing-rules-view' => WOO_DISCOUNT_DIR . '/view/view-pricing-rules.php',
552
  'cart-rules-view' => WOO_DISCOUNT_DIR . '/view/view-cart-rules.php'
553
+ );
554
  }
555
 
556
  /**
includes/pricing-rules.php CHANGED
@@ -79,7 +79,7 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
79
  */
80
  public function save($request)
81
  {
82
- var_dump($request);
83
  // die();
84
  // foreach ($request as $index => $value) {
85
  // $request[$index] = $value;
@@ -100,27 +100,27 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
100
  $slug = str_replace(' ', '-', $slug);
101
 
102
  if ($id) {
103
- $post = [
104
  'ID' => $id,
105
  'post_title' => $title,
106
  'post_name' => $slug,
107
  'post_content' => 'New Rule',
108
  'post_type' => $this->post_type,
109
  'post_status' => 'publish'
110
- ];
111
  wp_update_post($post);
112
  } else {
113
- $post = [
114
  'post_title' => $title,
115
  'post_name' => $slug,
116
  'post_content' => 'New Rule',
117
  'post_type' => $this->post_type,
118
  'post_status' => 'publish'
119
- ];
120
  $id = wp_insert_post($post);
121
  }
122
 
123
- $form = [
124
  'rule_name',
125
  'rule_descr',
126
  'rule_method',
@@ -137,7 +137,7 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
137
  'customer',
138
  'discount_range',
139
  'rule_order'
140
- ];
141
 
142
  //--------------------------------------------------------------------------------------------------------------
143
  // Manage Products with it's ID or Category.
@@ -267,9 +267,9 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
267
  {
268
  if (is_null($this->matched_sets)) return false;
269
 
270
- $discount_log = [
271
  'line_discount' => $this->matched_sets,
272
- ];
273
  WC()->session->set('woo_price_discount', json_encode($discount_log));
274
  }
275
 
@@ -293,7 +293,7 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
293
  if (is_null($rules) || !isset($rules)) return false;
294
 
295
  // Start with empty set.
296
- $rule_set = [];
297
 
298
  foreach ($rules as $index => $rule) {
299
  $status = (isset($rule->status) ? $rule->status : false);
@@ -332,7 +332,7 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
332
  {
333
  if (empty($this->rules)) return false;
334
 
335
- $ordered_rules = [];
336
 
337
  // Make associative array with Order ID.
338
  foreach ($this->rules as $index => $rule) {
@@ -356,6 +356,7 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
356
  public function applyRules()
357
  {
358
  global $woocommerce;
 
359
  // If there is no rules, then return false.
360
  if (!isset($this->rules)) return false;
361
 
@@ -365,6 +366,7 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
365
  // To Generate Valid Rule sets.
366
  $this->generateRuleSets($woocommerce);
367
  // Sort cart by price ascending
 
368
  $cart_contents = $this->sortCartPrice($woocommerce->cart->cart_contents, 'asc');
369
  foreach ($cart_contents as $index => $item) {
370
  $this->matchRules($index, $item);
@@ -380,7 +382,7 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
380
  */
381
  public function generateRuleSets($woocommerce)
382
  {
383
- $rule_sets = [];
384
 
385
  if (!isset($this->rules)) return false;
386
 
@@ -425,6 +427,7 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
425
  $rule_sets[$index]['allow']['users'] = $this->checkWithUsers($rule, $woocommerce);
426
  }
427
  }
 
428
  }
429
 
430
  // If Current Customer is not Allowed to use this discount, then it's going to be removed.
@@ -441,40 +444,60 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
441
  * @param $index
442
  * @param array $item line item.
443
  */
444
- public function matchRules($index, $item)
445
  {
446
- $applied_rules = [];
447
- $quantity = (isset($item['quantity']) ? $item['quantity'] : 0);
448
- $i = 0;
449
- foreach ($this->rule_sets as $id => $rule) {
 
450
 
451
- if (isset($rule['type'])) {
 
452
 
453
  // Working with Products and Category.
454
- if (isset($rule['type']['specific_products'])) {
455
- if ($this->isItemInProductList($rule['type']['specific_products'], $item)) {
456
- $applied_rules[$i]['amount'] = $this->getAdjustmentAmount($quantity, (object)$this->array_first($rule['discount']));
457
- $applied_rules[$i]['name'] = $rule['name'];
458
- $applied_rules[$i]['item'] = $index;
459
- $applied_rules[$i]['id'] = $item['product_id'];
460
- }
461
- } elseif (isset($rule['type']['specific_category'])) {
462
- if ($this->isItemInCategoryList($rule['type']['specific_category'], $item)) {
463
- $applied_rules[$i]['amount'] = $this->getAdjustmentAmount($quantity, (object)$this->array_first($rule['discount']));
464
- $applied_rules[$i]['name'] = $rule['name'];
465
- $applied_rules[$i]['item'] = $index;
466
- $applied_rules[$i]['id'] = $item['product_id'];
467
- }
468
- } else {
469
- $applied_rules[$i]['amount'] = $this->getAdjustmentAmount($quantity, (object)$this->array_first($rule['discount']));
470
- $applied_rules[$i]['name'] = $rule['name'];
471
- $applied_rules[$i]['item'] = $index;
472
- $applied_rules[$i]['id'] = $item['product_id'];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473
  }
474
  }
475
- $i++;
476
  }
477
- $this->matched_sets[$index] = $applied_rules;
478
  }
479
 
480
  /**
@@ -500,7 +523,7 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
500
  */
501
  public function getAdjustmentAmount($quantity, $discount_range)
502
  {
503
- $adjustment = [];
504
  if (!is_array($discount_range) && !is_object($discount_range)) return false;
505
 
506
  $range = $discount_range;
@@ -536,7 +559,7 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
536
 
537
  $user = get_current_user_id();
538
 
539
- if (count(array_intersect($users, [$user])) > 0) {
540
  $allowed = 'yes';
541
  }
542
 
@@ -553,7 +576,8 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
553
  public function isItemInProductList($product_list, $product)
554
  {
555
  if (!isset($product['product_id'])) return false;
556
- if (count(array_intersect($product_list, [$product['product_id']])) == 1) {
 
557
  return true;
558
  } else {
559
  return false;
@@ -661,7 +685,7 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
661
  */
662
  public function checkWithProducts($rule, $woocommerce)
663
  {
664
- $specific_product_list = [];
665
  if (is_string($rule->product_to_apply)) {
666
  $specific_product_list = json_decode($rule->product_to_apply, true);
667
  }
@@ -677,7 +701,7 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
677
  */
678
  public function checkWithCategory($rule, $woocommerce)
679
  {
680
- $specific_category_list = [];
681
  if (is_string($rule->category_to_apply)) {
682
  $specific_category_list = json_decode($rule->category_to_apply, true);
683
  }
@@ -706,7 +730,7 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
706
  */
707
  public function getDiscountRangeList($rule)
708
  {
709
- $discount_range_list = [];
710
  if (is_string($rule->discount_range)) {
711
  $discount_range_list = json_decode($rule->discount_range);
712
  }
@@ -764,7 +788,7 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
764
 
765
  if ($rule->product_to_apply != null) {
766
  $status = false;
767
- if (array_intersect($products_to_apply, [$id]) > 0) {
768
  $status = true;
769
  }
770
  }
@@ -794,15 +818,15 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
794
  if ($rule->customer == 'all') {
795
  $status = true;
796
  } else {
797
- $users = (is_string($rule->users_to_apply) ? json_decode($rule->users_to_apply, true) : []);
798
  $user_id = get_current_user_id();
799
- if (count(array_intersect($users, [$user_id])) > 0) {
800
  $status = true;
801
  }
802
  }
803
  }
804
  if ($status) {
805
- $discount_range[] = (isset($rule->discount_range) ? json_decode($rule->discount_range) : []);
806
  }
807
  }
808
  return $discount_range;
@@ -1021,4 +1045,4 @@ if ( ! class_exists( 'woo_dicount_rules_pricingRules' ) ) {
1021
  return $item_price;
1022
  }
1023
  }
1024
- }
79
  */
80
  public function save($request)
81
  {
82
+ // var_dump($request);
83
  // die();
84
  // foreach ($request as $index => $value) {
85
  // $request[$index] = $value;
100
  $slug = str_replace(' ', '-', $slug);
101
 
102
  if ($id) {
103
+ $post = array(
104
  'ID' => $id,
105
  'post_title' => $title,
106
  'post_name' => $slug,
107
  'post_content' => 'New Rule',
108
  'post_type' => $this->post_type,
109
  'post_status' => 'publish'
110
+ );
111
  wp_update_post($post);
112
  } else {
113
+ $post = array(
114
  'post_title' => $title,
115
  'post_name' => $slug,
116
  'post_content' => 'New Rule',
117
  'post_type' => $this->post_type,
118
  'post_status' => 'publish'
119
+ );
120
  $id = wp_insert_post($post);
121
  }
122
 
123
+ $form = array(
124
  'rule_name',
125
  'rule_descr',
126
  'rule_method',
137
  'customer',
138
  'discount_range',
139
  'rule_order'
140
+ );
141
 
142
  //--------------------------------------------------------------------------------------------------------------
143
  // Manage Products with it's ID or Category.
267
  {
268
  if (is_null($this->matched_sets)) return false;
269
 
270
+ $discount_log = array(
271
  'line_discount' => $this->matched_sets,
272
+ );
273
  WC()->session->set('woo_price_discount', json_encode($discount_log));
274
  }
275
 
293
  if (is_null($rules) || !isset($rules)) return false;
294
 
295
  // Start with empty set.
296
+ $rule_set = array();
297
 
298
  foreach ($rules as $index => $rule) {
299
  $status = (isset($rule->status) ? $rule->status : false);
332
  {
333
  if (empty($this->rules)) return false;
334
 
335
+ $ordered_rules = array();
336
 
337
  // Make associative array with Order ID.
338
  foreach ($this->rules as $index => $rule) {
356
  public function applyRules()
357
  {
358
  global $woocommerce;
359
+
360
  // If there is no rules, then return false.
361
  if (!isset($this->rules)) return false;
362
 
366
  // To Generate Valid Rule sets.
367
  $this->generateRuleSets($woocommerce);
368
  // Sort cart by price ascending
369
+
370
  $cart_contents = $this->sortCartPrice($woocommerce->cart->cart_contents, 'asc');
371
  foreach ($cart_contents as $index => $item) {
372
  $this->matchRules($index, $item);
382
  */
383
  public function generateRuleSets($woocommerce)
384
  {
385
+ $rule_sets = array();
386
 
387
  if (!isset($this->rules)) return false;
388
 
427
  $rule_sets[$index]['allow']['users'] = $this->checkWithUsers($rule, $woocommerce);
428
  }
429
  }
430
+ $rule_sets[$index]['apply_to'] = $rule->apply_to;
431
  }
432
 
433
  // If Current Customer is not Allowed to use this discount, then it's going to be removed.
444
  * @param $index
445
  * @param array $item line item.
446
  */
447
+ public function matchRules( $index, $item )
448
  {
449
+ $applied_rules = array();
450
+ $quantity = ( isset( $item['quantity'] ) ? $item['quantity'] : 0 );
451
+ $i = 0;
452
+ foreach ( $this->rule_sets as $id => $rule )
453
+ {
454
 
455
+ if ( isset( $rule['type'] ) && isset( $rule['apply_to'] ) )
456
+ {
457
 
458
  // Working with Products and Category.
459
+ switch ( $rule['apply_to'] )
460
+ {
461
+
462
+ case 'specific_products':
463
+
464
+ if ( $this->isItemInProductList( $rule['type']['specific_products'], $item ) )
465
+ {
466
+ $applied_rules[ $i ]['amount'] = $this->getAdjustmentAmount( $quantity, (object) $this->array_first( $rule['discount'] ) );
467
+ $applied_rules[ $i ]['name'] = $rule['name'];
468
+ $applied_rules[ $i ]['item'] = $index;
469
+ $applied_rules[ $i ]['id'] = $item['product_id'];
470
+ }
471
+
472
+ break;
473
+
474
+ case 'specific_category':
475
+ if ( $this->isItemInCategoryList( $rule['type']['specific_category'], $item ) )
476
+ {
477
+ $applied_rules[ $i ]['amount'] = $this->getAdjustmentAmount( $quantity, (object) $this->array_first( $rule['discount'] ) );
478
+ $applied_rules[ $i ]['name'] = $rule['name'];
479
+ $applied_rules[ $i ]['item'] = $index;
480
+ $applied_rules[ $i ]['id'] = $item['product_id'];
481
+ }
482
+
483
+
484
+ break;
485
+
486
+ case 'all_products':
487
+ default:
488
+
489
+ $applied_rules[ $i ]['amount'] = $this->getAdjustmentAmount( $quantity, (object) $this->array_first( $rule['discount'] ) );
490
+ $applied_rules[ $i ]['name'] = $rule['name'];
491
+ $applied_rules[ $i ]['item'] = $index;
492
+ $applied_rules[ $i ]['id'] = $item['product_id'];
493
+
494
+ break;
495
+
496
  }
497
  }
498
+ $i ++;
499
  }
500
+ $this->matched_sets[ $index ] = $applied_rules;
501
  }
502
 
503
  /**
523
  */
524
  public function getAdjustmentAmount($quantity, $discount_range)
525
  {
526
+ $adjustment = array();
527
  if (!is_array($discount_range) && !is_object($discount_range)) return false;
528
 
529
  $range = $discount_range;
559
 
560
  $user = get_current_user_id();
561
 
562
+ if (count(array_intersect($users, array($user))) > 0) {
563
  $allowed = 'yes';
564
  }
565
 
576
  public function isItemInProductList($product_list, $product)
577
  {
578
  if (!isset($product['product_id'])) return false;
579
+ if(!is_array($product_list)) $product_list = (array) $product_list;
580
+ if (count(array_intersect($product_list, array($product['product_id']))) == 1) {
581
  return true;
582
  } else {
583
  return false;
685
  */
686
  public function checkWithProducts($rule, $woocommerce)
687
  {
688
+ $specific_product_list = array();
689
  if (is_string($rule->product_to_apply)) {
690
  $specific_product_list = json_decode($rule->product_to_apply, true);
691
  }
701
  */
702
  public function checkWithCategory($rule, $woocommerce)
703
  {
704
+ $specific_category_list = array();
705
  if (is_string($rule->category_to_apply)) {
706
  $specific_category_list = json_decode($rule->category_to_apply, true);
707
  }
730
  */
731
  public function getDiscountRangeList($rule)
732
  {
733
+ $discount_range_list = array();
734
  if (is_string($rule->discount_range)) {
735
  $discount_range_list = json_decode($rule->discount_range);
736
  }
788
 
789
  if ($rule->product_to_apply != null) {
790
  $status = false;
791
+ if (array_intersect($products_to_apply, array($id)) > 0) {
792
  $status = true;
793
  }
794
  }
818
  if ($rule->customer == 'all') {
819
  $status = true;
820
  } else {
821
+ $users = (is_string($rule->users_to_apply) ? json_decode($rule->users_to_apply, true) : array());
822
  $user_id = get_current_user_id();
823
+ if (count(array_intersect($users, array($user_id))) > 0) {
824
  $status = true;
825
  }
826
  }
827
  }
828
  if ($status) {
829
+ $discount_range[] = (isset($rule->discount_range) ? json_decode($rule->discount_range) : array());
830
  }
831
  }
832
  return $discount_range;
1045
  return $item_price;
1046
  }
1047
  }
1048
+ }
loader.php CHANGED
@@ -3,7 +3,7 @@ if (!defined('ABSPATH')) exit; // Exit if accessed directly
3
  /**
4
  * Version of Woo Discount Rules.
5
  */
6
- define('WOO_DISCOUNT_VERSION', '1.2');
7
  /**
8
  * Required Version of WooCommerce to Run.
9
  */
3
  /**
4
  * Version of Woo Discount Rules.
5
  */
6
+ define('WOO_DISCOUNT_VERSION', '1.3');
7
  /**
8
  * Required Version of WooCommerce to Run.
9
  */
readme.txt CHANGED
@@ -1,10 +1,10 @@
1
- === Woo Discount Rules ===
2
  Contributors: flycart
3
- Donate link:
4
  Tags: woocommerce, ecommerce, discounts, coupons, promotion, campaigns, sales, price rules, advanced coupons, advanced discounts
5
  Requires at least: 4.4.1
6
- Tested up to: 4.6.1
7
- Stable tag: 1.2
8
  License: GPLv3 or later
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
@@ -15,7 +15,9 @@ Create any type of discount and pricing rules in your WooCommerce Store. Run fle
15
  Increase your sales multifold by offering flexible, customised discounts in your WooCommerce online store. Attract more customers.
16
  Encourage bulk purchases by providing discounts.
17
 
18
- ** Use cases **
 
 
19
 
20
  * Buy 5 items and get 10 % discount
21
  * Buy T-shirts from Winter Collections and save 10 %
@@ -25,7 +27,7 @@ Encourage bulk purchases by providing discounts.
25
  * Add 5 items to your shopping bag and get 10% discount
26
  * Special offer for Customers from California. Get 25 % discount
27
 
28
- ** Adjustment rules **
29
 
30
  * Category specific discount on product price
31
  * Date and Time based discount on product price
@@ -35,10 +37,10 @@ Encourage bulk purchases by providing discounts.
35
  * Total Sum of Item quantities
36
  * Shipping address based discount rule
37
 
38
- ** Conditions based on **
39
  Products, categories, customers, customer roles, shipping location and more.
40
 
41
- ** Rules apply based on **
42
 
43
  * Products
44
  * Line items in cart
@@ -46,9 +48,9 @@ Products, categories, customers, customer roles, shipping location and more.
46
  * Line item quantity
47
  * Total cart quantity
48
 
49
- === PRO version ===
50
 
51
- === PRO only features ===
52
 
53
  * Fixed price discount for Quantity based discount conditions ( Get $9 discount for purchasing over 6 items)
54
  * Category specific discount conditions ( Get 25 % off on all items under Summer Collection )
@@ -78,6 +80,14 @@ More information could be found in the documentation
78
 
79
  == Frequently asked questions ==
80
 
 
 
 
 
 
 
 
 
81
  == Changelog ==
82
 
83
- == Upgrade notice ==
1
+ === WooCommerce Discount Rules ===
2
  Contributors: flycart
3
+ Donate link: https://flycart.org/
4
  Tags: woocommerce, ecommerce, discounts, coupons, promotion, campaigns, sales, price rules, advanced coupons, advanced discounts
5
  Requires at least: 4.4.1
6
+ Tested up to: 4.7
7
+ Stable tag: 1.3
8
  License: GPLv3 or later
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
15
  Increase your sales multifold by offering flexible, customised discounts in your WooCommerce online store. Attract more customers.
16
  Encourage bulk purchases by providing discounts.
17
 
18
+ [View Demo](http://demo.flycart.org/woo-discount-rules) | [Documentation](https://www.flycart.org/docs/wordpress/woo-discount-rules)
19
+
20
+ = Use cases =
21
 
22
  * Buy 5 items and get 10 % discount
23
  * Buy T-shirts from Winter Collections and save 10 %
27
  * Add 5 items to your shopping bag and get 10% discount
28
  * Special offer for Customers from California. Get 25 % discount
29
 
30
+ = Adjustment rules =
31
 
32
  * Category specific discount on product price
33
  * Date and Time based discount on product price
37
  * Total Sum of Item quantities
38
  * Shipping address based discount rule
39
 
40
+ = Conditions based on =
41
  Products, categories, customers, customer roles, shipping location and more.
42
 
43
+ = Rules apply based on =
44
 
45
  * Products
46
  * Line items in cart
48
  * Line item quantity
49
  * Total cart quantity
50
 
51
+ [PRO version](https://www.flycart.org/products/wordpress/woocommerce-discount-rules)
52
 
53
+ = PRO only features =
54
 
55
  * Fixed price discount for Quantity based discount conditions ( Get $9 discount for purchasing over 6 items)
56
  * Category specific discount conditions ( Get 25 % off on all items under Summer Collection )
80
 
81
  == Frequently asked questions ==
82
 
83
+ == Screenshots ==
84
+
85
+ 1. Woo Discount Rules menu
86
+ 2. Price rules and Cart rules
87
+ 3. Creating a price rule
88
+ 4. Example Promotion Offers created using Woo Discount rules
89
+ 5. Discounted price is applied in the Cart
90
+
91
  == Changelog ==
92
 
93
+ == Upgrade notice ==
view/cart-rules.php CHANGED
@@ -7,7 +7,7 @@ include_once(WOO_DISCOUNT_DIR . '/view/includes/menu.php');
7
 
8
  $config = (isset($config)) ? $config : '{}';
9
 
10
- $data = [];
11
  $rule_list = $config;
12
 
13
  //for ( WC()->cart->get_coupons as $code => $coupon){
7
 
8
  $config = (isset($config)) ? $config : '{}';
9
 
10
+ $data = array();
11
  $rule_list = $config;
12
 
13
  //for ( WC()->cart->get_coupons as $code => $coupon){
view/pricing-rules.php CHANGED
@@ -7,7 +7,7 @@ include_once(WOO_DISCOUNT_DIR . '/view/includes/menu.php');
7
 
8
  $config = (isset($config)) ? $config : '{}';
9
 
10
- $data = [];
11
  $rule_list = $config;
12
  ?>
13
 
7
 
8
  $config = (isset($config)) ? $config : '{}';
9
 
10
+ $data = array();
11
  $rule_list = $config;
12
  ?>
13
 
view/view-cart-rules.php CHANGED
@@ -21,13 +21,13 @@ if (is_string($config)) {
21
  // Dummy Object.
22
  $obj = new stdClass();
23
 
24
- $data = (isset($config[0]) ? $config[0] : []);
25
  $rule_id = (isset($data->ID)) ? $data->ID : 0;
26
 
27
- $discounts = [];
28
- $discount_rules = [];
29
  if (isset($data->discount_rule)) {
30
- $discount_rules = (is_string($data->discount_rule) ? json_decode($data->discount_rule, true) : ['' => '']);
31
  }
32
 
33
  foreach ($discount_rules as $index => $rule) {
@@ -37,7 +37,7 @@ foreach ($discount_rules as $index => $rule) {
37
  }
38
  $discount_rules = $discounts;
39
  if (empty($discount_rules)) {
40
- $discount_rules = [0 => ''];
41
  $type = 'subtotal_least';
42
  }
43
 
@@ -270,7 +270,7 @@ if (empty($discount_rules)) {
270
  <div class="col-md-3 form-group">
271
  <label> Value
272
  <?php
273
- $users_list = [];
274
  $class = 'style="display:none"';
275
  $hit = false;
276
  if ($type == 'users_in') {
@@ -291,7 +291,7 @@ if (empty($discount_rules)) {
291
  </select>
292
  </div>
293
  <?php
294
- $products_list = [];
295
  $class = 'style="display:none"';
296
  if ($type == 'products_atleast_one' || $type == 'products_not_in') {
297
  $products_list = $discount_rules[$type];;
@@ -312,7 +312,7 @@ if (empty($discount_rules)) {
312
  </select>
313
  </div>
314
  <?php
315
- $category_list = [];
316
  $class = 'style="display:none"';
317
  if ($type == 'categories_atleast_one' || $type == 'categories_not_in') {
318
  $category_list = $discount_rules[$type];;
@@ -334,7 +334,7 @@ if (empty($discount_rules)) {
334
  </select>
335
  </div>
336
  <?php
337
- $roles_list = [];
338
  $class = 'style="display:none"';
339
  if ($type == 'roles_in') {
340
  $roles_list = $discount_rules[$type];;
@@ -354,7 +354,7 @@ if (empty($discount_rules)) {
354
  </select>
355
  </div>
356
  <?php
357
- $countries_list = [];
358
  $class = 'style="display:none"';
359
  if ($type == 'shipping_countries_in') {
360
  $countries_list = $discount_rules[$type];;
21
  // Dummy Object.
22
  $obj = new stdClass();
23
 
24
+ $data = (isset($config[0]) ? $config[0] : array());
25
  $rule_id = (isset($data->ID)) ? $data->ID : 0;
26
 
27
+ $discounts = array();
28
+ $discount_rules = array();
29
  if (isset($data->discount_rule)) {
30
+ $discount_rules = (is_string($data->discount_rule) ? json_decode($data->discount_rule, true) : array('' => ''));
31
  }
32
 
33
  foreach ($discount_rules as $index => $rule) {
37
  }
38
  $discount_rules = $discounts;
39
  if (empty($discount_rules)) {
40
+ $discount_rules = array(0 => '');
41
  $type = 'subtotal_least';
42
  }
43
 
270
  <div class="col-md-3 form-group">
271
  <label> Value
272
  <?php
273
+ $users_list = array();
274
  $class = 'style="display:none"';
275
  $hit = false;
276
  if ($type == 'users_in') {
291
  </select>
292
  </div>
293
  <?php
294
+ $products_list = array();
295
  $class = 'style="display:none"';
296
  if ($type == 'products_atleast_one' || $type == 'products_not_in') {
297
  $products_list = $discount_rules[$type];;
312
  </select>
313
  </div>
314
  <?php
315
+ $category_list = array();
316
  $class = 'style="display:none"';
317
  if ($type == 'categories_atleast_one' || $type == 'categories_not_in') {
318
  $category_list = $discount_rules[$type];;
334
  </select>
335
  </div>
336
  <?php
337
+ $roles_list = array();
338
  $class = 'style="display:none"';
339
  if ($type == 'roles_in') {
340
  $roles_list = $discount_rules[$type];;
354
  </select>
355
  </div>
356
  <?php
357
+ $countries_list = array();
358
  $class = 'style="display:none"';
359
  if ($type == 'shipping_countries_in') {
360
  $countries_list = $discount_rules[$type];;
view/view-pricing-rules.php CHANGED
@@ -238,7 +238,7 @@ $rule_id = (isset($data->ID)) ? $data->ID : 0;
238
  }
239
 
240
  // Make Dummy Element.
241
- if ($discount_range == '') $discount_range = [0 => ''];
242
 
243
  foreach ($discount_range as $index => $discount) {
244
  ?>
@@ -311,4 +311,4 @@ $rule_id = (isset($data->ID)) ? $data->ID : 0;
311
  </form>
312
  </div>
313
 
314
- <?php include_once(WOO_DISCOUNT_DIR . '/view/includes/footer.php'); ?>
238
  }
239
 
240
  // Make Dummy Element.
241
+ if ($discount_range == '') $discount_range = array(0 => '');
242
 
243
  foreach ($discount_range as $index => $discount) {
244
  ?>
311
  </form>
312
  </div>
313
 
314
+ <?php include_once(WOO_DISCOUNT_DIR . '/view/includes/footer.php'); ?>
woo-discount-rules.php CHANGED
@@ -5,7 +5,7 @@
5
  * Description: Simple Discount Rules for WooCommerce.
6
  * Author: Flycart Technologies LLP
7
  * Author URI: https://www.flycart.org
8
- * Version: 1.2
9
  * Requires at least: 4.6.1
10
  */
11
 
5
  * Description: Simple Discount Rules for WooCommerce.
6
  * Author: Flycart Technologies LLP
7
  * Author URI: https://www.flycart.org
8
+ * Version: 1.3
9
  * Requires at least: 4.6.1
10
  */
11