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='cart_rules'; if(empty($this->pid_category_id)) { foreach($this->cart_array as $pid=>$qnty) { $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) { foreach ($terms as $key => $term) { $cats_ids_array[$key] = $term->term_id; } return $cats_ids_array; } } function check_min_max_all(&$rule) { if(is_null($rule)) { return FALSE; } $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) || is_null($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_discount[$rule_no.":".$this->mode]) && ($this->running_max_discount[$rule_no.":".$this->mode]<=0)) { 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; } $all_possible_prod_qnty=array(); foreach ($this->cart_array as $pid=>$qnty) { if( 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; } //die('
'.print_r($all_possible_prod_qnty,true).'
'); if(isset($rule['discount_type']) && $rule['discount_type']=='Fixed Price') { $possible_combinations=array($all_possible_prod_qnty); }else { $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>=$min && $_total_no_of_items<=$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>=$min && $_total_weight<=$max ) ) { if(!in_array($subset, $new_mapping)) { $new_mapping[]=$subset; } } else { } } if(!empty($new_mapping)>0) { $rule['mapping']=$new_mapping; 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; } if(is_product()==true && $return===true && !is_cart()) { global $offers; $offers['cart_rules'][$rule['rule_no']]=$offer_name; } return $return; } function get_valid_rules() { return $this->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']; $total_units=0; $price=0.0; foreach ( $subset as $pids) { $cal_discount=0.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); } elseif($rule['discount_type']=='Fixed Price') { $cal_discount+=floatval($price)-$val; } else { die('error on ref:23x22KK4, 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(); } }