slug = $slug; $this->condition = 'postcode'; /* this adds the condition in set of rules dropdown */ add_filter("pi_".$this->slug."_condition", array($this, 'addRule')); /* this gives value field blank of populated */ add_action( 'wp_ajax_pi_'.$this->slug.'_value_field_'.$this->condition, array( $this, 'ajaxCall' ) ); add_filter('pi_'.$this->slug.'_saved_values_'.$this->condition, array($this, 'savedDropdown'), 10, 3); add_filter('pi_'.$this->slug.'_condition_check_'.$this->condition,array($this,'conditionCheck'),10,4); add_action('pi_'.$this->slug.'_logic_'.$this->condition, array($this, 'logicDropdown')); add_filter('pi_'.$this->slug.'_saved_logic_'.$this->condition, array($this, 'savedLogic'),10,3); } function addRule($rules){ $rules[$this->condition] = array( 'name'=>__('Postcode', 'advanced-free-flat-shipping-woocommerce'), 'group'=>'location_related', 'condition'=>$this->condition ); return $rules; } function logicDropdown(){ $html = ""; $html .= 'var pi_logic_'.$this->condition.'= "";'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $html; } function savedLogic($html_in, $saved_logic, $count){ $html = ""; $html .= ''; return $html; } function ajaxCall(){ $cap = Pi_Efrs_Menu::getCapability(); if(!current_user_can( $cap )) { return; die; } $count = sanitize_text_field(filter_input(INPUT_POST,'count')); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo Pi_efrs_selection_rule_main::createTextField($count, $this->condition, null); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $this->description(); die; } function savedDropdown($html, $values, $count){ $html = Pi_efrs_selection_rule_main::createTextField($count, $this->condition, $values); $html .= $this->description(); return $html; } function description(){ $html = 'How to insert multiple post code, range of post code and wildcard post code
'; return $html; } function conditionCheck($result, $package, $logic, $values){ $or_result = false; $cart_postcode = PISOL\EFRS\Package::get_postcode($package); $rules = $this->getPostCodes( $values[0] ); $post_code_matched = $this->postCodeMatched( $cart_postcode, $rules); $rule_cart_postcode = $values[0]; switch ($logic){ case 'equal_to': if($post_code_matched){ $or_result = true; } break; case 'not_equal_to': if($post_code_matched){ $or_result = false; }else{ $or_result = true; } break; } return $or_result; } function getPostCodes( $text_value ){ $post_codes = array(); $values = explode(',', $text_value); $post_codes = array_map( 'trim', $values ); return $post_codes; } function postCodeMatched( $post_code, $rules){ $country = apply_filters('pisol_efrs_postcode_country', function_exists('WC') && is_object(WC()->customer) ? WC()->customer->get_shipping_country() : ''); foreach($rules as $rule){ $object[] = (object)array( 'zone_id'=> 1, 'location_code'=> $rule ); /** * this is woocommerce location matcher function */ $match = wc_postcode_location_matcher($post_code, $object, 'zone_id', 'location_code', $country); if(count($match) > 0 ) return true; } return false; } } new Pi_efrs_selection_rule_postcode(PI_EFRS_SELECTION_RULE_SLUG);