Discount Rules for WooCommerce - Version 1.7.10

Version Description

  • 13/02/19 =
  • Fix - Warning: Invalid argument supplied for foreach()
Download this release

Release Info

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

Code changes from version 1.7.9 to 1.7.10

includes/cart-rules.php CHANGED
@@ -880,10 +880,12 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
880
  $rules = (is_string($rules) ? json_decode($rules, true) : array());
881
  // Simple array helper to re-arrange the structure.
882
  FlycartWooDiscountRulesGeneralHelper::reArrangeArray($rules);
883
- foreach ($rules as $index => $rule) {
884
- // Validating the Rules one by one.
885
- if ($this->applyRule($index, $rule, $rules) == false) {
886
- return false;
 
 
887
  }
888
  }
889
  return true;
@@ -975,9 +977,11 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
975
  break;
976
  case ($index == 'customer_email_tld' || $index == 'customer_email_domain'):
977
  $rule = explode(',', $rule);
978
- foreach($rule as $key => $r){
979
- $rule[$key] = trim($r);
980
- $rule[$key] = trim($rule[$key], '.');
 
 
981
  }
982
  $postData = $this->postData->get('post_data', '', 'raw');
983
  $postDataArray = array();
@@ -1036,8 +1040,10 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1036
 
1037
  case 'customer_billing_city':
1038
  $rule = explode(',', $rule);
1039
- foreach($rule as $key => $r){
1040
- $rule[$key] = strtolower(trim($r));
 
 
1041
  }
1042
  $postData = $this->postData->get('post_data', '', 'raw');
1043
  $postDataArray = array();
@@ -1072,8 +1078,10 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1072
  break;
1073
  case 'customer_shipping_state':
1074
  $rule = explode(',', $rule);
1075
- foreach($rule as $key => $r){
1076
- $rule[$key] = strtolower(trim($r));
 
 
1077
  }
1078
  $postData = $this->postData->get('post_data', '', 'raw');
1079
  $postDataArray = array();
@@ -1113,8 +1121,10 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1113
  break;
1114
  case 'customer_shipping_city':
1115
  $rule = explode(',', $rule);
1116
- foreach($rule as $key => $r){
1117
- $rule[$key] = strtolower(trim($r));
 
 
1118
  }
1119
  $postData = $this->postData->get('post_data', '', 'raw');
1120
  $postDataArray = array();
@@ -1154,8 +1164,10 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1154
  break;
1155
  case 'customer_shipping_zip_code':
1156
  $rule = explode(',', $rule);
1157
- foreach($rule as $key => $r){
1158
- $rule[$key] = strtolower(trim($r));
 
 
1159
  }
1160
  $postData = $this->postData->get('post_data', '', 'raw');
1161
  $postDataArray = array();
@@ -1283,10 +1295,12 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1283
  } else {
1284
  return 0;
1285
  }
1286
- foreach ($coupons as $coupon){
1287
- if($woocommerce->cart->has_discount($coupon)){
1288
- $allowed = 1;
1289
- break;
 
 
1290
  }
1291
  }
1292
 
@@ -1306,12 +1320,14 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1306
  } else {
1307
  return 0;
1308
  }
1309
- foreach ($coupons as $coupon){
1310
- if(!$woocommerce->cart->has_discount($coupon)){
1311
- $allowed = 0;
1312
- break;
1313
- } else {
1314
- $allowed = 1;
 
 
1315
  }
1316
  }
1317
 
@@ -1424,33 +1440,35 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1424
  }
1425
  if($categoryFound){
1426
  $ruleSuccess = 1;
1427
- foreach ($rules as $rule_type => $rule_values){
1428
- $checkRuleTypes = array('quantity_least', 'quantity_less', 'subtotal_least', 'subtotal_less', 'item_count_least', 'item_count_less');
1429
- if(in_array($rule_type, $checkRuleTypes)){
1430
- if($rule_type == 'subtotal_least'){
1431
- if ($sub_total < $rule_values) {
1432
- $ruleSuccess = 0;
1433
- }
1434
- } elseif ($rule_type == 'subtotal_less'){
 
1435
 
1436
- if ($sub_total >= $rule_values) {
1437
- $ruleSuccess = 0;
1438
- }
1439
- } elseif ($rule_type == 'item_count_least'){
1440
- if ($item_count < $rule_values) {
1441
- $ruleSuccess = 0;
1442
- }
1443
- } elseif ($rule_type == 'item_count_less'){
1444
- if ($item_count >= $rule_values) {
1445
- $ruleSuccess = 0;
1446
- }
1447
- } elseif ($rule_type == 'quantity_least'){
1448
- if ($quantity < $rule_values) {
1449
- $ruleSuccess = 0;
1450
- }
1451
- } elseif ($rule_type == 'quantity_less'){
1452
- if ($quantity >= $rule_values) {
1453
- $ruleSuccess = 0;
 
1454
  }
1455
  }
1456
  }
@@ -1521,9 +1539,11 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1521
 
1522
  $cart_subtotal = 0;
1523
  // Iterate over all cart items and
1524
- foreach ($this->cart_items as $cart_item_key => $cart_item) {
1525
- $quantity = (isset($cart_item['quantity']) && $cart_item['quantity']) ? $cart_item['quantity'] : 1;
1526
- $cart_subtotal += ((float)FlycartWoocommerceProduct::get_price($cart_item['data'], true)) * $quantity;
 
 
1527
  }
1528
 
1529
  $this->sub_total = (float)$cart_subtotal;
@@ -1534,15 +1554,17 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1534
 
1535
  $cart_subtotal = 0;
1536
  // Iterate over all cart items and
1537
- foreach ($this->cart_items as $cart_item_key => $cart_item) {
 
1538
 
1539
- if($this->does_item_belong_to_category($conditions, $cart_item['data'])) {
1540
 
1541
- //total should be specific to the products from certan categories
1542
- $quantity = (isset($cart_item['quantity']) && $cart_item['quantity']) ? $cart_item['quantity'] : 1;
1543
- $cart_subtotal += FlycartWoocommerceProduct::get_price($cart_item['data'], true) * $quantity;
1544
- }
1545
 
 
1546
  }
1547
 
1548
  return (float)$cart_subtotal;
@@ -1568,10 +1590,11 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1568
  global $woocommerce;
1569
  $cart_items = $woocommerce->cart->cart_contents;
1570
  $total_quantity = 0;
1571
-
1572
- foreach ($cart_items as $cart_item) {
1573
- $current_quantity = (isset($cart_item['quantity']) && $cart_item['quantity']) ? $cart_item['quantity'] : 1;
1574
- $total_quantity += $current_quantity;
 
1575
  }
1576
  return $total_quantity;
1577
  }
@@ -1621,31 +1644,33 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1621
  $product_id = FlycartWoocommerceProduct::get_id($cart_item['data']);
1622
  $added_products[$product_id] = array('item_name'=> FlycartWoocommerceProduct::get_name($cart_item['data']), 'item_quantity' => $cart_item['quantity'], 'item' => $cart_item_key, 'item_price' => FlycartWoocommerceProduct::get_price($cart_item['data'], true));
1623
  }
1624
- foreach ($product_ids as $discounted_product_id) {
1625
- $discounted_price=0;
1626
- //Check the discounted product already found in cart
1627
- if (array_key_exists($discounted_product_id, $added_products)) {
1628
- $old_quantity = isset($added_products[$discounted_product_id]['item_quantity']) ? $added_products[$discounted_product_id]['item_quantity'] : 0;
1629
- if ($old_quantity < $discount_quantity) {
1630
- if (isset($added_products[$discounted_product_id]['item']) && !empty($added_products[$discounted_product_id]['item'])) {
1631
- FlycartWoocommerceCart::set_quantity($added_products[$discounted_product_id]['item'], $discount_quantity);
1632
- }
1633
- }
1634
- $discounted_price = ($discount_quantity * $added_products[$discounted_product_id]['item_price']);
1635
- $coupon_msg = str_replace(array('{{product_name}}','{{quantity}}'),array($added_products[$discounted_product_id]['item_name'],$discount_quantity),$rule_text);
1636
- $this->bogo_coupon_codes[wc_strtolower($coupon_msg)] = array('product_id'=>$discounted_product_id,'amount'=>$discounted_price);
1637
- } else {
1638
- //If product not in cart,then add to cart
1639
- $product = FlycartWoocommerceProduct::wc_get_product($discounted_product_id);
1640
- if($product) {
1641
- FlycartWoocommerceCart::add_to_cart($discounted_product_id, $discount_quantity);
1642
- do_action('woo_discount_rules_cart_rules_after_adding_free_product_to_cart');
1643
- $discounted_price = ($discount_quantity * FlycartWoocommerceProduct::get_price($product, true));
1644
- $coupon_msg = str_replace(array('{{product_name}}', '{{quantity}}'), array(FlycartWoocommerceProduct::get_name($product, true), $discount_quantity), $rule_text);
1645
  $this->bogo_coupon_codes[wc_strtolower($coupon_msg)] = array('product_id'=>$discounted_product_id,'amount'=>$discounted_price);
 
 
 
 
 
 
 
 
 
 
1646
  }
 
1647
  }
1648
- $this->product_discount_total += $discounted_price;
1649
  }
1650
  return true;
1651
  }
@@ -1661,10 +1686,12 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1661
  $rules = (is_string($conditions) ? json_decode($conditions, true) : array());
1662
  // Simple array helper to re-arrange the structure.
1663
  FlycartWooDiscountRulesGeneralHelper::reArrangeArray($rules);
1664
- foreach ($rules as $index => $rule) {
1665
- // Validating the Rules one by one.
1666
- if ($this->applyCartBOGORule($index, $rule, $rules,$rule_set) == false) {
1667
- return false;
 
 
1668
  }
1669
  }
1670
  return true;
@@ -1684,23 +1711,27 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1684
  $free_line_item = 0;
1685
  $free_quantity = 0;
1686
  $free_item_price = 0;
1687
- foreach ($this->cart_items as $cart_items){
1688
- $product_id = FlycartWoocommerceProduct::get_id($cart_items['data']);
1689
- $cart[$product_id]['quantity'] = $cart_items['quantity'];
1690
- $cart[$product_id]['price'] = FlycartWoocommerceProduct::get_price($cart_items['data'], true);
1691
- $cart[$product_id]['subtotal'] = $cart[$product_id]['price'] * $cart[$product_id]['quantity'];
 
 
1692
  }
1693
  $discounted_products = (isset($rule_set->cart_discounted_products)) ? $rule_set->cart_discounted_products : '[]';
1694
  $products = json_decode($discounted_products);
1695
  $rule_discount_quantity = (isset($rule_set->product_discount_quantity)) ? $rule_set->product_discount_quantity : 0;
1696
- foreach ($products as $discounted_product_id) {
1697
- if(array_key_exists($discounted_product_id,$cart))
1698
- {
1699
- $free_line_item += 1;
1700
- $free_quantity += $rule_discount_quantity;
1701
- $product = FlycartWoocommerceProduct::wc_get_product($discounted_product_id);
1702
- $product_price = FlycartWoocommerceProduct::get_price($product, true);
1703
- $free_item_price += $product_price;
 
 
1704
  }
1705
  }
1706
  $cart_quantity_except_free = array_sum(array_column($cart,'quantity')) - $free_quantity;
@@ -1783,9 +1814,11 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1783
  break;
1784
  case ($index == 'customer_email_tld' || $index == 'customer_email_domain'):
1785
  $rule = explode(',', $rule);
1786
- foreach($rule as $key => $r){
1787
- $rule[$key] = trim($r);
1788
- $rule[$key] = trim($rule[$key], '.');
 
 
1789
  }
1790
  $postData = $this->postData->get('post_data', '', 'raw');
1791
  $postDataArray = array();
@@ -1844,8 +1877,10 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1844
 
1845
  case 'customer_billing_city':
1846
  $rule = explode(',', $rule);
1847
- foreach($rule as $key => $r){
1848
- $rule[$key] = strtolower(trim($r));
 
 
1849
  }
1850
  $postData = $this->postData->get('post_data', '', 'raw');
1851
  $postDataArray = array();
@@ -1880,8 +1915,10 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1880
  break;
1881
  case 'customer_shipping_state':
1882
  $rule = explode(',', $rule);
1883
- foreach($rule as $key => $r){
1884
- $rule[$key] = strtolower(trim($r));
 
 
1885
  }
1886
  $postData = $this->postData->get('post_data', '', 'raw');
1887
  $postDataArray = array();
@@ -1921,8 +1958,10 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1921
  break;
1922
  case 'customer_shipping_city':
1923
  $rule = explode(',', $rule);
1924
- foreach($rule as $key => $r){
1925
- $rule[$key] = strtolower(trim($r));
 
 
1926
  }
1927
  $postData = $this->postData->get('post_data', '', 'raw');
1928
  $postDataArray = array();
@@ -1962,8 +2001,10 @@ if (!class_exists('FlycartWooDiscountRulesCartRules')) {
1962
  break;
1963
  case 'customer_shipping_zip_code':
1964
  $rule = explode(',', $rule);
1965
- foreach($rule as $key => $r){
1966
- $rule[$key] = strtolower(trim($r));
 
 
1967
  }
1968
  $postData = $this->postData->get('post_data', '', 'raw');
1969
  $postDataArray = array();
880
  $rules = (is_string($rules) ? json_decode($rules, true) : array());
881
  // Simple array helper to re-arrange the structure.
882
  FlycartWooDiscountRulesGeneralHelper::reArrangeArray($rules);
883
+ if(is_array($rules) && count($rules)){
884
+ foreach ($rules as $index => $rule) {
885
+ // Validating the Rules one by one.
886
+ if ($this->applyRule($index, $rule, $rules) == false) {
887
+ return false;
888
+ }
889
  }
890
  }
891
  return true;
977
  break;
978
  case ($index == 'customer_email_tld' || $index == 'customer_email_domain'):
979
  $rule = explode(',', $rule);
980
+ if(is_array($rule) && count($rule)){
981
+ foreach($rule as $key => $r){
982
+ $rule[$key] = trim($r);
983
+ $rule[$key] = trim($rule[$key], '.');
984
+ }
985
  }
986
  $postData = $this->postData->get('post_data', '', 'raw');
987
  $postDataArray = array();
1040
 
1041
  case 'customer_billing_city':
1042
  $rule = explode(',', $rule);
1043
+ if(is_array($rule) && count($rule)){
1044
+ foreach($rule as $key => $r){
1045
+ $rule[$key] = strtolower(trim($r));
1046
+ }
1047
  }
1048
  $postData = $this->postData->get('post_data', '', 'raw');
1049
  $postDataArray = array();
1078
  break;
1079
  case 'customer_shipping_state':
1080
  $rule = explode(',', $rule);
1081
+ if(is_array($rule) && count($rule)){
1082
+ foreach($rule as $key => $r){
1083
+ $rule[$key] = strtolower(trim($r));
1084
+ }
1085
  }
1086
  $postData = $this->postData->get('post_data', '', 'raw');
1087
  $postDataArray = array();
1121
  break;
1122
  case 'customer_shipping_city':
1123
  $rule = explode(',', $rule);
1124
+ if(is_array($rule) && count($rule)){
1125
+ foreach($rule as $key => $r){
1126
+ $rule[$key] = strtolower(trim($r));
1127
+ }
1128
  }
1129
  $postData = $this->postData->get('post_data', '', 'raw');
1130
  $postDataArray = array();
1164
  break;
1165
  case 'customer_shipping_zip_code':
1166
  $rule = explode(',', $rule);
1167
+ if(is_array($rule) && count($rule)){
1168
+ foreach($rule as $key => $r){
1169
+ $rule[$key] = strtolower(trim($r));
1170
+ }
1171
  }
1172
  $postData = $this->postData->get('post_data', '', 'raw');
1173
  $postDataArray = array();
1295
  } else {
1296
  return 0;
1297
  }
1298
+ if(is_array($coupons) && count($coupons)){
1299
+ foreach ($coupons as $coupon){
1300
+ if($woocommerce->cart->has_discount($coupon)){
1301
+ $allowed = 1;
1302
+ break;
1303
+ }
1304
  }
1305
  }
1306
 
1320
  } else {
1321
  return 0;
1322
  }
1323
+ if(is_array($coupons) && count($coupons)){
1324
+ foreach ($coupons as $coupon){
1325
+ if(!$woocommerce->cart->has_discount($coupon)){
1326
+ $allowed = 0;
1327
+ break;
1328
+ } else {
1329
+ $allowed = 1;
1330
+ }
1331
  }
1332
  }
1333
 
1440
  }
1441
  if($categoryFound){
1442
  $ruleSuccess = 1;
1443
+ if(is_array($rules) && count($rules)){
1444
+ foreach ($rules as $rule_type => $rule_values){
1445
+ $checkRuleTypes = array('quantity_least', 'quantity_less', 'subtotal_least', 'subtotal_less', 'item_count_least', 'item_count_less');
1446
+ if(in_array($rule_type, $checkRuleTypes)){
1447
+ if($rule_type == 'subtotal_least'){
1448
+ if ($sub_total < $rule_values) {
1449
+ $ruleSuccess = 0;
1450
+ }
1451
+ } elseif ($rule_type == 'subtotal_less'){
1452
 
1453
+ if ($sub_total >= $rule_values) {
1454
+ $ruleSuccess = 0;
1455
+ }
1456
+ } elseif ($rule_type == 'item_count_least'){
1457
+ if ($item_count < $rule_values) {
1458
+ $ruleSuccess = 0;
1459
+ }
1460
+ } elseif ($rule_type == 'item_count_less'){
1461
+ if ($item_count >= $rule_values) {
1462
+ $ruleSuccess = 0;
1463
+ }
1464
+ } elseif ($rule_type == 'quantity_least'){
1465
+ if ($quantity < $rule_values) {
1466
+ $ruleSuccess = 0;
1467
+ }
1468
+ } elseif ($rule_type == 'quantity_less'){
1469
+ if ($quantity >= $rule_values) {
1470
+ $ruleSuccess = 0;
1471
+ }
1472
  }
1473
  }
1474
  }
1539
 
1540
  $cart_subtotal = 0;
1541
  // Iterate over all cart items and
1542
+ if(is_array($this->cart_items) && count($this->cart_items)){
1543
+ foreach ($this->cart_items as $cart_item_key => $cart_item) {
1544
+ $quantity = (isset($cart_item['quantity']) && $cart_item['quantity']) ? $cart_item['quantity'] : 1;
1545
+ $cart_subtotal += ((float)FlycartWoocommerceProduct::get_price($cart_item['data'], true)) * $quantity;
1546
+ }
1547
  }
1548
 
1549
  $this->sub_total = (float)$cart_subtotal;
1554
 
1555
  $cart_subtotal = 0;
1556
  // Iterate over all cart items and
1557
+ if(is_array($this->cart_items) && count($this->cart_items)){
1558
+ foreach ($this->cart_items as $cart_item_key => $cart_item) {
1559
 
1560
+ if($this->does_item_belong_to_category($conditions, $cart_item['data'])) {
1561
 
1562
+ //total should be specific to the products from certan categories
1563
+ $quantity = (isset($cart_item['quantity']) && $cart_item['quantity']) ? $cart_item['quantity'] : 1;
1564
+ $cart_subtotal += FlycartWoocommerceProduct::get_price($cart_item['data'], true) * $quantity;
1565
+ }
1566
 
1567
+ }
1568
  }
1569
 
1570
  return (float)$cart_subtotal;
1590
  global $woocommerce;
1591
  $cart_items = $woocommerce->cart->cart_contents;
1592
  $total_quantity = 0;
1593
+ if(is_array($cart_items) && count($cart_items)){
1594
+ foreach ($cart_items as $cart_item) {
1595
+ $current_quantity = (isset($cart_item['quantity']) && $cart_item['quantity']) ? $cart_item['quantity'] : 1;
1596
+ $total_quantity += $current_quantity;
1597
+ }
1598
  }
1599
  return $total_quantity;
1600
  }
1644
  $product_id = FlycartWoocommerceProduct::get_id($cart_item['data']);
1645
  $added_products[$product_id] = array('item_name'=> FlycartWoocommerceProduct::get_name($cart_item['data']), 'item_quantity' => $cart_item['quantity'], 'item' => $cart_item_key, 'item_price' => FlycartWoocommerceProduct::get_price($cart_item['data'], true));
1646
  }
1647
+ if(is_array($product_ids) && count($product_ids)){
1648
+ foreach ($product_ids as $discounted_product_id) {
1649
+ $discounted_price=0;
1650
+ //Check the discounted product already found in cart
1651
+ if (array_key_exists($discounted_product_id, $added_products)) {
1652
+ $old_quantity = isset($added_products[$discounted_product_id]['item_quantity']) ? $added_products[$discounted_product_id]['item_quantity'] : 0;
1653
+ if ($old_quantity < $discount_quantity) {
1654
+ if (isset($added_products[$discounted_product_id]['item']) && !empty($added_products[$discounted_product_id]['item'])) {
1655
+ FlycartWoocommerceCart::set_quantity($added_products[$discounted_product_id]['item'], $discount_quantity);
1656
+ }
1657
+ }
1658
+ $discounted_price = ($discount_quantity * $added_products[$discounted_product_id]['item_price']);
1659
+ $coupon_msg = str_replace(array('{{product_name}}','{{quantity}}'),array($added_products[$discounted_product_id]['item_name'],$discount_quantity),$rule_text);
 
 
 
 
 
 
 
 
1660
  $this->bogo_coupon_codes[wc_strtolower($coupon_msg)] = array('product_id'=>$discounted_product_id,'amount'=>$discounted_price);
1661
+ } else {
1662
+ //If product not in cart,then add to cart
1663
+ $product = FlycartWoocommerceProduct::wc_get_product($discounted_product_id);
1664
+ if($product) {
1665
+ FlycartWoocommerceCart::add_to_cart($discounted_product_id, $discount_quantity);
1666
+ do_action('woo_discount_rules_cart_rules_after_adding_free_product_to_cart');
1667
+ $discounted_price = ($discount_quantity * FlycartWoocommerceProduct::get_price($product, true));
1668
+ $coupon_msg = str_replace(array('{{product_name}}', '{{quantity}}'), array(FlycartWoocommerceProduct::get_name($product, true), $discount_quantity), $rule_text);
1669
+ $this->bogo_coupon_codes[wc_strtolower($coupon_msg)] = array('product_id'=>$discounted_product_id,'amount'=>$discounted_price);
1670
+ }
1671
  }
1672
+ $this->product_discount_total += $discounted_price;
1673
  }
 
1674
  }
1675
  return true;
1676
  }
1686
  $rules = (is_string($conditions) ? json_decode($conditions, true) : array());
1687
  // Simple array helper to re-arrange the structure.
1688
  FlycartWooDiscountRulesGeneralHelper::reArrangeArray($rules);
1689
+ if(is_array($rules) && count($rules)){
1690
+ foreach ($rules as $index => $rule) {
1691
+ // Validating the Rules one by one.
1692
+ if ($this->applyCartBOGORule($index, $rule, $rules,$rule_set) == false) {
1693
+ return false;
1694
+ }
1695
  }
1696
  }
1697
  return true;
1711
  $free_line_item = 0;
1712
  $free_quantity = 0;
1713
  $free_item_price = 0;
1714
+ if(is_array($this->cart_items) && count($this->cart_items)){
1715
+ foreach ($this->cart_items as $cart_items){
1716
+ $product_id = FlycartWoocommerceProduct::get_id($cart_items['data']);
1717
+ $cart[$product_id]['quantity'] = $cart_items['quantity'];
1718
+ $cart[$product_id]['price'] = FlycartWoocommerceProduct::get_price($cart_items['data'], true);
1719
+ $cart[$product_id]['subtotal'] = $cart[$product_id]['price'] * $cart[$product_id]['quantity'];
1720
+ }
1721
  }
1722
  $discounted_products = (isset($rule_set->cart_discounted_products)) ? $rule_set->cart_discounted_products : '[]';
1723
  $products = json_decode($discounted_products);
1724
  $rule_discount_quantity = (isset($rule_set->product_discount_quantity)) ? $rule_set->product_discount_quantity : 0;
1725
+ if(is_array($products) && count($products)){
1726
+ foreach ($products as $discounted_product_id) {
1727
+ if(array_key_exists($discounted_product_id,$cart))
1728
+ {
1729
+ $free_line_item += 1;
1730
+ $free_quantity += $rule_discount_quantity;
1731
+ $product = FlycartWoocommerceProduct::wc_get_product($discounted_product_id);
1732
+ $product_price = FlycartWoocommerceProduct::get_price($product, true);
1733
+ $free_item_price += $product_price;
1734
+ }
1735
  }
1736
  }
1737
  $cart_quantity_except_free = array_sum(array_column($cart,'quantity')) - $free_quantity;
1814
  break;
1815
  case ($index == 'customer_email_tld' || $index == 'customer_email_domain'):
1816
  $rule = explode(',', $rule);
1817
+ if(is_array($rule) && count($rule)){
1818
+ foreach($rule as $key => $r){
1819
+ $rule[$key] = trim($r);
1820
+ $rule[$key] = trim($rule[$key], '.');
1821
+ }
1822
  }
1823
  $postData = $this->postData->get('post_data', '', 'raw');
1824
  $postDataArray = array();
1877
 
1878
  case 'customer_billing_city':
1879
  $rule = explode(',', $rule);
1880
+ if(is_array($rule) && count($rule)){
1881
+ foreach($rule as $key => $r){
1882
+ $rule[$key] = strtolower(trim($r));
1883
+ }
1884
  }
1885
  $postData = $this->postData->get('post_data', '', 'raw');
1886
  $postDataArray = array();
1915
  break;
1916
  case 'customer_shipping_state':
1917
  $rule = explode(',', $rule);
1918
+ if(is_array($rule) && count($rule)){
1919
+ foreach($rule as $key => $r){
1920
+ $rule[$key] = strtolower(trim($r));
1921
+ }
1922
  }
1923
  $postData = $this->postData->get('post_data', '', 'raw');
1924
  $postDataArray = array();
1958
  break;
1959
  case 'customer_shipping_city':
1960
  $rule = explode(',', $rule);
1961
+ if(is_array($rule) && count($rule)){
1962
+ foreach($rule as $key => $r){
1963
+ $rule[$key] = strtolower(trim($r));
1964
+ }
1965
  }
1966
  $postData = $this->postData->get('post_data', '', 'raw');
1967
  $postDataArray = array();
2001
  break;
2002
  case 'customer_shipping_zip_code':
2003
  $rule = explode(',', $rule);
2004
+ if(is_array($rule) && count($rule)){
2005
+ foreach($rule as $key => $r){
2006
+ $rule[$key] = strtolower(trim($r));
2007
+ }
2008
  }
2009
  $postData = $this->postData->get('post_data', '', 'raw');
2010
  $postDataArray = array();
includes/discount-base.php CHANGED
@@ -379,21 +379,23 @@ if (!class_exists('FlycartWooDiscountBase')) {
379
  // Here, Eliminating the Cart's rule with duplicates.
380
  $discount_rule = (isset($cart_rules['discount_rule']) ? $cart_rules['discount_rule'] : array());
381
  if ($isCartRules) {
382
- foreach ($discount_rule as $index => $value) {
 
383
 
384
- // The Type of Option should get value from it's native index.
385
- // $link[$value['type']] will gives the native index of the "type"
386
 
387
- if (isset($link[$value['type']])) {
388
- if(is_array($link[$value['type']])){
389
- foreach ($link[$value['type']] as $fields){
390
- $discount_list[$index][$value['type']][$fields] = $value[$fields];
 
 
 
391
  }
392
- } else if (isset($value[$link[$value['type']]])) {
393
- $discount_list[$index][$value['type']] = $value[$link[$value['type']]];
394
  }
395
- } else {
396
- $discount_list[$index][$value['type']] = $value['option_value'];
397
  }
398
  }
399
  }
379
  // Here, Eliminating the Cart's rule with duplicates.
380
  $discount_rule = (isset($cart_rules['discount_rule']) ? $cart_rules['discount_rule'] : array());
381
  if ($isCartRules) {
382
+ if(is_array($discount_rule) && count($discount_rule)){
383
+ foreach ($discount_rule as $index => $value) {
384
 
385
+ // The Type of Option should get value from it's native index.
386
+ // $link[$value['type']] will gives the native index of the "type"
387
 
388
+ if (isset($link[$value['type']])) {
389
+ if(is_array($link[$value['type']])){
390
+ foreach ($link[$value['type']] as $fields){
391
+ $discount_list[$index][$value['type']][$fields] = $value[$fields];
392
+ }
393
+ } else if (isset($value[$link[$value['type']]])) {
394
+ $discount_list[$index][$value['type']] = $value[$link[$value['type']]];
395
  }
396
+ } else {
397
+ $discount_list[$index][$value['type']] = $value['option_value'];
398
  }
 
 
399
  }
400
  }
401
  }
includes/pricing-rules.php CHANGED
@@ -480,28 +480,30 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
480
 
481
  // Start with empty set.
482
  $rule_set = array();
483
-
484
- foreach ($rules as $index => $rule) {
485
- $status = (isset($rule->status) ? $rule->status : false);
486
-
487
- // To Check as Plugin Active - InActive.
488
- if ($status == 'publish') {
489
- $date_from = (isset($rule->date_from) ? $rule->date_from : false);
490
- $date_to = (isset($rule->date_to) ? $rule->date_to : false);
491
- $validateDate = FlycartWooDiscountRulesGeneralHelper::validateDateAndTime($date_from, $date_to);
492
- // Validating Rule with Date of Expiry.
493
- if ($validateDate) {
494
-
495
- // Validating the Rule with its Order ID.
496
- if (isset($rule->rule_order)) {
497
- // If Order ID is '-', then this rule not going to implement.
498
- if ($rule->rule_order !== '-') {
499
- $rule_set[] = $rule;
 
500
  }
501
  }
502
  }
503
  }
504
  }
 
505
  $this->rules = $rule_set;
506
 
507
  // To Order the Rules, based on its order ID.
@@ -627,12 +629,14 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
627
  * */
628
  public function handleBOGODiscountAfterApplyCoupon($coupon_code){
629
  $carts = FlycartWoocommerceCart::get_cart();
630
- foreach ($carts as $cart_item_key => $cart_item){
631
- if(empty($cart_item['data'])){
632
- continue;
 
 
 
 
633
  }
634
- $quantity = $old_quantity = $cart_item['quantity'];
635
- do_action( 'woocommerce_after_cart_item_quantity_update', $cart_item_key, $quantity, $old_quantity, $carts );
636
  }
637
  }
638
 
@@ -656,10 +660,12 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
656
  }
657
  } else {
658
  $carts = FlycartWoocommerceCart::get_cart();
659
- foreach ($carts as $key => $cart_item) {
660
- if($cart_item_key == $key){
661
- $cart_data = $cart_item;
662
- break;
 
 
663
  }
664
  }
665
  }
@@ -972,10 +978,12 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
972
  global $woocommerce;
973
  $allowed = 0;
974
  $coupons = explode(',', $coupons_selected);
975
- foreach ($coupons as $coupon){
976
- if($woocommerce->cart->has_discount($coupon)){
977
- $allowed = 1;
978
- break;
 
 
979
  }
980
  }
981
 
@@ -989,12 +997,14 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
989
  global $woocommerce;
990
  $allowed = 0;
991
  $coupons = explode(',', $coupons_selected);
992
- foreach ($coupons as $coupon){
993
- if(!$woocommerce->cart->has_discount($coupon)){
994
- $allowed = 0;
995
- break;
996
- } else {
997
- $allowed = 1;
 
 
998
  }
999
  }
1000
 
@@ -1275,16 +1285,20 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
1275
  $applyToProducts = $applied_rules[$i]['amount']['product_ids'];
1276
  $applyPercent = $applied_rules[$i]['amount'];
1277
  unset($applied_rules[$i]);
1278
- foreach ($applyToProducts as $key => $productId) {
1279
- $cart = FlycartWoocommerceCart::get_cart();
1280
- foreach ($cart as $cart_item_key => $values) {
1281
- if(empty($values['data'])){
1282
- continue;
1283
- }
1284
- $_product = $values['data'];
1285
- if (FlycartWoocommerceProduct::get_id($_product) == $productId){
1286
- $additionalKeys = array('apply_from' => $item['product_id']);
1287
- $this->matched_sets[$cart_item_key][] = $this->formatRulesToApply($applyPercent, $rule['name'], $cart_item_key, $productId, $id, $additionalKeys);
 
 
 
 
1288
  }
1289
  }
1290
  }
@@ -1297,39 +1311,43 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
1297
  if(!empty($checkRuleMatches)){
1298
  if(class_exists('FlycartWooDiscountRulesPriceProductDependent'))
1299
  $discountInEachProducts = FlycartWooDiscountRulesPriceProductDependent::getDiscountInEachProducts($item, $rule, $checkRuleMatches, $product_page, $index);
1300
- foreach ($checkRuleMatches['apply_to']['products'] as $key => $productId) {
1301
- if($product_page && $productId == $index){
1302
- $additionalKeys = array('apply_from' => $item['product_id']);
1303
- if(isset($discountInEachProducts[$productId]))
1304
- $discount_amount = $discountInEachProducts[$productId];
1305
- else
1306
- $discount_amount = $checkRuleMatches['amount'];
1307
- $applied_rules_new = $this->formatRulesToApply($discount_amount, $rule['name'], $index, $productId, $id, $additionalKeys);
1308
- $this->matched_sets[$index][] = $applied_rules_new;
1309
- } else {
1310
- $cart = FlycartWoocommerceCart::get_cart();
1311
- foreach ($cart as $cart_item_key => $values) {
1312
- if(empty($values['data'])){
1313
- continue;
1314
- }
1315
- $_product = $values['data'];
1316
- if (FlycartWoocommerceProduct::get_id($_product) == $productId){
1317
- $additionalKeys = array('apply_from' => $item['product_id']);
1318
- if(isset($discountInEachProducts[$productId]))
1319
- $discount_amount = $discountInEachProducts[$productId];
1320
- else
1321
- $discount_amount = $checkRuleMatches['amount'];
1322
- $applied_rules_new = $this->formatRulesToApply($discount_amount, $rule['name'], $cart_item_key, $productId, $id, $additionalKeys);
1323
- $alreadyExists = 0;
1324
- if(!empty($this->matched_sets[$cart_item_key])){
1325
- foreach($this->matched_sets[$cart_item_key] as $machedRules){
1326
- if(isset($machedRules['rule_order']) && $machedRules['rule_order'] == $id){
1327
- $alreadyExists = 1;
1328
- break;
 
 
 
 
1329
  }
 
1330
  }
1331
  }
1332
- if(!$alreadyExists) $this->matched_sets[$cart_item_key][] = $applied_rules_new;
1333
  }
1334
  }
1335
  }
@@ -1395,14 +1413,16 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
1395
  }
1396
  }
1397
  if ( sizeof( $cart ) > 0 ) {
1398
- foreach ($product_to_buy as $key => $productId) {
1399
- foreach ($cart as $cart_item_key => $values) {
1400
- if(empty($values['data'])){
1401
- continue;
1402
- }
1403
- $_product = $values['data'];
1404
- if (FlycartWoocommerceProduct::get_id($_product) == $productId){
1405
- $_quantity[$productId] = $values['quantity'];
 
 
1406
  }
1407
  }
1408
  }
@@ -1434,7 +1454,7 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
1434
  global $woocommerce;
1435
  $hasExcludeProduct = $quantity = 0;
1436
  if(!empty($product_to_exclude) && is_array($product_to_exclude) && count($product_to_exclude)) $hasExcludeProduct = 1;
1437
- if(count($woocommerce->cart->cart_contents)){
1438
  foreach ($woocommerce->cart->cart_contents as $cartItem) {
1439
  if(empty($cartItem['data'])){
1440
  continue;
@@ -1481,9 +1501,11 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
1481
 
1482
  if($terms){
1483
  $has = 0;
1484
- foreach ($terms as $term) {
1485
- if(in_array($term, $category)){
1486
- $has = 1;
 
 
1487
  }
1488
  }
1489
  if($has){
@@ -1502,7 +1524,7 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
1502
  global $woocommerce;
1503
  $hasExcludeProduct = $quantity = $alreadyExists = 0;
1504
  if(!empty($product_to_exclude) && is_array($product_to_exclude) && count($product_to_exclude)) $hasExcludeProduct = 1;
1505
- if(count($woocommerce->cart->cart_contents)){
1506
  foreach ($woocommerce->cart->cart_contents as $cartItem) {
1507
  if(empty($cartItem['data'])){
1508
  continue;
@@ -1543,7 +1565,7 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
1543
  global $woocommerce;
1544
  $quantity = 0;
1545
  if($product_page) $quantity++;
1546
- if(count($woocommerce->cart->cart_contents)){
1547
  foreach ($woocommerce->cart->cart_contents as $cartItem) {
1548
  if(empty($cartItem['data'])){
1549
  continue;
@@ -1571,7 +1593,7 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
1571
  $hasExcludeProduct = $quantity = 0;
1572
  if($product_page) $quantity++;
1573
  if(!empty($product_to_exclude) && is_array($product_to_exclude) && count($product_to_exclude)) $hasExcludeProduct = 1;
1574
- if(count($woocommerce->cart->cart_contents)){
1575
  foreach ($woocommerce->cart->cart_contents as $cartItem) {
1576
  if(empty($cartItem['data'])){
1577
  continue;
@@ -1623,97 +1645,100 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
1623
  public function getAdjustmentAmount($item, $quantity, $discount_ranges, $product_page, $bogo, $product_to_exclude = array())
1624
  {
1625
  $adjustment = array();
1626
- foreach($discount_ranges as $discount_range) {
1627
- if (!is_array($discount_range) && !is_object($discount_range)) return false;
1628
- $range = is_array($discount_range) ? (object) $discount_range : $discount_range;
1629
- $min = (isset($range->min_qty) ? $range->min_qty : 0);
1630
- $max = (isset($range->max_qty) ? $range->max_qty : false);
1631
- if($max == 0 || $max == '' || $max == false) $max = 999;
1632
-
1633
- $type = (isset($range->discount_type) ? $range->discount_type : 'price_discount');
1634
-
1635
- if ($max == false) continue;
1636
-
1637
- if ((int)$min <= (int)$quantity && (int)$max >= (int)$quantity) {
1638
- if($type == 'product_discount'){
1639
- $discount_product_option = isset($range->discount_product_option) ? $range->discount_product_option : 'all';
1640
- $productIds = isset($range->discount_product) ? $range->discount_product : array();
1641
- $productIds = FlycartWoocommerceVersion::backwardCompatibilityStringToArray($productIds);
1642
- if($discount_product_option == 'more_than_one_cheapest' || $discount_product_option == 'any_cheapest'){
1643
- $productIds = apply_filters('woo_discount_rule_products_to_include', $productIds, $discount_ranges);
1644
- }
1645
- if($discount_product_option == 'same_product'){
1646
- $productId = FlycartWoocommerceProduct::get_id($item['data']);
1647
- $discount_quantity = isset($range->discount_bogo_qty)? $range->discount_bogo_qty: 1000;
1648
- $productCheapest = $this->getCheapestProductFromCart(array($productId), 0, $discount_quantity, $range, $product_to_exclude);
1649
- if(!empty($productCheapest)){
1650
- $adjustment = array ( 'price_discount' => $productCheapest['percent'], 'product_ids' => array($productCheapest['product']), 'product_cart_item_keys' => array($productCheapest['product_cart_item_key']), 'product_discount_details' => $productCheapest['discount_details']) ;
1651
  }
1652
- } else if($discount_product_option == 'any_cheapest_from_all'){
1653
- $productCheapest = $this->getCheapestProductFromCart($productIds, 1, 1, $range, $product_to_exclude);
1654
- if(!empty($productCheapest)){
1655
- $adjustment = array ( 'price_discount' => $productCheapest['percent'], 'product_ids' => array($productCheapest['product']), 'product_cart_item_keys' => array($productCheapest['product_cart_item_key']), 'product_discount_details' => $productCheapest['discount_details']) ;
1656
- }
1657
- } else if($discount_product_option == 'any_cheapest'){
1658
- $productCheapest = $this->getCheapestProductFromCart($productIds,0, 1, $range, $product_to_exclude);
1659
- if(!empty($productCheapest)){
1660
- $adjustment = array ( 'price_discount' => $productCheapest['percent'], 'product_ids' => array($productCheapest['product']), 'product_cart_item_keys' => array($productCheapest['product_cart_item_key']), 'product_discount_details' => $productCheapest['discount_details']) ;
1661
- }
1662
- } else if($discount_product_option == 'more_than_one_cheapest' || $discount_product_option == 'more_than_one_cheapest_from_all'|| $discount_product_option == 'more_than_one_cheapest_from_cat'){
1663
- $discount_product_items = (isset($range->discount_product_items) ? $range->discount_product_items : 1);
1664
- if($discount_product_items < 1) $discount_product_items = 1;
1665
- $discount_product_qty = (isset($range->discount_product_qty) ? $range->discount_product_qty : 1);
1666
- if($discount_product_qty < 1) $discount_product_qty = 1;
1667
- $productCheapest = $this->getMoreThanOneCheapestProductFromCart($productIds, $discount_product_qty, $discount_product_items, $range, $product_to_exclude);
1668
- if(!empty($productCheapest)){
1669
- $adjustment = $productCheapest ;
1670
- }
1671
- } else {
1672
- //To handle BOGO
1673
- if(!empty($productIds)) {
1674
- $bogo_count = isset($range->discount_bogo_qty)? (int)$range->discount_bogo_qty: 1;
1675
- if($bogo_count < 1) $bogo_count = 1;
1676
- if($bogo){
1677
- $free_product = $this->bogo_matches;
1678
- foreach ($productIds as $productId){
1679
-
1680
- if(isset($free_product[$productId])){
1681
- $free_product[$productId]['count'] = $free_product[$productId]['count']+$bogo_count;
1682
- } else {
1683
- $free_product[$productId]['count'] = $bogo_count;
1684
- $free_product[$productId]['rule_name'] = $range->title;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1685
  }
1686
- }
1687
- $this->bogo_matches = $free_product;
1688
- $adjustment[$type] = $productIds;
1689
- } else {
1690
- $range->discount_product_item_type = 'static';
1691
- $productCheapest = $this->getMoreThanOneCheapestProductFromCart($productIds, $bogo_count, count($productIds), $range, $product_to_exclude);
1692
- if (!empty($productCheapest)) {
1693
- $adjustment = $productCheapest;
1694
  }
1695
  }
1696
- }
1697
 
1698
- }
1699
- } else {
1700
- $adjustment[$type] = (isset($range->to_discount) ? $range->to_discount : 0);
1701
- $product_discount_details = array();
1702
- if($type == 'percentage_discount'){
1703
- $product_discount_details['discount_type'] = 'percent';
1704
- $productPrice = FlycartWoocommerceProduct::get_price($item['data']);
1705
- $discount_price = $productPrice * ($range->to_discount / 100);
1706
  } else {
1707
- $product_discount_details['discount_type'] = 'price_discount';
1708
- $discount_price = $range->to_discount;
 
 
 
 
 
 
 
 
 
 
 
 
1709
  }
1710
- $product_discount_details['discount_value'] = $range->to_discount;
1711
- $product_discount_details['discount_quantity'] = $quantity;
1712
- $product_discount_details['discount_price'] = $discount_price;
1713
- $adjustment['product_discount_details'] = $product_discount_details;
1714
  }
1715
  }
1716
  }
 
1717
  return $adjustment;
1718
  }
1719
 
@@ -1766,9 +1791,11 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
1766
  $cart_item_details = array();
1767
  $cart_item_details['total_items'] = $cart_item_details['total_quantities'] = 0;
1768
  $cart = FlycartWoocommerceCart::get_cart();
1769
- foreach ( $cart as $cart_item ) {
1770
- $cart_item_details['total_items']++;
1771
- $cart_item_details['total_quantities'] += $cart_item['quantity'];
 
 
1772
  }
1773
 
1774
  return $cart_item_details;
@@ -1780,11 +1807,13 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
1780
  protected function getAllProductsFromCart(){
1781
  $products = array();
1782
  $cart = FlycartWoocommerceCart::get_cart();
1783
- foreach ( $cart as $cart_item ) {
1784
- if($cart_item['variation_id'] && $cart_item['variation_id'])
1785
- $products[] = $cart_item['variation_id'];
1786
- else
1787
- $products[] = $cart_item['product_id'];
 
 
1788
  }
1789
  return $products;
1790
  }
@@ -1796,13 +1825,15 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
1796
  $products = array();
1797
  if(!empty($category) && is_array($category)){
1798
  $cart = FlycartWoocommerceCart::get_cart();
1799
- foreach ( $cart as $cart_item ) {
1800
- $result = $this->isItemInCategoryList($category, $cart_item);
1801
- if($result){
1802
- if(isset($cart_item['variation_id']) && $cart_item['variation_id']){
1803
- $products[] = $cart_item['variation_id'];
1804
- } else {
1805
- $products[] = $cart_item['product_id'];
 
 
1806
  }
1807
  }
1808
  }
@@ -1825,49 +1856,51 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
1825
  $check_cheapestProductValue = $cheapestProductValue = -1;
1826
  }
1827
  $cart = FlycartWoocommerceCart::get_cart();
1828
- foreach ($cart as $cart_item_key => $values) {
1829
- if(empty($values['data'])){
1830
- continue;
1831
- }
1832
- //Exclude the bundled products items
1833
- $exclude_bundle_product_item = apply_filters('woo_discount_rules_exclude_woocommerce_bundled_item', true, $values);
1834
- if($exclude_bundle_product_item){
1835
- if(isset($values['bundled_item_id']) && !empty($values['bundled_item_id'])){
1836
  continue;
1837
  }
1838
- }
1839
- $exclude_cart_item = apply_filters('woo_discount_rules_exclude_cart_item_from_discount', false, $values);
1840
- if($exclude_cart_item) continue;
 
 
 
 
 
 
1841
 
1842
- $_product = $values['data'];
1843
- $productId = FlycartWoocommerceProduct::get_id($_product);
1844
- $loadOriginalPrice = apply_filters('woo_discount_rules_load_original_price_on_get_cheapest_product', false);
1845
- if($loadOriginalPrice){
1846
- $_product = FlycartWoocommerceProduct::wc_get_product($productId);
1847
- }
1848
- if(!empty($product_to_exclude) && is_array($product_to_exclude)){
1849
- if(in_array($productId, $product_to_exclude)) continue;
1850
- }
1851
- if(!in_array($productId, $products) && !$all) continue;
1852
- $skip_free_product = apply_filters('woo_discount_rules_skip_discount_for_free_product', false, $values);
1853
- if($skip_free_product){
1854
- $reduce_quantity = apply_filters('woo_discount_rules_reduce_qty_skip_discount_for_free_product', false, $values);
1855
- if($reduce_quantity){
1856
- $discount_quantity -= (int)$reduce_quantity;
 
 
1857
  }
1858
- continue;
1859
- }
1860
 
1861
- if($cheapestProductValue == $check_cheapestProductValue){
1862
- $cheapestProductValue = FlycartWoocommerceProduct::get_price($_product);
1863
- $cheapestProduct = FlycartWoocommerceProduct::get_id($_product);
1864
- $cheapestProductCartItemKey = $cart_item_key;
1865
- $quantity = $values['quantity'];
1866
- } else if($cheapestProductValue > FlycartWoocommerceProduct::get_price($_product)){
1867
- $cheapestProductValue = FlycartWoocommerceProduct::get_price($_product);
1868
- $cheapestProduct = FlycartWoocommerceProduct::get_id($_product);
1869
- $cheapestProductCartItemKey = $cart_item_key;
1870
- $quantity = $values['quantity'];
 
1871
  }
1872
  }
1873
  $product_discount_details = array();
@@ -2070,18 +2103,22 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
2070
  }
2071
  if(!empty($available_attributes)){
2072
  if($all_attr){
2073
- foreach ($attribute_list as $attribute_list_item){
2074
- if(!in_array($attribute_list_item, $available_attributes)){
2075
- $status = false;
2076
- break;
 
 
 
2077
  }
2078
- $status = true;
2079
  }
2080
  } else {
2081
- foreach ($attribute_list as $attribute_list_item){
2082
- if(in_array($attribute_list_item, $available_attributes)){
2083
- $status = true;
2084
- break;
 
 
2085
  }
2086
  }
2087
  }
@@ -2508,9 +2545,11 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
2508
  $childProducts = array();
2509
  if(FlycartWoocommerceVersion::wcVersion('3.1.0')){
2510
  $children = array_filter( array_map( 'wc_get_product', FlycartWoocommerceProduct::get_children($product) ), 'wc_products_array_filter_visible_grouped' );
2511
- foreach ( $children as $child ) {
2512
- if ( '' !== FlycartWoocommerceProduct::get_price($child) ) {
2513
- $childProducts[] = FlycartWoocommerceProduct::get_id($child);
 
 
2514
  }
2515
  }
2516
  } else {
@@ -2527,8 +2566,10 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
2527
  $p_data = $product->get_data();
2528
  if(!empty($p_data['attributes'])){
2529
  $attr = array();
2530
- foreach ($p_data['attributes'] as $key => $value){
2531
- $attr['attribute_'.$key] = $value;
 
 
2532
  }
2533
  $item['variation'] = $attr;
2534
  $item['variation_id'] = $product_id;
@@ -2701,90 +2742,94 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
2701
  private function getDiscountTableContentInHTML($table_data, $data){
2702
  $dataReturn = array();
2703
  $table = $table_data;
2704
- foreach ($table as $index => $item) {
2705
- if ($item) {
2706
- foreach ($item as $id => $value) {
2707
- if(isset($value->rule_method) && $value->rule_method == 'product_based'){
2708
- $title = $value->title;
2709
- $condition = $value->condition;
2710
- if ($value->discount_type == 'percentage_discount') {
2711
- $discount = $value->to_discount.' %';
 
 
 
 
2712
  } else {
2713
- $discount = FlycartWoocommerceProduct::wc_price($value->to_discount);
2714
- }
2715
- } else {
2716
- $title = isset($value->title) ? $value->title : '';
2717
- $min = isset($value->min_qty) ? $value->min_qty : 1;
2718
- if(((int)$min) <= 0) $min = 1;
2719
- $max = isset($value->max_qty) ? $value->max_qty : 0;
2720
- if($max == 0 || $max == '' || $max == false) $max = 999;
2721
- $discount_type = isset($value->discount_type) ? $value->discount_type : 0;
2722
- $to_discount = isset($value->to_discount) ? $value->to_discount : 0;
2723
- $product_discount = isset($value->discount_product) ? $value->discount_product : array();
2724
- $category_discount = isset($value->discount_category) ? $value->discount_category : array();
2725
- $discount_product_option = isset($value->discount_product_option) ? $value->discount_product_option : 'all';
2726
- $discount_product_discount_type = isset($value->discount_product_discount_type) ? $value->discount_product_discount_type : '';
2727
- $discount_product_percent = isset($value->discount_product_percent) ? $value->discount_product_percent : 0;
2728
- $discount_product_item_type = isset($value->discount_product_item_type) ? $value->discount_product_item_type : 'static';
2729
- $discount_product_items = isset($value->discount_product_items) ? $value->discount_product_items : 1;
2730
- $discount_product_qty = isset($value->discount_product_qty) ? $value->discount_product_qty : 1;
2731
- $product_discount = FlycartWoocommerceVersion::backwardCompatibilityStringToArray($product_discount);
2732
- if (isset($base_config['show_discount_title_table'])) {
2733
- }
2734
- $condition = $min .' - ' . $max;
2735
- if ($discount_type == 'product_discount') {
2736
- $htmlProduct = '';
2737
- $htmlProduct .= esc_html__('Get ', 'woo-discount-rules');
2738
- if($discount_product_discount_type == "limited_percent" && $discount_product_percent>0){
2739
- $htmlProduct .= $discount_product_percent.esc_html__('% discount in ', 'woo-discount-rules');
2740
  }
2741
- if($discount_product_option == 'same_product'){
2742
- $htmlProduct .= esc_html__('same product', 'woo-discount-rules');
2743
- } elseif($discount_product_option == 'any_cheapest_from_all'){
2744
- $htmlProduct .= esc_html__('any cheapest one from cart', 'woo-discount-rules');
2745
- } else {
2746
- if($discount_product_option == 'any_cheapest'){
2747
- $htmlProduct .= esc_html__('any cheapest one of ', 'woo-discount-rules');
2748
  }
2749
- if($discount_product_option == 'more_than_one_cheapest' || $discount_product_option == 'more_than_one_cheapest_from_all' || $discount_product_option == 'more_than_one_cheapest_from_cat'){
2750
- if($discount_product_item_type == 'static'){
2751
- $htmlProduct .= $discount_product_qty;
2752
- $htmlProduct .= esc_html__(' quantity of any ', 'woo-discount-rules');
2753
- $htmlProduct .= $discount_product_items;
2754
- $htmlProduct .= esc_html__(' cheapest item ', 'woo-discount-rules');
2755
- } else {
2756
- $htmlProduct .= $discount_product_qty;
2757
- $htmlProduct .= esc_html__(' quantity of cheapest item ', 'woo-discount-rules');
2758
  }
2759
- }
2760
- if($discount_product_option == 'more_than_one_cheapest_from_cat'){
2761
- $htmlProduct .= esc_html__('from the category ', 'woo-discount-rules');
2762
- $htmlCategories = '';
2763
- foreach ($category_discount as $category_id){
2764
- $htmlCategories .= FlycartWoocommerceProduct::get_product_category_by_id($category_id).', ';
 
 
 
 
2765
  }
2766
- $htmlProduct .= trim($htmlCategories, ', ');
2767
- } else if(count($product_discount) && $discount_product_option != 'more_than_one_cheapest_from_all'){
2768
- foreach ($product_discount as $product_id){
2769
- $product = FlycartWoocommerceProduct::wc_get_product($product_id);
2770
- $htmlProduct .= "<a href='".FlycartWoocommerceProduct::get_permalink($product)."'>";
2771
- $htmlProduct .= FlycartWoocommerceProduct::get_title($product);
2772
- $htmlProduct .= "</a>";
2773
- $htmlProduct .= ' ('.FlycartWoocommerceProduct::get_price_html($product).')<br>';
 
 
 
 
 
 
 
 
 
2774
  }
2775
  }
 
 
 
 
 
2776
  }
2777
- $discount = trim($htmlProduct, '<br>');
2778
- } else if ($discount_type == 'percentage_discount') {
2779
- $discount = $to_discount.' %';
2780
- } else {
2781
- $discount = FlycartWoocommerceProduct::wc_price($to_discount);
2782
- }
2783
 
 
 
 
 
2784
  }
2785
- $dataReturn[$index.$id]['title'] = $title;
2786
- $dataReturn[$index.$id]['condition'] = $condition;
2787
- $dataReturn[$index.$id]['discount'] = $discount;
2788
  }
2789
  }
2790
  }
@@ -2818,9 +2863,10 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
2818
  if(self::$rules_applied_already) return false;
2819
  }
2820
  if(!self::$rules_applied_already) self::$rules_applied_already = 1;
2821
-
2822
- foreach ($cart_items as $cart_item_key => $cart_item) {
2823
- $this->applyAdjustment($cart_item, $cart_item_key, $type);
 
2824
  }
2825
  }
2826
 
@@ -2907,22 +2953,24 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
2907
  * To reset the adjustment set if the Product discount adjustment exists
2908
  * */
2909
  protected function resetTheDiscountIfProductDiscountAdjustmentExists($adjustment_sets, $product_id, $cart_item_key){
2910
- foreach ($adjustment_sets as $key => $adjustment_set){
2911
- if(isset($adjustment_set['amount']['product_discount_adjustment']) && !empty($adjustment_set['amount']['product_discount_adjustment'])){
2912
- if(isset($adjustment_set['amount']['product_discount_adjustment'][$product_id])){
2913
- if(isset($adjustment_set['amount']['product_cart_item_keys'])){
2914
- if(!in_array($cart_item_key, $adjustment_set['amount']['product_cart_item_keys'])){
2915
- unset($adjustment_sets[$key]);
2916
- continue;
 
 
2917
  }
 
 
2918
  }
2919
- $adjustment_sets[$key]['amount']['price_discount'] = $adjustment_set['amount']['product_discount_adjustment'][$product_id];
2920
- $adjustment_sets[$key]['amount']['product_ids'] = array($product_id);
2921
  }
2922
- }
2923
- if(isset($adjustment_set['amount']['product_discount_details']) && !empty($adjustment_set['amount']['product_discount_details'])){
2924
- if(isset($adjustment_set['amount']['product_discount_details'][$product_id])){
2925
- $adjustment_sets[$key]['amount']['product_discount_details'] = $adjustment_set['amount']['product_discount_details'][$product_id];
2926
  }
2927
  }
2928
  }
@@ -3020,33 +3068,35 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
3020
  {
3021
  $big = $amount = 0;
3022
  $details = $rule_data = array();
3023
- foreach ($discount_list as $id => $discount_item) {
3024
- $amount_type = (isset($discount_item['amount']['percentage_discount']) ? 'percentage_discount' : 'price_discount');
3025
- if ($amount_type == 'percentage_discount') {
3026
- if (isset($discount_item['amount']['percentage_discount'])) {
3027
- $amount = (($price / 100) * $discount_item['amount']['percentage_discount']);
3028
- }
3029
- } else {
3030
- if (isset($discount_item['amount']['price_discount'])) {
3031
- $amount = $discount_item['amount']['price_discount'];
3032
- if($product_page){
3033
- if(get_option('woocommerce_prices_include_tax', 'no') == 'no'){
3034
- if(get_option('woocommerce_tax_display_shop', 'incl') == 'incl'){
3035
- $amount = FlycartWoocommerceProduct::get_price_including_tax($product, 1, $amount);
 
 
3036
  }
3037
  }
3038
  }
3039
  }
3040
- }
3041
 
3042
- if ($big < $amount) {
3043
- $big = $amount;
3044
- if(isset($discount_item['rule_order'])){
3045
- $rule_data = array($discount_item['rule_order'] => $discount_item['item']);
3046
- } else {
3047
- $rule_data = array();
 
 
3048
  }
3049
- $details = isset($discount_item['amount']['product_discount_details'])? $discount_item['amount']['product_discount_details'] : array();
3050
  }
3051
  }
3052
  if(!empty($details)) $details = array($details);
@@ -3170,11 +3220,13 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
3170
  protected function hasToSplitTheStrikeOutInCart($cart_item, $additional_details){
3171
  $run_multiple_strikeout = true;
3172
  $quantity = $cart_item['quantity'];
3173
- foreach ($additional_details as $detail){
3174
- if(isset($detail['discount_quantity']))
3175
- if($detail['discount_quantity'] >= $quantity){
3176
- $run_multiple_strikeout = false;
3177
- }
 
 
3178
  }
3179
  return $run_multiple_strikeout;
3180
  }
@@ -3219,29 +3271,31 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
3219
  $item_price_first = '<div style="float: left;"><span class="cart_price wdr_cart_strikeout">' . $original_price_to_display . '</span></div>';
3220
  $item_price = '';
3221
  $haslimitedDiscount = 0;
3222
- foreach ($additional_details as $key => $additional_detail){
3223
- if(!empty($additional_detail) && isset($additional_detail['discount_price'])){
3224
- $haslimitedDiscount = 1;
3225
- if(get_option('woocommerce_prices_include_tax', 'no') == 'no'){
3226
- if(get_option('woocommerce_tax_display_cart', 'incl') == 'incl'){
3227
- $additional_detail['discount_price'] = FlycartWoocommerceProduct::get_price_including_tax($cart_item['data'], 1, $additional_detail['discount_price']);
3228
- }
3229
- } else {
3230
- if(get_option('woocommerce_tax_display_cart') == 'excl'){
3231
- $additional_detail['discount_price'] = FlycartWoocommerceProduct::get_price_excluding_tax($cart_item['data'], 1, $additional_detail['discount_price']);
 
 
3232
  }
 
 
 
 
 
 
 
 
 
 
 
3233
  }
3234
- $new_price_to_display = $cart_item['woo_discount']['original_price'] - $additional_detail['discount_price'];
3235
- if($new_price_to_display < 0) $new_price_to_display = 0;
3236
- $new_price_to_display = FlycartWoocommerceProduct::wc_price($new_price_to_display);
3237
- $quantity -= $additional_detail['discount_quantity'];
3238
- $item_price .= '<div style="float: left;">';
3239
- $item_price .= '<span class="cart_price wdr_cart_strikeout"><del>' . $original_price_to_display . '</del> <ins>' . $new_price_to_display . '</ins></span>';
3240
- $item_price .= '</div>';
3241
- $item_price .= '<div style="float: right; padding-left: 1em;">';
3242
- $item_price .= 'x '.$additional_detail['discount_quantity'];
3243
- $item_price .= '</div>';
3244
- $item_price .= '<div style="clear: both;"></div>';
3245
  }
3246
  }
3247
  if($haslimitedDiscount){
@@ -3501,17 +3555,21 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
3501
 
3502
  if(FlycartWoocommerceVersion::wcVersion('3.1.0')){
3503
  $children = array_filter( array_map( 'wc_get_product', FlycartWoocommerceProduct::get_children($product) ), 'wc_products_array_filter_visible_grouped' );
3504
- foreach ( $children as $child ) {
3505
- if ( '' !== FlycartWoocommerceProduct::get_price($child) ) {
3506
- $child_prices[FlycartWoocommerceProduct::get_id($child)] = 'incl' === $tax_display_mode ? FlycartWoocommerceProduct::get_price_including_tax($child) : FlycartWoocommerceProduct::get_price_excluding_tax( $child );
 
 
3507
  }
3508
  }
3509
  } else {
3510
  $children = $product->get_children();
3511
- foreach ( $children as $child_id ) {
3512
- $child = FlycartWoocommerceProduct::wc_get_product($child_id);
3513
- if ( '' !== FlycartWoocommerceProduct::get_price($child) ) {
3514
- $child_prices[FlycartWoocommerceProduct::get_id($child)] = 'incl' === $tax_display_mode ? FlycartWoocommerceProduct::get_price_including_tax($child) : FlycartWoocommerceProduct::get_price_excluding_tax( $child );
 
 
3515
  }
3516
  }
3517
  }
@@ -3567,17 +3625,21 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
3567
 
3568
  if(FlycartWoocommerceVersion::wcVersion('3.1.0')){
3569
  $children = array_filter( array_map( 'wc_get_product', FlycartWoocommerceProduct::get_children($product) ), 'wc_products_array_filter_visible_grouped' );
3570
- foreach ( $children as $child ) {
3571
- if ( '' !== FlycartWoocommerceProduct::get_price($child) ) {
3572
- $child_prices[FlycartWoocommerceProduct::get_id($child)] = 'incl' === $tax_display_mode ? FlycartWoocommerceProduct::get_price_including_tax($child) : FlycartWoocommerceProduct::get_price_excluding_tax( $child );
 
 
3573
  }
3574
  }
3575
  } else {
3576
  $children = $product->get_children();
3577
- foreach ( $children as $child_id ) {
3578
- $child = FlycartWoocommerceProduct::wc_get_product($child_id);
3579
- if ( '' !== FlycartWoocommerceProduct::get_price($child) ) {
3580
- $child_prices[FlycartWoocommerceProduct::get_id($child)] = 'incl' === $tax_display_mode ? FlycartWoocommerceProduct::get_price_including_tax($child) : FlycartWoocommerceProduct::get_price_excluding_tax( $child );
 
 
3581
  }
3582
  }
3583
  }
@@ -3666,39 +3728,43 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
3666
  if($product->is_type(array('variable', 'subscription_variation', 'variable-subscription'))){
3667
  if(FlycartWoocommerceVersion::wcVersion('3.1.0')){
3668
  $children = array_filter( array_map( 'wc_get_product', FlycartWoocommerceProduct::get_children($product) ), 'wc_products_array_filter_visible_grouped' );
3669
- foreach ( $children as $child ) {
3670
- if ( '' !== FlycartWoocommerceProduct::get_price($child) ) {
3671
- $discountPrice = $this->getDiscountPriceForTheProduct($child);
3672
- if($discountPrice > 0){
3673
- $on_sale = true;
3674
- self::$product_on_sale[$product_id] = 1;
3675
- break;
3676
- } else {
3677
- $product_id = FlycartWoocommerceProduct::get_id($child);
3678
- if($this->hasDiscountForProductId($product_id)){
3679
  $on_sale = true;
3680
  self::$product_on_sale[$product_id] = 1;
3681
  break;
 
 
 
 
 
 
 
3682
  }
3683
  }
3684
  }
3685
  }
3686
  } else {
3687
  $children = $product->get_children();
3688
- foreach ( $children as $child_id ) {
3689
- $child = FlycartWoocommerceProduct::wc_get_product($child_id);
3690
- if ( '' !== FlycartWoocommerceProduct::get_price($child) ) {
3691
- $discountPrice = $this->getDiscountPriceForTheProduct($child);
3692
- if($discountPrice > 0){
3693
- $on_sale = true;
3694
- self::$product_on_sale[$product_id] = 1;
3695
- break;
3696
- } else {
3697
- $product_id = FlycartWoocommerceProduct::get_id($child);
3698
- if($this->hasDiscountForProductId($product_id)){
3699
  $on_sale = true;
3700
  self::$product_on_sale[$product_id] = 1;
3701
  break;
 
 
 
 
 
 
 
3702
  }
3703
  }
3704
  }
@@ -3756,8 +3822,10 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
3756
  $p_data = $product->get_data();
3757
  if(!empty($p_data['attributes'])){
3758
  $attr = array();
3759
- foreach ($p_data['attributes'] as $key => $value){
3760
- $attr['attribute_'.$key] = $value;
 
 
3761
  }
3762
  $item['variation'] = $attr;
3763
  $item['variation_id'] = $product_id;
@@ -3794,10 +3862,12 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
3794
  $item['data'] = $product;
3795
  $qty = 1;
3796
  $cart = FlycartWoocommerceCart::get_cart();
3797
- foreach ( $cart as $cart_item ) {
3798
- if($cart_item['product_id'] == $product_id ){
3799
- $qty = $cart_item['quantity'];
3800
- break; // stop the loop if product is found
 
 
3801
  }
3802
  }
3803
  $item['quantity'] = $qty;
@@ -3809,8 +3879,10 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
3809
  $p_data = $product->get_data();
3810
  if(!empty($p_data['attributes'])){
3811
  $attr = array();
3812
- foreach ($p_data['attributes'] as $key => $value){
3813
- $attr['attribute_'.$key] = $value;
 
 
3814
  }
3815
  $item['variation'] = $attr;
3816
  $item['variation_id'] = $product_id;
@@ -3842,10 +3914,12 @@ if (!class_exists('FlycartWooDiscountRulesPricingRules')) {
3842
  protected function getQuantityOfProductInCart($productId){
3843
  $qty = 0;
3844
  $cart = FlycartWoocommerceCart::get_cart();
3845
- foreach ( $cart as $cart_item ) {
3846
- if($cart_item['product_id'] == $productId ){
3847
- $qty = $cart_item['quantity'];
3848
- break; // stop the loop if product is found
 
 
3849
  }
3850
  }
3851
  return $qty;
480
 
481
  // Start with empty set.
482
  $rule_set = array();
483
+ if(is_array($rules) && count($rules)){
484
+ foreach ($rules as $index => $rule) {
485
+ $status = (isset($rule->status) ? $rule->status : false);
486
+
487
+ // To Check as Plugin Active - InActive.
488
+ if ($status == 'publish') {
489
+ $date_from = (isset($rule->date_from) ? $rule->date_from : false);
490
+ $date_to = (isset($rule->date_to) ? $rule->date_to : false);
491
+ $validateDate = FlycartWooDiscountRulesGeneralHelper::validateDateAndTime($date_from, $date_to);
492
+ // Validating Rule with Date of Expiry.
493
+ if ($validateDate) {
494
+
495
+ // Validating the Rule with its Order ID.
496
+ if (isset($rule->rule_order)) {
497
+ // If Order ID is '-', then this rule not going to implement.
498
+ if ($rule->rule_order !== '-') {
499
+ $rule_set[] = $rule;
500
+ }
501
  }
502
  }
503
  }
504
  }
505
  }
506
+
507
  $this->rules = $rule_set;
508
 
509
  // To Order the Rules, based on its order ID.
629
  * */
630
  public function handleBOGODiscountAfterApplyCoupon($coupon_code){
631
  $carts = FlycartWoocommerceCart::get_cart();
632
+ if(is_array($carts) && count($carts)){
633
+ foreach ($carts as $cart_item_key => $cart_item){
634
+ if(empty($cart_item['data'])){
635
+ continue;
636
+ }
637
+ $quantity = $old_quantity = $cart_item['quantity'];
638
+ do_action( 'woocommerce_after_cart_item_quantity_update', $cart_item_key, $quantity, $old_quantity, $carts );
639
  }
 
 
640
  }
641
  }
642
 
660
  }
661
  } else {
662
  $carts = FlycartWoocommerceCart::get_cart();
663
+ if(is_array($carts) && count($carts)){
664
+ foreach ($carts as $key => $cart_item) {
665
+ if($cart_item_key == $key){
666
+ $cart_data = $cart_item;
667
+ break;
668
+ }
669
  }
670
  }
671
  }
978
  global $woocommerce;
979
  $allowed = 0;
980
  $coupons = explode(',', $coupons_selected);
981
+ if(is_array($coupons) && count($coupons)){
982
+ foreach ($coupons as $coupon){
983
+ if($woocommerce->cart->has_discount($coupon)){
984
+ $allowed = 1;
985
+ break;
986
+ }
987
  }
988
  }
989
 
997
  global $woocommerce;
998
  $allowed = 0;
999
  $coupons = explode(',', $coupons_selected);
1000
+ if(is_array($coupons) && count($coupons)){
1001
+ foreach ($coupons as $coupon){
1002
+ if(!$woocommerce->cart->has_discount($coupon)){
1003
+ $allowed = 0;
1004
+ break;
1005
+ } else {
1006
+ $allowed = 1;
1007
+ }
1008
  }
1009
  }
1010
 
1285
  $applyToProducts = $applied_rules[$i]['amount']['product_ids'];
1286
  $applyPercent = $applied_rules[$i]['amount'];
1287
  unset($applied_rules[$i]);
1288
+ if(is_array($applyToProducts) && count($applyToProducts)){
1289
+ foreach ($applyToProducts as $key => $productId) {
1290
+ $cart = FlycartWoocommerceCart::get_cart();
1291
+ if(is_array($cart) && count($cart)){
1292
+ foreach ($cart as $cart_item_key => $values) {
1293
+ if(empty($values['data'])){
1294
+ continue;
1295
+ }
1296
+ $_product = $values['data'];
1297
+ if (FlycartWoocommerceProduct::get_id($_product) == $productId){
1298
+ $additionalKeys = array('apply_from' => $item['product_id']);
1299
+ $this->matched_sets[$cart_item_key][] = $this->formatRulesToApply($applyPercent, $rule['name'], $cart_item_key, $productId, $id, $additionalKeys);
1300
+ }
1301
+ }
1302
  }
1303
  }
1304
  }
1311
  if(!empty($checkRuleMatches)){
1312
  if(class_exists('FlycartWooDiscountRulesPriceProductDependent'))
1313
  $discountInEachProducts = FlycartWooDiscountRulesPriceProductDependent::getDiscountInEachProducts($item, $rule, $checkRuleMatches, $product_page, $index);
1314
+ if(is_array($checkRuleMatches['apply_to']['products']) && count($checkRuleMatches['apply_to']['products'])){
1315
+ foreach ($checkRuleMatches['apply_to']['products'] as $key => $productId) {
1316
+ if($product_page && $productId == $index){
1317
+ $additionalKeys = array('apply_from' => $item['product_id']);
1318
+ if(isset($discountInEachProducts[$productId]))
1319
+ $discount_amount = $discountInEachProducts[$productId];
1320
+ else
1321
+ $discount_amount = $checkRuleMatches['amount'];
1322
+ $applied_rules_new = $this->formatRulesToApply($discount_amount, $rule['name'], $index, $productId, $id, $additionalKeys);
1323
+ $this->matched_sets[$index][] = $applied_rules_new;
1324
+ } else {
1325
+ $cart = FlycartWoocommerceCart::get_cart();
1326
+ if(is_array($cart) && count($cart)){
1327
+ foreach ($cart as $cart_item_key => $values) {
1328
+ if(empty($values['data'])){
1329
+ continue;
1330
+ }
1331
+ $_product = $values['data'];
1332
+ if (FlycartWoocommerceProduct::get_id($_product) == $productId){
1333
+ $additionalKeys = array('apply_from' => $item['product_id']);
1334
+ if(isset($discountInEachProducts[$productId]))
1335
+ $discount_amount = $discountInEachProducts[$productId];
1336
+ else
1337
+ $discount_amount = $checkRuleMatches['amount'];
1338
+ $applied_rules_new = $this->formatRulesToApply($discount_amount, $rule['name'], $cart_item_key, $productId, $id, $additionalKeys);
1339
+ $alreadyExists = 0;
1340
+ if(!empty($this->matched_sets[$cart_item_key])){
1341
+ foreach($this->matched_sets[$cart_item_key] as $machedRules){
1342
+ if(isset($machedRules['rule_order']) && $machedRules['rule_order'] == $id){
1343
+ $alreadyExists = 1;
1344
+ break;
1345
+ }
1346
+ }
1347
  }
1348
+ if(!$alreadyExists) $this->matched_sets[$cart_item_key][] = $applied_rules_new;
1349
  }
1350
  }
 
1351
  }
1352
  }
1353
  }
1413
  }
1414
  }
1415
  if ( sizeof( $cart ) > 0 ) {
1416
+ if(is_array($product_to_buy) && count($product_to_buy)){
1417
+ foreach ($product_to_buy as $key => $productId) {
1418
+ foreach ($cart as $cart_item_key => $values) {
1419
+ if(empty($values['data'])){
1420
+ continue;
1421
+ }
1422
+ $_product = $values['data'];
1423
+ if (FlycartWoocommerceProduct::get_id($_product) == $productId){
1424
+ $_quantity[$productId] = $values['quantity'];
1425
+ }
1426
  }
1427
  }
1428
  }
1454
  global $woocommerce;
1455
  $hasExcludeProduct = $quantity = 0;
1456
  if(!empty($product_to_exclude) && is_array($product_to_exclude) && count($product_to_exclude)) $hasExcludeProduct = 1;
1457
+ if(is_array($woocommerce->cart->cart_contents) && count($woocommerce->cart->cart_contents)){
1458
  foreach ($woocommerce->cart->cart_contents as $cartItem) {
1459
  if(empty($cartItem['data'])){
1460
  continue;
1501
 
1502
  if($terms){
1503
  $has = 0;
1504
+ if(is_array($terms) && count($terms)){
1505
+ foreach ($terms as $term) {
1506
+ if(in_array($term, $category)){
1507
+ $has = 1;
1508
+ }
1509
  }
1510
  }
1511
  if($has){
1524
  global $woocommerce;
1525
  $hasExcludeProduct = $quantity = $alreadyExists = 0;
1526
  if(!empty($product_to_exclude) && is_array($product_to_exclude) && count($product_to_exclude)) $hasExcludeProduct = 1;
1527
+ if(is_array($woocommerce->cart->cart_contents) && count($woocommerce->cart->cart_contents)){
1528
  foreach ($woocommerce->cart->cart_contents as $cartItem) {
1529
  if(empty($cartItem['data'])){
1530
  continue;
1565
  global $woocommerce;
1566
  $quantity = 0;
1567
  if($product_page) $quantity++;
1568
+ if(is_array($woocommerce->cart->cart_contents) && count($woocommerce->cart->cart_contents)){
1569
  foreach ($woocommerce->cart->cart_contents as $cartItem) {
1570
  if(empty($cartItem['data'])){
1571
  continue;
1593
  $hasExcludeProduct = $quantity = 0;
1594
  if($product_page) $quantity++;
1595
  if(!empty($product_to_exclude) && is_array($product_to_exclude) && count($product_to_exclude)) $hasExcludeProduct = 1;
1596
+ if(is_array($woocommerce->cart->cart_contents) && count($woocommerce->cart->cart_contents)){
1597
  foreach ($woocommerce->cart->cart_contents as $cartItem) {
1598
  if(empty($cartItem['data'])){
1599
  continue;
1645
  public function getAdjustmentAmount($item, $quantity, $discount_ranges, $product_page, $bogo, $product_to_exclude = array())
1646
  {
1647
  $adjustment = array();
1648
+ if(is_array($discount_ranges) && count($discount_ranges)){
1649
+ foreach($discount_ranges as $discount_range) {
1650
+ if (!is_array($discount_range) && !is_object($discount_range)) return false;
1651
+ $range = is_array($discount_range) ? (object) $discount_range : $discount_range;
1652
+ $min = (isset($range->min_qty) ? $range->min_qty : 0);
1653
+ $max = (isset($range->max_qty) ? $range->max_qty : false);
1654
+ if($max == 0 || $max == '' || $max == false) $max = 999;
1655
+
1656
+ $type = (isset($range->discount_type) ? $range->discount_type : 'price_discount');
1657
+
1658
+ if ($max == false) continue;
1659
+
1660
+ if ((int)$min <= (int)$quantity && (int)$max >= (int)$quantity) {
1661
+ if($type == 'product_discount'){
1662
+ $discount_product_option = isset($range->discount_product_option) ? $range->discount_product_option : 'all';
1663
+ $productIds = isset($range->discount_product) ? $range->discount_product : array();
1664
+ $productIds = FlycartWoocommerceVersion::backwardCompatibilityStringToArray($productIds);
1665
+ if($discount_product_option == 'more_than_one_cheapest' || $discount_product_option == 'any_cheapest'){
1666
+ $productIds = apply_filters('woo_discount_rule_products_to_include', $productIds, $discount_ranges);
 
 
 
 
 
 
1667
  }
1668
+ if($discount_product_option == 'same_product'){
1669
+ $productId = FlycartWoocommerceProduct::get_id($item['data']);
1670
+ $discount_quantity = isset($range->discount_bogo_qty)? $range->discount_bogo_qty: 1000;
1671
+ $productCheapest = $this->getCheapestProductFromCart(array($productId), 0, $discount_quantity, $range, $product_to_exclude);
1672
+ if(!empty($productCheapest)){
1673
+ $adjustment = array ( 'price_discount' => $productCheapest['percent'], 'product_ids' => array($productCheapest['product']), 'product_cart_item_keys' => array($productCheapest['product_cart_item_key']), 'product_discount_details' => $productCheapest['discount_details']) ;
1674
+ }
1675
+ } else if($discount_product_option == 'any_cheapest_from_all'){
1676
+ $productCheapest = $this->getCheapestProductFromCart($productIds, 1, 1, $range, $product_to_exclude);
1677
+ if(!empty($productCheapest)){
1678
+ $adjustment = array ( 'price_discount' => $productCheapest['percent'], 'product_ids' => array($productCheapest['product']), 'product_cart_item_keys' => array($productCheapest['product_cart_item_key']), 'product_discount_details' => $productCheapest['discount_details']) ;
1679
+ }
1680
+ } else if($discount_product_option == 'any_cheapest'){
1681
+ $productCheapest = $this->getCheapestProductFromCart($productIds,0, 1, $range, $product_to_exclude);
1682
+ if(!empty($productCheapest)){
1683
+ $adjustment = array ( 'price_discount' => $productCheapest['percent'], 'product_ids' => array($productCheapest['product']), 'product_cart_item_keys' => array($productCheapest['product_cart_item_key']), 'product_discount_details' => $productCheapest['discount_details']) ;
1684
+ }
1685
+ } else if($discount_product_option == 'more_than_one_cheapest' || $discount_product_option == 'more_than_one_cheapest_from_all'|| $discount_product_option == 'more_than_one_cheapest_from_cat'){
1686
+ $discount_product_items = (isset($range->discount_product_items) ? $range->discount_product_items : 1);
1687
+ if($discount_product_items < 1) $discount_product_items = 1;
1688
+ $discount_product_qty = (isset($range->discount_product_qty) ? $range->discount_product_qty : 1);
1689
+ if($discount_product_qty < 1) $discount_product_qty = 1;
1690
+ $productCheapest = $this->getMoreThanOneCheapestProductFromCart($productIds, $discount_product_qty, $discount_product_items, $range, $product_to_exclude);
1691
+ if(!empty($productCheapest)){
1692
+ $adjustment = $productCheapest ;
1693
+ }
1694
+ } else {
1695
+ //To handle BOGO
1696
+ if(is_array($productIds) && count($productIds)){
1697
+ $bogo_count = isset($range->discount_bogo_qty)? (int)$range->discount_bogo_qty: 1;
1698
+ if($bogo_count < 1) $bogo_count = 1;
1699
+ if($bogo){
1700
+ $free_product = $this->bogo_matches;
1701
+ foreach ($productIds as $productId){
1702
+
1703
+ if(isset($free_product[$productId])){
1704
+ $free_product[$productId]['count'] = $free_product[$productId]['count']+$bogo_count;
1705
+ } else {
1706
+ $free_product[$productId]['count'] = $bogo_count;
1707
+ $free_product[$productId]['rule_name'] = $range->title;
1708
+ }
1709
+ }
1710
+ $this->bogo_matches = $free_product;
1711
+ $adjustment[$type] = $productIds;
1712
+ } else {
1713
+ $range->discount_product_item_type = 'static';
1714
+ $productCheapest = $this->getMoreThanOneCheapestProductFromCart($productIds, $bogo_count, count($productIds), $range, $product_to_exclude);
1715
+ if (!empty($productCheapest)) {
1716
+ $adjustment = $productCheapest;
1717
  }
 
 
 
 
 
 
 
 
1718
  }
1719
  }
 
1720
 
1721
+ }
 
 
 
 
 
 
 
1722
  } else {
1723
+ $adjustment[$type] = (isset($range->to_discount) ? $range->to_discount : 0);
1724
+ $product_discount_details = array();
1725
+ if($type == 'percentage_discount'){
1726
+ $product_discount_details['discount_type'] = 'percent';
1727
+ $productPrice = FlycartWoocommerceProduct::get_price($item['data']);
1728
+ $discount_price = $productPrice * ($range->to_discount / 100);
1729
+ } else {
1730
+ $product_discount_details['discount_type'] = 'price_discount';
1731
+ $discount_price = $range->to_discount;
1732
+ }
1733
+ $product_discount_details['discount_value'] = $range->to_discount;
1734
+ $product_discount_details['discount_quantity'] = $quantity;
1735
+ $product_discount_details['discount_price'] = $discount_price;
1736
+ $adjustment['product_discount_details'] = $product_discount_details;
1737
  }
 
 
 
 
1738
  }
1739
  }
1740
  }
1741
+
1742
  return $adjustment;
1743
  }
1744
 
1791
  $cart_item_details = array();
1792
  $cart_item_details['total_items'] = $cart_item_details['total_quantities'] = 0;
1793
  $cart = FlycartWoocommerceCart::get_cart();
1794
+ if(is_array($cart) && count($cart)){
1795
+ foreach ( $cart as $cart_item ) {
1796
+ $cart_item_details['total_items']++;
1797
+ $cart_item_details['total_quantities'] += $cart_item['quantity'];
1798
+ }
1799
  }
1800
 
1801
  return $cart_item_details;
1807
  protected function getAllProductsFromCart(){
1808
  $products = array();
1809
  $cart = FlycartWoocommerceCart::get_cart();
1810
+ if(is_array($cart) && count($cart)){
1811
+ foreach ( $cart as $cart_item ) {
1812
+ if($cart_item['variation_id'] && $cart_item['variation_id'])
1813
+ $products[] = $cart_item['variation_id'];
1814
+ else
1815
+ $products[] = $cart_item['product_id'];
1816
+ }
1817
  }
1818
  return $products;
1819
  }
1825
  $products = array();
1826
  if(!empty($category) && is_array($category)){
1827
  $cart = FlycartWoocommerceCart::get_cart();
1828
+ if(is_array($cart) && count($cart)){
1829
+ foreach ( $cart as $cart_item ) {
1830
+ $result = $this->isItemInCategoryList($category, $cart_item);
1831
+ if($result){
1832
+ if(isset($cart_item['variation_id']) && $cart_item['variation_id']){
1833
+ $products[] = $cart_item['variation_id'];
1834
+ } else {
1835
+ $products[] = $cart_item['product_id'];
1836
+ }
1837
  }
1838
  }
1839
  }
1856
  $check_cheapestProductValue = $cheapestProductValue = -1;
1857
  }
1858
  $cart = FlycartWoocommerceCart::get_cart();
1859
+ if(is_array($cart) && count($cart)){
1860
+ foreach ($cart as $cart_item_key => $values) {
1861
+ if(empty($values['data'])){
 
 
 
 
 
1862
  continue;
1863
  }
1864
+ //Exclude the bundled products items
1865
+ $exclude_bundle_product_item = apply_filters('woo_discount_rules_exclude_woocommerce_bundled_item', true, $values);
1866
+ if($exclude_bundle_product_item){
1867
+ if(isset($values['bundled_item_id']) && !empty($values['bundled_item_id'])){
1868
+ continue;
1869
+ }
1870
+ }
1871
+ $exclude_cart_item = apply_filters('woo_discount_rules_exclude_cart_item_from_discount', false, $values);
1872
+ if($exclude_cart_item) continue;
1873
 
1874
+ $_product = $values['data'];
1875
+ $productId = FlycartWoocommerceProduct::get_id($_product);
1876
+ $loadOriginalPrice = apply_filters('woo_discount_rules_load_original_price_on_get_cheapest_product', false);
1877
+ if($loadOriginalPrice){
1878
+ $_product = FlycartWoocommerceProduct::wc_get_product($productId);
1879
+ }
1880
+ if(!empty($product_to_exclude) && is_array($product_to_exclude)){
1881
+ if(in_array($productId, $product_to_exclude)) continue;
1882
+ }
1883
+ if(!in_array($productId, $products) && !$all) continue;
1884
+ $skip_free_product = apply_filters('woo_discount_rules_skip_discount_for_free_product', false, $values);
1885
+ if($skip_free_product){
1886
+ $reduce_quantity = apply_filters('woo_discount_rules_reduce_qty_skip_discount_for_free_product', false, $values);
1887
+ if($reduce_quantity){
1888
+ $discount_quantity -= (int)$reduce_quantity;
1889
+ }
1890
+ continue;
1891
  }
 
 
1892
 
1893
+ if($cheapestProductValue == $check_cheapestProductValue){
1894
+ $cheapestProductValue = FlycartWoocommerceProduct::get_price($_product);
1895
+ $cheapestProduct = FlycartWoocommerceProduct::get_id($_product);
1896
+ $cheapestProductCartItemKey = $cart_item_key;
1897
+ $quantity = $values['quantity'];
1898
+ } else if($cheapestProductValue > FlycartWoocommerceProduct::get_price($_product)){
1899
+ $cheapestProductValue = FlycartWoocommerceProduct::get_price($_product);
1900
+ $cheapestProduct = FlycartWoocommerceProduct::get_id($_product);
1901
+ $cheapestProductCartItemKey = $cart_item_key;
1902
+ $quantity = $values['quantity'];
1903
+ }
1904
  }
1905
  }
1906
  $product_discount_details = array();
2103
  }
2104
  if(!empty($available_attributes)){
2105
  if($all_attr){
2106
+ if(is_array($attribute_list) && count($attribute_list)){
2107
+ foreach ($attribute_list as $attribute_list_item){
2108
+ if(!in_array($attribute_list_item, $available_attributes)){
2109
+ $status = false;
2110
+ break;
2111
+ }
2112
+ $status = true;
2113
  }
 
2114
  }
2115
  } else {
2116
+ if(is_array($attribute_list) && count($attribute_list)){
2117
+ foreach ($attribute_list as $attribute_list_item){
2118
+ if(in_array($attribute_list_item, $available_attributes)){
2119
+ $status = true;
2120
+ break;
2121
+ }
2122
  }
2123
  }
2124
  }
2545
  $childProducts = array();
2546
  if(FlycartWoocommerceVersion::wcVersion('3.1.0')){
2547
  $children = array_filter( array_map( 'wc_get_product', FlycartWoocommerceProduct::get_children($product) ), 'wc_products_array_filter_visible_grouped' );
2548
+ if(is_array($children) && count($children)){
2549
+ foreach ( $children as $child ) {
2550
+ if ( '' !== FlycartWoocommerceProduct::get_price($child) ) {
2551
+ $childProducts[] = FlycartWoocommerceProduct::get_id($child);
2552
+ }
2553
  }
2554
  }
2555
  } else {
2566
  $p_data = $product->get_data();
2567
  if(!empty($p_data['attributes'])){
2568
  $attr = array();
2569
+ if(is_array($p_data['attributes']) && count($p_data['attributes'])){
2570
+ foreach ($p_data['attributes'] as $key => $value){
2571
+ $attr['attribute_'.$key] = $value;
2572
+ }
2573
  }
2574
  $item['variation'] = $attr;
2575
  $item['variation_id'] = $product_id;
2742
  private function getDiscountTableContentInHTML($table_data, $data){
2743
  $dataReturn = array();
2744
  $table = $table_data;
2745
+ if(is_array($table) && count($table)){
2746
+ foreach ($table as $index => $item) {
2747
+ if(is_array($item) && count($item)){
2748
+ foreach ($item as $id => $value) {
2749
+ if(isset($value->rule_method) && $value->rule_method == 'product_based'){
2750
+ $title = $value->title;
2751
+ $condition = $value->condition;
2752
+ if ($value->discount_type == 'percentage_discount') {
2753
+ $discount = $value->to_discount.' %';
2754
+ } else {
2755
+ $discount = FlycartWoocommerceProduct::wc_price($value->to_discount);
2756
+ }
2757
  } else {
2758
+ $title = isset($value->title) ? $value->title : '';
2759
+ $min = isset($value->min_qty) ? $value->min_qty : 1;
2760
+ if(((int)$min) <= 0) $min = 1;
2761
+ $max = isset($value->max_qty) ? $value->max_qty : 0;
2762
+ if($max == 0 || $max == '' || $max == false) $max = 999;
2763
+ $discount_type = isset($value->discount_type) ? $value->discount_type : 0;
2764
+ $to_discount = isset($value->to_discount) ? $value->to_discount : 0;
2765
+ $product_discount = isset($value->discount_product) ? $value->discount_product : array();
2766
+ $category_discount = isset($value->discount_category) ? $value->discount_category : array();
2767
+ $discount_product_option = isset($value->discount_product_option) ? $value->discount_product_option : 'all';
2768
+ $discount_product_discount_type = isset($value->discount_product_discount_type) ? $value->discount_product_discount_type : '';
2769
+ $discount_product_percent = isset($value->discount_product_percent) ? $value->discount_product_percent : 0;
2770
+ $discount_product_item_type = isset($value->discount_product_item_type) ? $value->discount_product_item_type : 'static';
2771
+ $discount_product_items = isset($value->discount_product_items) ? $value->discount_product_items : 1;
2772
+ $discount_product_qty = isset($value->discount_product_qty) ? $value->discount_product_qty : 1;
2773
+ $product_discount = FlycartWoocommerceVersion::backwardCompatibilityStringToArray($product_discount);
2774
+ if (isset($base_config['show_discount_title_table'])) {
 
 
 
 
 
 
 
 
 
 
2775
  }
2776
+ $condition = $min .' - ' . $max;
2777
+ if ($discount_type == 'product_discount') {
2778
+ $htmlProduct = '';
2779
+ $htmlProduct .= esc_html__('Get ', 'woo-discount-rules');
2780
+ if($discount_product_discount_type == "limited_percent" && $discount_product_percent>0){
2781
+ $htmlProduct .= $discount_product_percent.esc_html__('% discount in ', 'woo-discount-rules');
 
2782
  }
2783
+ if($discount_product_option == 'same_product'){
2784
+ $htmlProduct .= esc_html__('same product', 'woo-discount-rules');
2785
+ } elseif($discount_product_option == 'any_cheapest_from_all'){
2786
+ $htmlProduct .= esc_html__('any cheapest one from cart', 'woo-discount-rules');
2787
+ } else {
2788
+ if($discount_product_option == 'any_cheapest'){
2789
+ $htmlProduct .= esc_html__('any cheapest one of ', 'woo-discount-rules');
 
 
2790
  }
2791
+ if($discount_product_option == 'more_than_one_cheapest' || $discount_product_option == 'more_than_one_cheapest_from_all' || $discount_product_option == 'more_than_one_cheapest_from_cat'){
2792
+ if($discount_product_item_type == 'static'){
2793
+ $htmlProduct .= $discount_product_qty;
2794
+ $htmlProduct .= esc_html__(' quantity of any ', 'woo-discount-rules');
2795
+ $htmlProduct .= $discount_product_items;
2796
+ $htmlProduct .= esc_html__(' cheapest item ', 'woo-discount-rules');
2797
+ } else {
2798
+ $htmlProduct .= $discount_product_qty;
2799
+ $htmlProduct .= esc_html__(' quantity of cheapest item ', 'woo-discount-rules');
2800
+ }
2801
  }
2802
+ if($discount_product_option == 'more_than_one_cheapest_from_cat'){
2803
+ $htmlProduct .= esc_html__('from the category ', 'woo-discount-rules');
2804
+ $htmlCategories = '';
2805
+ if(is_array($category_discount) && count($category_discount)){
2806
+ foreach ($category_discount as $category_id){
2807
+ $htmlCategories .= FlycartWoocommerceProduct::get_product_category_by_id($category_id).', ';
2808
+ }
2809
+ }
2810
+ $htmlProduct .= trim($htmlCategories, ', ');
2811
+ } else if(count($product_discount) && $discount_product_option != 'more_than_one_cheapest_from_all'){
2812
+ foreach ($product_discount as $product_id){
2813
+ $product = FlycartWoocommerceProduct::wc_get_product($product_id);
2814
+ $htmlProduct .= "<a href='".FlycartWoocommerceProduct::get_permalink($product)."'>";
2815
+ $htmlProduct .= FlycartWoocommerceProduct::get_title($product);
2816
+ $htmlProduct .= "</a>";
2817
+ $htmlProduct .= ' ('.FlycartWoocommerceProduct::get_price_html($product).')<br>';
2818
+ }
2819
  }
2820
  }
2821
+ $discount = trim($htmlProduct, '<br>');
2822
+ } else if ($discount_type == 'percentage_discount') {
2823
+ $discount = $to_discount.' %';
2824
+ } else {
2825
+ $discount = FlycartWoocommerceProduct::wc_price($to_discount);
2826
  }
 
 
 
 
 
 
2827
 
2828
+ }
2829
+ $dataReturn[$index.$id]['title'] = $title;
2830
+ $dataReturn[$index.$id]['condition'] = $condition;
2831
+ $dataReturn[$index.$id]['discount'] = $discount;
2832
  }
 
 
 
2833
  }
2834
  }
2835
  }
2863
  if(self::$rules_applied_already) return false;
2864
  }
2865
  if(!self::$rules_applied_already) self::$rules_applied_already = 1;
2866
+ if(is_array($cart_items) && count($cart_items)){
2867
+ foreach ($cart_items as $cart_item_key => $cart_item) {
2868
+ $this->applyAdjustment($cart_item, $cart_item_key, $type);
2869
+ }
2870
  }
2871
  }
2872
 
2953
  * To reset the adjustment set if the Product discount adjustment exists
2954
  * */
2955
  protected function resetTheDiscountIfProductDiscountAdjustmentExists($adjustment_sets, $product_id, $cart_item_key){
2956
+ if(is_array($adjustment_sets) && count($adjustment_sets)){
2957
+ foreach ($adjustment_sets as $key => $adjustment_set){
2958
+ if(isset($adjustment_set['amount']['product_discount_adjustment']) && !empty($adjustment_set['amount']['product_discount_adjustment'])){
2959
+ if(isset($adjustment_set['amount']['product_discount_adjustment'][$product_id])){
2960
+ if(isset($adjustment_set['amount']['product_cart_item_keys'])){
2961
+ if(!in_array($cart_item_key, $adjustment_set['amount']['product_cart_item_keys'])){
2962
+ unset($adjustment_sets[$key]);
2963
+ continue;
2964
+ }
2965
  }
2966
+ $adjustment_sets[$key]['amount']['price_discount'] = $adjustment_set['amount']['product_discount_adjustment'][$product_id];
2967
+ $adjustment_sets[$key]['amount']['product_ids'] = array($product_id);
2968
  }
 
 
2969
  }
2970
+ if(isset($adjustment_set['amount']['product_discount_details']) && !empty($adjustment_set['amount']['product_discount_details'])){
2971
+ if(isset($adjustment_set['amount']['product_discount_details'][$product_id])){
2972
+ $adjustment_sets[$key]['amount']['product_discount_details'] = $adjustment_set['amount']['product_discount_details'][$product_id];
2973
+ }
2974
  }
2975
  }
2976
  }
3068
  {
3069
  $big = $amount = 0;
3070
  $details = $rule_data = array();
3071
+ if(is_array($discount_list) && count($discount_list)){
3072
+ foreach ($discount_list as $id => $discount_item) {
3073
+ $amount_type = (isset($discount_item['amount']['percentage_discount']) ? 'percentage_discount' : 'price_discount');
3074
+ if ($amount_type == 'percentage_discount') {
3075
+ if (isset($discount_item['amount']['percentage_discount'])) {
3076
+ $amount = (($price / 100) * $discount_item['amount']['percentage_discount']);
3077
+ }
3078
+ } else {
3079
+ if (isset($discount_item['amount']['price_discount'])) {
3080
+ $amount = $discount_item['amount']['price_discount'];
3081
+ if($product_page){
3082
+ if(get_option('woocommerce_prices_include_tax', 'no') == 'no'){
3083
+ if(get_option('woocommerce_tax_display_shop', 'incl') == 'incl'){
3084
+ $amount = FlycartWoocommerceProduct::get_price_including_tax($product, 1, $amount);
3085
+ }
3086
  }
3087
  }
3088
  }
3089
  }
 
3090
 
3091
+ if ($big < $amount) {
3092
+ $big = $amount;
3093
+ if(isset($discount_item['rule_order'])){
3094
+ $rule_data = array($discount_item['rule_order'] => $discount_item['item']);
3095
+ } else {
3096
+ $rule_data = array();
3097
+ }
3098
+ $details = isset($discount_item['amount']['product_discount_details'])? $discount_item['amount']['product_discount_details'] : array();
3099
  }
 
3100
  }
3101
  }
3102
  if(!empty($details)) $details = array($details);
3220
  protected function hasToSplitTheStrikeOutInCart($cart_item, $additional_details){
3221
  $run_multiple_strikeout = true;
3222
  $quantity = $cart_item['quantity'];
3223
+ if(is_array($additional_details) && count($additional_details)){
3224
+ foreach ($additional_details as $detail){
3225
+ if(isset($detail['discount_quantity']))
3226
+ if($detail['discount_quantity'] >= $quantity){
3227
+ $run_multiple_strikeout = false;
3228
+ }
3229
+ }
3230
  }
3231
  return $run_multiple_strikeout;
3232
  }
3271
  $item_price_first = '<div style="float: left;"><span class="cart_price wdr_cart_strikeout">' . $original_price_to_display . '</span></div>';
3272
  $item_price = '';
3273
  $haslimitedDiscount = 0;
3274
+ if(is_array($additional_details) && count($additional_details)){
3275
+ foreach ($additional_details as $key => $additional_detail){
3276
+ if(!empty($additional_detail) && isset($additional_detail['discount_price'])){
3277
+ $haslimitedDiscount = 1;
3278
+ if(get_option('woocommerce_prices_include_tax', 'no') == 'no'){
3279
+ if(get_option('woocommerce_tax_display_cart', 'incl') == 'incl'){
3280
+ $additional_detail['discount_price'] = FlycartWoocommerceProduct::get_price_including_tax($cart_item['data'], 1, $additional_detail['discount_price']);
3281
+ }
3282
+ } else {
3283
+ if(get_option('woocommerce_tax_display_cart') == 'excl'){
3284
+ $additional_detail['discount_price'] = FlycartWoocommerceProduct::get_price_excluding_tax($cart_item['data'], 1, $additional_detail['discount_price']);
3285
+ }
3286
  }
3287
+ $new_price_to_display = $cart_item['woo_discount']['original_price'] - $additional_detail['discount_price'];
3288
+ if($new_price_to_display < 0) $new_price_to_display = 0;
3289
+ $new_price_to_display = FlycartWoocommerceProduct::wc_price($new_price_to_display);
3290
+ $quantity -= $additional_detail['discount_quantity'];
3291
+ $item_price .= '<div style="float: left;">';
3292
+ $item_price .= '<span class="cart_price wdr_cart_strikeout"><del>' . $original_price_to_display . '</del> <ins>' . $new_price_to_display . '</ins></span>';
3293
+ $item_price .= '</div>';
3294
+ $item_price .= '<div style="float: right; padding-left: 1em;">';
3295
+ $item_price .= 'x '.$additional_detail['discount_quantity'];
3296
+ $item_price .= '</div>';
3297
+ $item_price .= '<div style="clear: both;"></div>';
3298
  }
 
 
 
 
 
 
 
 
 
 
 
3299
  }
3300
  }
3301
  if($haslimitedDiscount){
3555
 
3556
  if(FlycartWoocommerceVersion::wcVersion('3.1.0')){
3557
  $children = array_filter( array_map( 'wc_get_product', FlycartWoocommerceProduct::get_children($product) ), 'wc_products_array_filter_visible_grouped' );
3558
+ if(is_array($children) && count($children)){
3559
+ foreach ( $children as $child ) {
3560
+ if ( '' !== FlycartWoocommerceProduct::get_price($child) ) {
3561
+ $child_prices[FlycartWoocommerceProduct::get_id($child)] = 'incl' === $tax_display_mode ? FlycartWoocommerceProduct::get_price_including_tax($child) : FlycartWoocommerceProduct::get_price_excluding_tax( $child );
3562
+ }
3563
  }
3564
  }
3565
  } else {
3566
  $children = $product->get_children();
3567
+ if(is_array($children) && count($children)){
3568
+ foreach ( $children as $child_id ) {
3569
+ $child = FlycartWoocommerceProduct::wc_get_product($child_id);
3570
+ if ( '' !== FlycartWoocommerceProduct::get_price($child) ) {
3571
+ $child_prices[FlycartWoocommerceProduct::get_id($child)] = 'incl' === $tax_display_mode ? FlycartWoocommerceProduct::get_price_including_tax($child) : FlycartWoocommerceProduct::get_price_excluding_tax( $child );
3572
+ }
3573
  }
3574
  }
3575
  }
3625
 
3626
  if(FlycartWoocommerceVersion::wcVersion('3.1.0')){
3627
  $children = array_filter( array_map( 'wc_get_product', FlycartWoocommerceProduct::get_children($product) ), 'wc_products_array_filter_visible_grouped' );
3628
+ if(is_array($children) && count($children)){
3629
+ foreach ( $children as $child ) {
3630
+ if ( '' !== FlycartWoocommerceProduct::get_price($child) ) {
3631
+ $child_prices[FlycartWoocommerceProduct::get_id($child)] = 'incl' === $tax_display_mode ? FlycartWoocommerceProduct::get_price_including_tax($child) : FlycartWoocommerceProduct::get_price_excluding_tax( $child );
3632
+ }
3633
  }
3634
  }
3635
  } else {
3636
  $children = $product->get_children();
3637
+ if(is_array($children) && count($children)){
3638
+ foreach ( $children as $child_id ) {
3639
+ $child = FlycartWoocommerceProduct::wc_get_product($child_id);
3640
+ if ( '' !== FlycartWoocommerceProduct::get_price($child) ) {
3641
+ $child_prices[FlycartWoocommerceProduct::get_id($child)] = 'incl' === $tax_display_mode ? FlycartWoocommerceProduct::get_price_including_tax($child) : FlycartWoocommerceProduct::get_price_excluding_tax( $child );
3642
+ }
3643
  }
3644
  }
3645
  }
3728
  if($product->is_type(array('variable', 'subscription_variation', 'variable-subscription'))){
3729
  if(FlycartWoocommerceVersion::wcVersion('3.1.0')){
3730
  $children = array_filter( array_map( 'wc_get_product', FlycartWoocommerceProduct::get_children($product) ), 'wc_products_array_filter_visible_grouped' );
3731
+ if(is_array($children) && count($children)){
3732
+ foreach ( $children as $child ) {
3733
+ if ( '' !== FlycartWoocommerceProduct::get_price($child) ) {
3734
+ $discountPrice = $this->getDiscountPriceForTheProduct($child);
3735
+ if($discountPrice > 0){
 
 
 
 
 
3736
  $on_sale = true;
3737
  self::$product_on_sale[$product_id] = 1;
3738
  break;
3739
+ } else {
3740
+ $product_id = FlycartWoocommerceProduct::get_id($child);
3741
+ if($this->hasDiscountForProductId($product_id)){
3742
+ $on_sale = true;
3743
+ self::$product_on_sale[$product_id] = 1;
3744
+ break;
3745
+ }
3746
  }
3747
  }
3748
  }
3749
  }
3750
  } else {
3751
  $children = $product->get_children();
3752
+ if(is_array($children) && count($children)){
3753
+ foreach ( $children as $child_id ) {
3754
+ $child = FlycartWoocommerceProduct::wc_get_product($child_id);
3755
+ if ( '' !== FlycartWoocommerceProduct::get_price($child) ) {
3756
+ $discountPrice = $this->getDiscountPriceForTheProduct($child);
3757
+ if($discountPrice > 0){
 
 
 
 
 
3758
  $on_sale = true;
3759
  self::$product_on_sale[$product_id] = 1;
3760
  break;
3761
+ } else {
3762
+ $product_id = FlycartWoocommerceProduct::get_id($child);
3763
+ if($this->hasDiscountForProductId($product_id)){
3764
+ $on_sale = true;
3765
+ self::$product_on_sale[$product_id] = 1;
3766
+ break;
3767
+ }
3768
  }
3769
  }
3770
  }
3822
  $p_data = $product->get_data();
3823
  if(!empty($p_data['attributes'])){
3824
  $attr = array();
3825
+ if(is_array($p_data['attributes']) && count($p_data['attributes'])){
3826
+ foreach ($p_data['attributes'] as $key => $value){
3827
+ $attr['attribute_'.$key] = $value;
3828
+ }
3829
  }
3830
  $item['variation'] = $attr;
3831
  $item['variation_id'] = $product_id;
3862
  $item['data'] = $product;
3863
  $qty = 1;
3864
  $cart = FlycartWoocommerceCart::get_cart();
3865
+ if(is_array($cart) && count($cart)){
3866
+ foreach ( $cart as $cart_item ) {
3867
+ if($cart_item['product_id'] == $product_id ){
3868
+ $qty = $cart_item['quantity'];
3869
+ break; // stop the loop if product is found
3870
+ }
3871
  }
3872
  }
3873
  $item['quantity'] = $qty;
3879
  $p_data = $product->get_data();
3880
  if(!empty($p_data['attributes'])){
3881
  $attr = array();
3882
+ if(is_array($p_data['attributes']) && count($p_data['attributes'])){
3883
+ foreach ($p_data['attributes'] as $key => $value){
3884
+ $attr['attribute_'.$key] = $value;
3885
+ }
3886
  }
3887
  $item['variation'] = $attr;
3888
  $item['variation_id'] = $product_id;
3914
  protected function getQuantityOfProductInCart($productId){
3915
  $qty = 0;
3916
  $cart = FlycartWoocommerceCart::get_cart();
3917
+ if(is_array($cart) && count($cart)){
3918
+ foreach ( $cart as $cart_item ) {
3919
+ if($cart_item['product_id'] == $productId ){
3920
+ $qty = $cart_item['quantity'];
3921
+ break; // stop the loop if product is found
3922
+ }
3923
  }
3924
  }
3925
  return $qty;
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://flycart.org/
4
  Tags: woocommerce, discounts, dynamic pricing, Buy One Get One Free, pricing deals, price rules, bulk discounts, advanced discounts, pricing deals
5
  Requires at least: 4.4.1
6
  Tested up to: 5.0
7
- Stable tag: 1.7.9
8
  License: GPLv3 or later
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
@@ -312,6 +312,9 @@ Discount - Enter minimum & Maximum quantity -> Adjustment Type -> Product Discou
312
 
313
  == Changelog ==
314
 
 
 
 
315
  = 1.7.9 - 08/02/19 =
316
  * Fix - Displays sale badge for all products while a bogo rule is enabled for specific products
317
  * Fix - Not displaying sale badge for dependent product rule
4
  Tags: woocommerce, discounts, dynamic pricing, Buy One Get One Free, pricing deals, price rules, bulk discounts, advanced discounts, pricing deals
5
  Requires at least: 4.4.1
6
  Tested up to: 5.0
7
+ Stable tag: 1.7.10
8
  License: GPLv3 or later
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
312
 
313
  == Changelog ==
314
 
315
+ = 1.7.10 - 13/02/19 =
316
+ * Fix - Warning: Invalid argument supplied for foreach()
317
+
318
  = 1.7.9 - 08/02/19 =
319
  * Fix - Displays sale badge for all products while a bogo rule is enabled for specific products
320
  * Fix - Not displaying sale badge for dependent product rule
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.7.9
9
  * Slug: woo-discount-rules
10
  * Text Domain: woo-discount-rules
11
  * Domain Path: /i18n/languages/
5
  * Description: Simple Discount Rules for WooCommerce.
6
  * Author: Flycart Technologies LLP
7
  * Author URI: https://www.flycart.org
8
+ * Version: 1.7.10
9
  * Slug: woo-discount-rules
10
  * Text Domain: woo-discount-rules
11
  * Domain Path: /i18n/languages/