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='combinational_rules'; $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; $pids=$rule['product_id']; $rule_no=$rule['rule_no']; foreach ($pids as $pid=>$qnty) { if(array_key_exists($pid,$prod_array) && $this->check_date_range_and_roles($rule,$pid) ==true && $this->check_min_quantity($rule,$pid,$qnty)==true ) { if ( !isset($rule['mapping']) || !in_array($pid,$rule['mapping'])) { $rule['mapping'][]=$pid; } } else { return false; } } return true; } function check_min_quantity(&$rule,$pid,$min) { 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; } if($this->cart_array[$pid]>=$min && apply_filters('eha_dp_skip_product',false,$pid,$rule,$this->mode)==false) { return true; } else { return false; } return false; } function check_roles($role) { if($role=='all' || current_user_can($role)) { return true; } return false; } function check_date_range_and_roles($rule,$pid) { $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) { $cpid = is_wc_version_gt_eql('2.7')?$product->get_id():$product->id; $prod=wc_get_product($pid); //$parent_id=is_wc_version_gt_eql('2.7')?$prod->get_parent_id():$prod->parent_id; $parent_id=wp_get_post_parent_id( $cpid); if($pid==$cpid || $parent_id==$cpid) { global $offers; $offers['combinational_rules'][$rule['rule_no']]=$offer_name; } } return $return; } function get_min_valid_quantity($rule) { $min_valid_qnty=1; extract($rule); if(!array_key_exists($product_id,$this->cart_array)) { return false; } $unit_price=$this->_price[$product_id]; $unit_weight=$this->_weight[$product_id]; if($check_on=='Price') { if(!empty($this->_price) && array_key_exists($product_id,$this->_price)===true) { $min_valid_qnty=ceil(floatval($min)/floatval($unit_price)); } } elseif($check_on=='Quantity') { if(!empty($this->_weight) && array_key_exists($product_id,$this->_weight)===true) { $min_valid_qnty=ceil($min); } } elseif($check_on=='Weight') { $min_valid_qnty=ceil(floatval($min)/floatval($unit_weight)); } else { $min_valid_qnty=1; } return $min_valid_qnty; } 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']; $price=0; $max_discount=$this->running_max_discount[$rule_no.":".$mode]; foreach ($rule['product_id'] as $pid=>$req_qnty) { 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]; } 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['product_id']))),$ps[$rule_no.':'.$mode][$dp])) $ps[$rule_no.':'.$mode][$dp]=array($dp=>implode(',', array_keys($rule['product_id']))); } } // 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(); } }