cart_keys = $cart_keys; $this->cart_array = &$cart_array; $this->rules_array = $rules_array; $this->running_max_discount = &$running_max_discount; $this->running_max = &$running_max; $this->_price = $price; $this->_weight = $weight; $this->mode = 'cat_combinational_rules'; $this->pid_category_id = array(); foreach ($this->cart_array as $pid => $qnty) { $prod_obj = wc_get_product($pid); if (empty($pid) || empty($prod_obj)) { continue; } if (get_class($prod_obj) == 'WC_Product_Variation') { //$id=is_wc_version_gt_eql('2.7')?$prod_obj->get_parent_id():$prod_obj->parent->get_id(); $id = wp_get_post_parent_id($pid); $this->pid_category_id[$pid] = $this->get_my_prod_cat_ids($id); } else { $this->pid_category_id[$pid] = $this->get_my_prod_cat_ids($pid); } } $this->update_product_valid_rules(); } } function get_my_prod_cat_ids($prod_id) { $terms = get_the_terms($prod_id, 'product_cat'); if ($terms) { $cats_ids_array = array(); foreach ($terms as $key => $term) { array_push($cats_ids_array, $term->term_id); $term2 = $term; if (!in_array($term2->parent, $cats_ids_array)) { while ($term2->parent > 0) { array_push($cats_ids_array, $term2->parent); $term2 = get_term_by("id", $term2->parent, "product_cat"); } } } return $cats_ids_array; } } function update_product_valid_rules() { $this->valid_rules = array(); foreach ($this->rules_array as $rule1) { if ($this->check_rule($rule1) === true) { $this->valid_rules[$rule1['rule_no']] = $rule1; } } } function check_rule(&$rule) { $prod_array = $this->cart_array; $cids = $rule['cat_id']; $rule_no = $rule['rule_no']; foreach ($this->pid_category_id as $pid => $pcat_ids) { if (is_array($pcat_ids) && is_array($cids)) { $tmp = array_intersect(array_keys($cids), $pcat_ids); if (!empty($pcat_ids) && !empty($tmp)) { if ($this->check_date_range_and_roles($rule) == true && $this->check_min_quantity($rule, $pid) == true) { if (!isset($rule['mapping']) || !in_array($pid, $rule['mapping']) && apply_filters('eha_dp_skip_product', false, $pid, $rule, $this->mode) == false) { $rule['mapping'][] = $pid; } } else { return false; } } } } return true; } function check_min_quantity(&$rule1, $pid2) { $rule = $rule1; extract($rule); if ($discount_type == 'Flat Discount' && isset($this->running_max[$rule_no . ":" . $this->mode]['min']) && $this->running_max[$rule_no . ":" . $this->mode]['min'] == 1) { return false; } if (isset($this->running_max_discount[$rule_no . ":" . $this->mode]) && $this->running_max_discount[$rule_no . ":" . $this->mode] <= 0) { return false; } foreach ($this->cart_array as $pid => $qnty) { foreach ($this->pid_category_id[$pid] as $pcat_id) { if (isset($rule['cat_id'][$pcat_id])) { $rule['cat_id'][$pcat_id] -= $this->cart_array[$pid]; if ($rule['cat_id'][$pcat_id] <= 0) { unset($rule['cat_id'][$pcat_id]); } } } } if (empty($rule['cat_id'])) { return true; } return false; } function check_roles($role) { if ($role == 'all' || current_user_can($role)) { return true; } return false; } function check_date_range_and_roles($rule) { $fromdate = $rule['from_date']; $todate = $rule['to_date']; $user_role = $rule['allow_roles']; $offer_name = $rule['offer_name']; if ($this->check_roles($user_role) != true) { return false; } $now = date('d-m-Y'); $return = false; if (empty($fromdate) && empty($todate)) { $return = true; } elseif (empty($fromdate) && empty($todate) == false && (strtotime($now) <= strtotime($todate))) { $return = true; } elseif (empty($fromdate) == false && (strtotime($now) >= strtotime($fromdate)) && empty($todate)) { $return = true; } elseif ((strtotime($now) >= strtotime($fromdate)) && (strtotime($now) <= strtotime($todate))) { $return = true; } else { $return = false; } global $product; if (is_product() == true && $return === true && !empty($product) && is_string($product) === false) { $pid = is_wc_version_gt_eql('2.7') ? $product->get_id() : $product->id; //$parent_id=is_wc_version_gt_eql('2.7')?$product->get_parent_id():$product->parent_id; $parent_id = wp_get_post_parent_id($pid); global $offers; $offers['cat_combinational_rules'][$rule['rule_no']] = $offer_name; } return $return; } function get_valid_rules() { return parent::get_valid_rules(); } public function possible_solutions(&$ps) { $mode = $this->mode; foreach ($this->valid_rules as $rule_no => $rule) { $cal_discount = 0.0; $dp = 0; $val = $rule['value']; $cids = $rule['cat_id']; $price = 0; $max_discount = $this->running_max_discount[$rule_no . ":" . $mode]; $prods_ids = array(); foreach ($this->pid_category_id as $pid => $pcat_ids) { if (is_array($pcat_ids) && is_array($cids)) { $tmp = array_intersect(array_keys($cids), $pcat_ids); if (!empty($pcat_ids) && !empty($tmp)) { $price += $this->_price[$pid]; $prods_ids[] = $pid; } } } if ($rule['discount_type'] == 'Percent Discount') { $cal_discount = (floatval($price) * $val) / 100; } elseif ($rule['discount_type'] == 'Flat Discount') { $cal_discount = floatval($val); } elseif ($rule['discount_type'] == 'Fixed item Price' || $rule['discount_type'] == 'Fixed Price') { $cal_discount = floatval($price) - $val; } else { die('error on ref:23x22334, message: discount_type is not set for rule no:' . $rule_no); } if ($cal_discount > $max_discount && $max_discount <> 0) { $cal_discount = $max_discount; } $dp = $cal_discount; $dp = round($dp, wc_get_price_decimals(), PHP_ROUND_HALF_UP); $dp = strval($dp); if (!isset($ps[$rule_no . ':' . $mode][$dp]) || !in_array(array($dp => implode(',', array_keys($rule['cat_id']))), $ps[$rule_no . ':' . $mode][$dp])) $ps[$rule_no . ':' . $mode][$dp] = array($dp => implode(',', $prods_ids)); } } // end function possible_sollution public function execute_rule($rule_no, &$running_max_discount, $selected_prods = array()) { //print_r($selected_prods); if (is_cart() && isset($_GET['debug']) ) { echo "\n
"; } return $discount_amt; } //end function execute_rule(); } }