Discount Rules for WooCommerce - Version 1.4.21

Version Description

  • 16/10/17 =
  • Fix Based on category in cart rules: Apply discount only for the subtotal of selected category products
Download this release

Release Info

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

Code changes from version 1.4.20 to 1.4.21

includes/cart-rules-3.php CHANGED
@@ -558,8 +558,15 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
558
  // Getting the Flat Rate of Discount.
559
  $discounts['to_discount'][$index] = $this->calculateDiscount($this->sub_total, array('type' => 'price', 'value' => $rule['to_discount']));
560
  } else {
 
 
 
 
 
 
 
561
  // Getting the Percentage level of Discount.
562
- $discounts['to_discount'][$index] = $this->calculateDiscount($this->sub_total, array('type' => 'percentage', 'value' => $rule['to_discount']));
563
  }
564
  // Sum of Available discount list.
565
  $discount += $discounts['to_discount'][$index];
@@ -578,8 +585,15 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
578
  // Getting the Flat Rate of Discount.
579
  $discounts['to_discount'][$index] = $this->calculateDiscount($this->sub_total, array('type' => 'price', 'value' => $rule['to_discount']));
580
  } else {
 
 
 
 
 
 
 
581
  // Getting the Percentage level of Discount.
582
- $discounts['to_discount'][$index] = $this->calculateDiscount($this->sub_total, array('type' => 'percentage', 'value' => $rule['to_discount']));
583
  }
584
  // Sum of Available discount list.
585
  $discount += $discounts['to_discount'][$index];
@@ -597,8 +611,15 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
597
  // Getting the Flat Rate of Discount.
598
  $newDiscount = $this->calculateDiscount($this->sub_total, array('type' => 'price', 'value' => $rule['to_discount']));
599
  } else {
 
 
 
 
 
 
 
600
  // Getting the Percentage level of Discount.
601
- $newDiscount = $this->calculateDiscount($this->sub_total, array('type' => 'percentage', 'value' => $rule['to_discount']));
602
  }
603
 
604
  if($newDiscount > $biggestDiscount){
@@ -619,6 +640,43 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
619
  return $discounts;
620
  }
621
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
622
  /**
623
  * Comparing the Rules with the each line item to check
624
  * and return as, matched or not.
@@ -853,17 +911,17 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
853
  if($terms){
854
  foreach ($terms as $term) {
855
  $product_id = '';
856
- $sub_total_with_tax = $quantity = $item_count = 0;
857
  if(isset($product_cat_id[$term->term_id])){
858
  $product_id = $product_cat_id[$term->term_id]->product_id.',';
859
- $sub_total_with_tax = $product_cat_id[$term->term_id]->sub_total_with_tax;
860
  $quantity = $product_cat_id[$term->term_id]->quantity;
861
  $item_count = $product_cat_id[$term->term_id]->item_count;
862
  }
863
  $productDetailObject = new stdClass();
864
  $productDetailObject->cat_id = $term->term_id;
865
  $productDetailObject->product_id = trim($product_id.$cartItem['product_id'], ',');
866
- $productDetailObject->sub_total_with_tax = $sub_total_with_tax+$cartItem['line_subtotal']+$cartItem['line_subtotal_tax'];
867
  $productDetailObject->quantity = $quantity+$cartItem['quantity'];
868
  $productDetailObject->item_count = $item_count+1;
869
  $product_cat_id[$term->term_id] = $productDetailObject;
@@ -874,14 +932,14 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
874
  $category_ids = array_keys($product_cat_id);
875
  $categoryFound = 0;
876
  $product_id = $cat_id = '';
877
- $sub_total_with_tax = $quantity = $item_count = 0;
878
  foreach ($rule as $catid){
879
  if(in_array($catid, $category_ids)){
880
  if(isset($product_cat_id[$catid])){
881
  $categoryFound = 1;
882
  $cat_id = trim($cat_id.','.$product_cat_id[$catid]->cat_id, ',');
883
  $product_id = trim($product_id.','.$product_cat_id[$catid]->product_id, ',');
884
- $sub_total_with_tax = $sub_total_with_tax+$product_cat_id[$catid]->sub_total_with_tax;
885
  $quantity = $quantity+$product_cat_id[$catid]->quantity;
886
  $item_count = $item_count+$product_cat_id[$catid]->item_count;
887
  }
@@ -893,11 +951,11 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
893
  $checkRuleTypes = array('quantity_least', 'quantity_less', 'subtotal_least', 'subtotal_less', 'item_count_least', 'item_count_less');
894
  if(in_array($rule_type, $checkRuleTypes)){
895
  if($rule_type == 'subtotal_least'){
896
- if ($sub_total_with_tax < $rule_values) {
897
  $ruleSuccess = 0;
898
  }
899
  } elseif ($rule_type == 'subtotal_less'){
900
- if ($sub_total_with_tax >= $rule_values) {
901
  $ruleSuccess = 0;
902
  }
903
  } elseif ($rule_type == 'item_count_least'){
@@ -973,8 +1031,38 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
973
  }
974
 
975
  $this->sub_total = (float)$cart_subtotal;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
976
  }
977
 
 
978
  /**
979
  * To Sum the Cart Item's Qty.
980
  *
@@ -1016,4 +1104,4 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
1016
  }
1017
 
1018
  }
1019
- }
558
  // Getting the Flat Rate of Discount.
559
  $discounts['to_discount'][$index] = $this->calculateDiscount($this->sub_total, array('type' => 'price', 'value' => $rule['to_discount']));
560
  } else {
561
+ //we will have to re-calculate the sub-total if it has category selected
562
+ if($this->is_category_specific($rule)) {
563
+ //re-calculate the sub-total
564
+ $subtotal = $this->calculate_conditional_subtotal($this->get_discounted_categories_from_json($rule));
565
+ }else {
566
+ $subtotal = $this->sub_total;
567
+ }
568
  // Getting the Percentage level of Discount.
569
+ $discounts['to_discount'][$index] = $this->calculateDiscount($subtotal, array('type' => 'percentage', 'value' => $rule['to_discount']));
570
  }
571
  // Sum of Available discount list.
572
  $discount += $discounts['to_discount'][$index];
585
  // Getting the Flat Rate of Discount.
586
  $discounts['to_discount'][$index] = $this->calculateDiscount($this->sub_total, array('type' => 'price', 'value' => $rule['to_discount']));
587
  } else {
588
+ //we will have to re-calculate the sub-total if it has category selected
589
+ if($this->is_category_specific($rule)) {
590
+ //re-calculate the sub-total
591
+ $subtotal = $this->calculate_conditional_subtotal($this->get_discounted_categories_from_json($rule));
592
+ }else {
593
+ $subtotal = $this->sub_total;
594
+ }
595
  // Getting the Percentage level of Discount.
596
+ $discounts['to_discount'][$index] = $this->calculateDiscount($subtotal, array('type' => 'percentage', 'value' => $rule['to_discount']));
597
  }
598
  // Sum of Available discount list.
599
  $discount += $discounts['to_discount'][$index];
611
  // Getting the Flat Rate of Discount.
612
  $newDiscount = $this->calculateDiscount($this->sub_total, array('type' => 'price', 'value' => $rule['to_discount']));
613
  } else {
614
+ //we will have to re-calculate the sub-total if it has category selected
615
+ if($this->is_category_specific($rule)) {
616
+ //re-calculate the sub-total
617
+ $subtotal = $this->calculate_conditional_subtotal($this->get_discounted_categories_from_json($rule));
618
+ }else {
619
+ $subtotal = $this->sub_total;
620
+ }
621
  // Getting the Percentage level of Discount.
622
+ $newDiscount = $this->calculateDiscount($subtotal, array('type' => 'percentage', 'value' => $rule['to_discount']));
623
  }
624
 
625
  if($newDiscount > $biggestDiscount){
640
  return $discounts;
641
  }
642
 
643
+ /**
644
+ * Check is specific to category
645
+ * */
646
+ public function is_category_specific($rule) {
647
+ if(count($this->get_discounted_categories_from_json($rule))) {
648
+ return true;
649
+ }
650
+ return false;
651
+ }
652
+
653
+ /**
654
+ * get discount categories from rule
655
+ * */
656
+ public function get_discounted_categories_from_json($rule)
657
+ {
658
+ $categories = array();
659
+ if ( ! empty( $rule['discount_rule'] ) )
660
+ {
661
+ if(!is_object($rule['discount_rule'])) {
662
+ //assume it is a json string and parse
663
+ $rules = json_decode($rule['discount_rule'], true);
664
+ }
665
+
666
+ if(count($rules)) {
667
+ foreach($rules as $rule) {
668
+ if(array_key_exists('categories_in', $rule)) {
669
+ $categories = $rule['categories_in'];
670
+ break;
671
+ }
672
+ }
673
+ }
674
+
675
+
676
+ }
677
+ return $categories;
678
+ }
679
+
680
  /**
681
  * Comparing the Rules with the each line item to check
682
  * and return as, matched or not.
911
  if($terms){
912
  foreach ($terms as $term) {
913
  $product_id = '';
914
+ $sub_total = $quantity = $item_count = 0;
915
  if(isset($product_cat_id[$term->term_id])){
916
  $product_id = $product_cat_id[$term->term_id]->product_id.',';
917
+ $sub_total = $product_cat_id[$term->term_id]->sub_total;
918
  $quantity = $product_cat_id[$term->term_id]->quantity;
919
  $item_count = $product_cat_id[$term->term_id]->item_count;
920
  }
921
  $productDetailObject = new stdClass();
922
  $productDetailObject->cat_id = $term->term_id;
923
  $productDetailObject->product_id = trim($product_id.$cartItem['product_id'], ',');
924
+ $productDetailObject->sub_total = $sub_total+$cartItem['line_subtotal'];//+$cartItem['line_subtotal_tax'];
925
  $productDetailObject->quantity = $quantity+$cartItem['quantity'];
926
  $productDetailObject->item_count = $item_count+1;
927
  $product_cat_id[$term->term_id] = $productDetailObject;
932
  $category_ids = array_keys($product_cat_id);
933
  $categoryFound = 0;
934
  $product_id = $cat_id = '';
935
+ $sub_total = $quantity = $item_count = 0;
936
  foreach ($rule as $catid){
937
  if(in_array($catid, $category_ids)){
938
  if(isset($product_cat_id[$catid])){
939
  $categoryFound = 1;
940
  $cat_id = trim($cat_id.','.$product_cat_id[$catid]->cat_id, ',');
941
  $product_id = trim($product_id.','.$product_cat_id[$catid]->product_id, ',');
942
+ $sub_total = $sub_total+$product_cat_id[$catid]->sub_total;
943
  $quantity = $quantity+$product_cat_id[$catid]->quantity;
944
  $item_count = $item_count+$product_cat_id[$catid]->item_count;
945
  }
951
  $checkRuleTypes = array('quantity_least', 'quantity_less', 'subtotal_least', 'subtotal_less', 'item_count_least', 'item_count_less');
952
  if(in_array($rule_type, $checkRuleTypes)){
953
  if($rule_type == 'subtotal_least'){
954
+ if ($sub_total < $rule_values) {
955
  $ruleSuccess = 0;
956
  }
957
  } elseif ($rule_type == 'subtotal_less'){
958
+ if ($sub_total >= $rule_values) {
959
  $ruleSuccess = 0;
960
  }
961
  } elseif ($rule_type == 'item_count_least'){
1031
  }
1032
 
1033
  $this->sub_total = (float)$cart_subtotal;
1034
+
1035
+ }
1036
+
1037
+ public function calculate_conditional_subtotal($conditions) {
1038
+
1039
+ $cart_subtotal = 0;
1040
+ // Iterate over all cart items and
1041
+ foreach ($this->cart_items as $cart_item_key => $cart_item) {
1042
+
1043
+ if($this->does_item_belong_to_category($conditions, $cart_item['data'])) {
1044
+
1045
+ //total should be specific to the products from certan categories
1046
+ $quantity = (isset($cart_item['quantity']) && $cart_item['quantity']) ? $cart_item['quantity'] : 1;
1047
+ $cart_subtotal += $cart_item['data']->get_price() * $quantity;
1048
+ }
1049
+
1050
+ }
1051
+
1052
+ return (float)$cart_subtotal;
1053
+
1054
+ }
1055
+
1056
+ public function does_item_belong_to_category($categories, $product) {
1057
+
1058
+ $result = array_intersect($categories, $product->get_category_ids());
1059
+ if(is_array($result) && count($result) > 0) {
1060
+ return true;
1061
+ }
1062
+ return false;
1063
  }
1064
 
1065
+
1066
  /**
1067
  * To Sum the Cart Item's Qty.
1068
  *
1104
  }
1105
 
1106
  }
1107
+ }
includes/cart-rules.php CHANGED
@@ -559,8 +559,15 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
559
  // Getting the Flat Rate of Discount.
560
  $discounts['to_discount'][$index] = $this->calculateDiscount($this->sub_total, array('type' => 'price', 'value' => $rule['to_discount']));
561
  } else {
 
 
 
 
 
 
 
562
  // Getting the Percentage level of Discount.
563
- $discounts['to_discount'][$index] = $this->calculateDiscount($this->sub_total, array('type' => 'percentage', 'value' => $rule['to_discount']));
564
  }
565
  // Sum of Available discount list.
566
  $discount += $discounts['to_discount'][$index];
@@ -579,8 +586,15 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
579
  // Getting the Flat Rate of Discount.
580
  $discounts['to_discount'][$index] = $this->calculateDiscount($this->sub_total, array('type' => 'price', 'value' => $rule['to_discount']));
581
  } else {
 
 
 
 
 
 
 
582
  // Getting the Percentage level of Discount.
583
- $discounts['to_discount'][$index] = $this->calculateDiscount($this->sub_total, array('type' => 'percentage', 'value' => $rule['to_discount']));
584
  }
585
  // Sum of Available discount list.
586
  $discount += $discounts['to_discount'][$index];
@@ -598,8 +612,15 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
598
  // Getting the Flat Rate of Discount.
599
  $newDiscount = $this->calculateDiscount($this->sub_total, array('type' => 'price', 'value' => $rule['to_discount']));
600
  } else {
 
 
 
 
 
 
 
601
  // Getting the Percentage level of Discount.
602
- $newDiscount = $this->calculateDiscount($this->sub_total, array('type' => 'percentage', 'value' => $rule['to_discount']));
603
  }
604
 
605
  if($newDiscount > $biggestDiscount){
@@ -620,6 +641,43 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
620
  return $discounts;
621
  }
622
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
623
  /**
624
  * Comparing the Rules with the each line item to check
625
  * and return as, matched or not.
@@ -854,17 +912,17 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
854
  if($terms){
855
  foreach ($terms as $term) {
856
  $product_id = '';
857
- $sub_total_with_tax = $quantity = $item_count = 0;
858
  if(isset($product_cat_id[$term->term_id])){
859
  $product_id = $product_cat_id[$term->term_id]->product_id.',';
860
- $sub_total_with_tax = $product_cat_id[$term->term_id]->sub_total_with_tax;
861
  $quantity = $product_cat_id[$term->term_id]->quantity;
862
  $item_count = $product_cat_id[$term->term_id]->item_count;
863
  }
864
  $productDetailObject = new stdClass();
865
  $productDetailObject->cat_id = $term->term_id;
866
  $productDetailObject->product_id = trim($product_id.$cartItem['product_id'], ',');
867
- $productDetailObject->sub_total_with_tax = $sub_total_with_tax+$cartItem['line_subtotal']+$cartItem['line_subtotal_tax'];
868
  $productDetailObject->quantity = $quantity+$cartItem['quantity'];
869
  $productDetailObject->item_count = $item_count+1;
870
  $product_cat_id[$term->term_id] = $productDetailObject;
@@ -875,14 +933,14 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
875
  $category_ids = array_keys($product_cat_id);
876
  $categoryFound = 0;
877
  $product_id = $cat_id = '';
878
- $sub_total_with_tax = $quantity = $item_count = 0;
879
  foreach ($rule as $catid){
880
  if(in_array($catid, $category_ids)){
881
  if(isset($product_cat_id[$catid])){
882
  $categoryFound = 1;
883
  $cat_id = trim($cat_id.','.$product_cat_id[$catid]->cat_id, ',');
884
  $product_id = trim($product_id.','.$product_cat_id[$catid]->product_id, ',');
885
- $sub_total_with_tax = $sub_total_with_tax+$product_cat_id[$catid]->sub_total_with_tax;
886
  $quantity = $quantity+$product_cat_id[$catid]->quantity;
887
  $item_count = $item_count+$product_cat_id[$catid]->item_count;
888
  }
@@ -894,11 +952,11 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
894
  $checkRuleTypes = array('quantity_least', 'quantity_less', 'subtotal_least', 'subtotal_less', 'item_count_least', 'item_count_less');
895
  if(in_array($rule_type, $checkRuleTypes)){
896
  if($rule_type == 'subtotal_least'){
897
- if ($sub_total_with_tax < $rule_values) {
898
  $ruleSuccess = 0;
899
  }
900
  } elseif ($rule_type == 'subtotal_less'){
901
- if ($sub_total_with_tax >= $rule_values) {
902
  $ruleSuccess = 0;
903
  }
904
  } elseif ($rule_type == 'item_count_least'){
@@ -976,6 +1034,40 @@ if (!class_exists('woo_dicount_rules_cartRules')) {
976
  $this->sub_total = (float)$cart_subtotal;
977
  }
978
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
979
  /**
980
  * To Sum the Cart Item's Qty.
981
  *
559
  // Getting the Flat Rate of Discount.
560
  $discounts['to_discount'][$index] = $this->calculateDiscount($this->sub_total, array('type' => 'price', 'value' => $rule['to_discount']));
561
  } else {
562
+ //we will have to re-calculate the sub-total if it has category selected
563
+ if($this->is_category_specific($rule)) {
564
+ //re-calculate the sub-total
565
+ $subtotal = $this->calculate_conditional_subtotal($this->get_discounted_categories_from_json($rule));
566
+ }else {
567
+ $subtotal = $this->sub_total;
568
+ }
569
  // Getting the Percentage level of Discount.
570
+ $discounts['to_discount'][$index] = $this->calculateDiscount($subtotal, array('type' => 'percentage', 'value' => $rule['to_discount']));
571
  }
572
  // Sum of Available discount list.
573
  $discount += $discounts['to_discount'][$index];
586
  // Getting the Flat Rate of Discount.
587
  $discounts['to_discount'][$index] = $this->calculateDiscount($this->sub_total, array('type' => 'price', 'value' => $rule['to_discount']));
588
  } else {
589
+ //we will have to re-calculate the sub-total if it has category selected
590
+ if($this->is_category_specific($rule)) {
591
+ //re-calculate the sub-total
592
+ $subtotal = $this->calculate_conditional_subtotal($this->get_discounted_categories_from_json($rule));
593
+ }else {
594
+ $subtotal = $this->sub_total;
595
+ }
596
  // Getting the Percentage level of Discount.
597
+ $discounts['to_discount'][$index] = $this->calculateDiscount($subtotal, array('type' => 'percentage', 'value' => $rule['to_discount']));
598
  }
599
  // Sum of Available discount list.
600
  $discount += $discounts['to_discount'][$index];
612
  // Getting the Flat Rate of Discount.
613
  $newDiscount = $this->calculateDiscount($this->sub_total, array('type' => 'price', 'value' => $rule['to_discount']));
614
  } else {
615
+ //we will have to re-calculate the sub-total if it has category selected
616
+ if($this->is_category_specific($rule)) {
617
+ //re-calculate the sub-total
618
+ $subtotal = $this->calculate_conditional_subtotal($this->get_discounted_categories_from_json($rule));
619
+ }else {
620
+ $subtotal = $this->sub_total;
621
+ }
622
  // Getting the Percentage level of Discount.
623
+ $newDiscount = $this->calculateDiscount($subtotal, array('type' => 'percentage', 'value' => $rule['to_discount']));
624
  }
625
 
626
  if($newDiscount > $biggestDiscount){
641
  return $discounts;
642
  }
643
 
644
+ /**
645
+ * Check is specific to category
646
+ * */
647
+ public function is_category_specific($rule) {
648
+ if(count($this->get_discounted_categories_from_json($rule))) {
649
+ return true;
650
+ }
651
+ return false;
652
+ }
653
+
654
+ /**
655
+ * get discount categories from rule
656
+ * */
657
+ public function get_discounted_categories_from_json($rule)
658
+ {
659
+ $categories = array();
660
+ if ( ! empty( $rule['discount_rule'] ) )
661
+ {
662
+ if(!is_object($rule['discount_rule'])) {
663
+ //assume it is a json string and parse
664
+ $rules = json_decode($rule['discount_rule'], true);
665
+ }
666
+
667
+ if(count($rules)) {
668
+ foreach($rules as $rule) {
669
+ if(array_key_exists('categories_in', $rule)) {
670
+ $categories = $rule['categories_in'];
671
+ break;
672
+ }
673
+ }
674
+ }
675
+
676
+
677
+ }
678
+ return $categories;
679
+ }
680
+
681
  /**
682
  * Comparing the Rules with the each line item to check
683
  * and return as, matched or not.
912
  if($terms){
913
  foreach ($terms as $term) {
914
  $product_id = '';
915
+ $sub_total = $quantity = $item_count = 0;
916
  if(isset($product_cat_id[$term->term_id])){
917
  $product_id = $product_cat_id[$term->term_id]->product_id.',';
918
+ $sub_total = $product_cat_id[$term->term_id]->sub_total;
919
  $quantity = $product_cat_id[$term->term_id]->quantity;
920
  $item_count = $product_cat_id[$term->term_id]->item_count;
921
  }
922
  $productDetailObject = new stdClass();
923
  $productDetailObject->cat_id = $term->term_id;
924
  $productDetailObject->product_id = trim($product_id.$cartItem['product_id'], ',');
925
+ $productDetailObject->sub_total = $sub_total+$cartItem['line_subtotal'];//+$cartItem['line_subtotal_tax'];
926
  $productDetailObject->quantity = $quantity+$cartItem['quantity'];
927
  $productDetailObject->item_count = $item_count+1;
928
  $product_cat_id[$term->term_id] = $productDetailObject;
933
  $category_ids = array_keys($product_cat_id);
934
  $categoryFound = 0;
935
  $product_id = $cat_id = '';
936
+ $sub_total = $quantity = $item_count = 0;
937
  foreach ($rule as $catid){
938
  if(in_array($catid, $category_ids)){
939
  if(isset($product_cat_id[$catid])){
940
  $categoryFound = 1;
941
  $cat_id = trim($cat_id.','.$product_cat_id[$catid]->cat_id, ',');
942
  $product_id = trim($product_id.','.$product_cat_id[$catid]->product_id, ',');
943
+ $sub_total = $sub_total+$product_cat_id[$catid]->sub_total;
944
  $quantity = $quantity+$product_cat_id[$catid]->quantity;
945
  $item_count = $item_count+$product_cat_id[$catid]->item_count;
946
  }
952
  $checkRuleTypes = array('quantity_least', 'quantity_less', 'subtotal_least', 'subtotal_less', 'item_count_least', 'item_count_less');
953
  if(in_array($rule_type, $checkRuleTypes)){
954
  if($rule_type == 'subtotal_least'){
955
+ if ($sub_total < $rule_values) {
956
  $ruleSuccess = 0;
957
  }
958
  } elseif ($rule_type == 'subtotal_less'){
959
+ if ($sub_total >= $rule_values) {
960
  $ruleSuccess = 0;
961
  }
962
  } elseif ($rule_type == 'item_count_least'){
1034
  $this->sub_total = (float)$cart_subtotal;
1035
  }
1036
 
1037
+ public function calculate_conditional_subtotal($conditions) {
1038
+
1039
+ $cart_subtotal = 0;
1040
+ // Iterate over all cart items and
1041
+ foreach ($this->cart_items as $cart_item_key => $cart_item) {
1042
+
1043
+ if($this->does_item_belong_to_category($conditions, $cart_item['data'])) {
1044
+
1045
+ //total should be specific to the products from certan categories
1046
+ $quantity = (isset($cart_item['quantity']) && $cart_item['quantity']) ? $cart_item['quantity'] : 1;
1047
+ $cart_subtotal += $cart_item['data']->price * $quantity;
1048
+ }
1049
+
1050
+ }
1051
+
1052
+ return (float)$cart_subtotal;
1053
+
1054
+ }
1055
+
1056
+ public function does_item_belong_to_category($categories, $product) {
1057
+ $cat_id = array();
1058
+ $terms = get_the_terms ( $product->id, 'product_cat' );
1059
+ if(!empty($terms))
1060
+ foreach ( $terms as $term ) {
1061
+ $cat_id[] = $term->term_id;
1062
+ }
1063
+ $result = array_intersect($categories, $cat_id);
1064
+ if(is_array($result) && count($result) > 0) {
1065
+ return true;
1066
+ }
1067
+ return false;
1068
+ }
1069
+
1070
+
1071
  /**
1072
  * To Sum the Cart Item's Qty.
1073
  *
readme.txt CHANGED
@@ -4,7 +4,7 @@ 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.8.1
7
- Stable tag: 1.4.20
8
  License: GPLv3 or later
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
@@ -13,7 +13,7 @@ Create simple to complex discount rules, promotions for your WooCommerce online
13
 
14
  WooCommerce discount rules helps you to create any type of discount and pricing rules in your WooCommerce store. Increase your sales multifold by offering flexible, customised discounts and free products to your customers. Encourage your customers to purchase more by providing bulk discounts.
15
 
16
- [View Demo](http://demo.flycart.org/woo-discount-rules) | [Documentation with real examples](https://www.flycart.org/docs/wordpress/woo-discount-rules) | [Ask Questions](https://www.flycart.org/support/6-wordpress-plugins)
17
 
18
  = Use cases =
19
 
@@ -42,14 +42,14 @@ Conditions can be based products, categories, customers, customer roles, shippin
42
 
43
  * All products
44
  * Specific products / Selected products
45
- * All categories (PRO feature)
46
- * Selected categories (PRO feature)
47
- * All customers (PRO feature)
48
- * Selected customers (PRO feature)
49
- * All customer roles (PRO feature)
50
- * Selected customer roles (PRO feature)
51
- * All Shipping locations (PRO feature)
52
- * Selected shipping locations (PRO feature)
53
 
54
  Examples:
55
 
@@ -109,6 +109,9 @@ More information could be found in the documentation
109
 
110
  == Changelog ==
111
 
 
 
 
112
  = 1.4.20 - 13/10/17 =
113
  * Feature – Based on category in cart rules for pro version
114
 
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.8.1
7
+ Stable tag: 1.4.21
8
  License: GPLv3 or later
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
13
 
14
  WooCommerce discount rules helps you to create any type of discount and pricing rules in your WooCommerce store. Increase your sales multifold by offering flexible, customised discounts and free products to your customers. Encourage your customers to purchase more by providing bulk discounts.
15
 
16
+ [View Demo](http://demo.flycart.org/woo-discount-rules) | [Documentation with real examples](https://www.flycart.org/docs/wordpress/woo-discount-rules) | [Ask Questions](https://www.flycart.org/support/6-wordpress-plugins) | [PRO version](https://www.flycart.org/products/wordpress/woocommerce-discount-rules)
17
 
18
  = Use cases =
19
 
42
 
43
  * All products
44
  * Specific products / Selected products
45
+ * All categories ([PRO feature](https://www.flycart.org/products/wordpress/woocommerce-discount-rules))
46
+ * Selected categories ([PRO feature](https://www.flycart.org/products/wordpress/woocommerce-discount-rules))
47
+ * All customers ([PRO feature](https://www.flycart.org/products/wordpress/woocommerce-discount-rules))
48
+ * Selected customers ([PRO feature](https://www.flycart.org/products/wordpress/woocommerce-discount-rules))
49
+ * All customer roles ([PRO feature](https://www.flycart.org/products/wordpress/woocommerce-discount-rules))
50
+ * Selected customer roles ([PRO feature](https://www.flycart.org/products/wordpress/woocommerce-discount-rules))
51
+ * All Shipping locations ([PRO feature](https://www.flycart.org/products/wordpress/woocommerce-discount-rules))
52
+ * Selected shipping locations ([PRO feature](https://www.flycart.org/products/wordpress/woocommerce-discount-rules))
53
 
54
  Examples:
55
 
109
 
110
  == Changelog ==
111
 
112
+ = 1.4.21 - 16/10/17 =
113
+ * Fix – Based on category in cart rules: Apply discount only for the subtotal of selected category products
114
+
115
  = 1.4.20 - 13/10/17 =
116
  * Feature – Based on category in cart rules for pro version
117
 
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.4.20
9
  * Slug: woo-discount-rules
10
  * Text Domain: woo-discount-rules
11
  * Requires at least: 4.6.1
5
  * Description: Simple Discount Rules for WooCommerce.
6
  * Author: Flycart Technologies LLP
7
  * Author URI: https://www.flycart.org
8
+ * Version: 1.4.21
9
  * Slug: woo-discount-rules
10
  * Text Domain: woo-discount-rules
11
  * Requires at least: 4.6.1