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 = 'category_rules'; if (empty($this->pid_category_id)) { foreach ($this->cart_array as $pid => $qnty) if (!empty($pid) && !empty($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 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; $min = (is_null($rule['min']) == true) ? 1 : $rule['min']; $max = (is_null($rule['max']) == true) ? 999999 : $rule['max']; if ($max < $min && $max != 0) { return false; } if ($this->check_date_range_and_roles($rule) == true && $this->check_min_max_all($rule) == true) { return true; } return false; } 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 check_min_max_all(&$rule) { $valid_pid = array(); $valid_total_price = 0.0; $valid_total_weight = 0.0; $valid_total_units = 0; $valid_no_of_items = 0; $new_mapping = array(); extract($rule); if (empty($max)) { $max = 999999; } 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[$rule_no . ":" . $this->mode][$check_on]) && ($this->running_max[$rule_no . ":" . $this->mode][$check_on] > $max && ($max != 0 || !is_null($max)))) { 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 $npid => $qnty) { $nextwt = 0.0; if ($check_on == 'Weight') { $nextwt = $this->_weight[$npid]; } if (!isset($this->running_max[$rule_no . ":" . $this->mode][$check_on]) || (($this->running_max[$rule_no . ":" . $this->mode][$check_on] + $nextwt) >= $max && ($max != 0 || !is_null($max)))) { return false; } } $all_possible_prod_qnty = array(); foreach ($this->cart_array as $pid => $qnty) { if (isset($this->pid_category_id[$pid]) && in_array($rule['category_id'], $this->pid_category_id[$pid]) && apply_filters('eha_dp_skip_product', false, $pid, $rule, $this->mode) == false) { { $all_possible_prod_qnty[] = $pid . ":" . $qnty; } $valid_pid[] = $pid; $valid_no_of_items += 1; $valid_total_units += $qnty; $valid_total_price = $valid_total_price + ( $qnty * $this->_price[$pid] ); $valid_total_weight = $valid_total_weight + ( $qnty * (!empty($this->_weight[$pid]) ? $this->_weight[$pid] : 0.0) ); } } if (sizeof($all_possible_prod_qnty) == 0 || $valid_total_price <= 0) { return false; } if ($check_on == 'Quantity' && ($valid_no_of_items < $min || $valid_no_of_items > $max )) { return false; } if ($check_on == 'TotalQuantity' && ($valid_total_units < $min || $valid_total_units > $max )) { return false; } if ($check_on == 'Price' && ($valid_total_price < $min || $valid_total_price > $max )) { return false; } if ($check_on == 'Weight' && ($valid_total_weight < $min || $valid_total_weight > $max )) { return false; } $possible_combinations = super_set($all_possible_prod_qnty); foreach ($possible_combinations as $subset) { $_total_price = 0.0; $_total_weight = 0.0; $_total_no_of_items = 0; $_total_units = 0; $used_pid = array(); foreach ($subset as $pidandqnty) { $pq = explode(':', $pidandqnty); $_pid = $pq[0]; $_qnty = $pq[1]; if (in_array($_pid, $used_pid)) { continue(2); } $used_pid[] = $_pid; $_total_price += $this->_price[$_pid] * $_qnty; $_total_no_of_items += 1; $_total_units += $_qnty; $_total_weight += (!empty($this->_weight[$_pid]) ? $this->_weight[$_pid] : 0.0) * $_qnty; } if (($check_on == 'Quantity' && $_total_no_of_items + $this->running_max[$rule_no . ":" . $this->mode][$check_on] >= $min && $_total_no_of_items + $this->running_max[$rule_no . ":" . $this->mode][$check_on] <= $max ) || ($check_on == 'TotalQuantity' && $_total_units >= $min && $_total_units <= $max ) || ($check_on == 'Price' && $_total_price + $this->running_max[$rule_no . ":" . $this->mode][$check_on] >= $min && $_total_price + $this->running_max[$rule_no . ":" . $this->mode][$check_on] <= $max ) || ($check_on == 'Weight' && $_total_weight + $this->running_max[$rule_no . ":" . $this->mode][$check_on] >= $min && $_total_weight + $this->running_max[$rule_no . ":" . $this->mode][$check_on] <= $max )) { if (!in_array($subset, $new_mapping)) { $new_mapping[] = $subset; } } } if (sizeof($new_mapping) > 0) { $rule['mapping'] = $new_mapping; return true; } } 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; global $offers; if (isset($this->pid_category_id[$pid]) && in_array($rule['category_id'], $this->pid_category_id[$pid])) { $offers['category_rules'][$rule['rule_no']] = $offer_name; } $prod = wc_get_product($pid); $parent_id = wp_get_post_parent_id($pid); if (isset($parent_id)) { $pid = $parent_id; if (isset($this->pid_category_id[$pid]) && in_array($rule['category_id'], $this->pid_category_id[$pid])) { $offers['category_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) { if (!isset($rule['mapping']) || empty($rule['mapping'])) { continue; } $cal_discount = 0.0; $dp = 0; $val = $rule['value']; $max_discount = $this->running_max_discount[$rule_no . ":" . $mode]; $subset = $rule['mapping']; $price = 0.0; // if any problem place this inside nested for below foreach ($subset as $pids) { $cal_discount = 0.0; $total_units = 0; foreach ($pids as $pid_qnty) { $_pr = explode(':', $pid_qnty); $pid = $_pr[0]; $qnty = $_pr[1]; if (!isset($this->cart_array[$pid])) { continue(2); } if ($this->cart_array[$pid] == 0) { unset($this->cart_array[$pid]); continue(2); } $price += $this->_price[$pid] * $qnty; $total_units += $qnty; } if ($rule['discount_type'] == 'Percent Discount') { $cal_discount += (floatval($price) * $val) / 100; } elseif ($rule['discount_type'] == 'Flat Discount') { $cal_discount += floatval($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; } if ($price > 0) { $dp = $cal_discount; $dp = $dp / $total_units; $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(',', $pids)), $ps[$rule_no . ':' . $mode][$dp])) $ps[$rule_no . ':' . $mode][$dp] = array($dp => implode(',', $pids)); } } } } // end function possible_sollution public function execute_rule($rule_no, &$running_max_discount, $selected_prods) { if (is_cart() && isset($_GET['debug']) ) { echo "\n"; } return $discount_amt; } //end function execute_rule(); } }