Last active
February 21, 2026 05:03
-
-
Save rajeshsingh520/adf85b6fbbb63551e5b581998cab26f7 to your computer and use it in GitHub Desktop.
charge fees based on selected delivery time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if(defined('PI_CEFW_SELECTION_RULE_SLUG')){ | |
| class Pi_cefw_selection_rule_selected_time_between_time{ | |
| public $slug; | |
| public $condition; | |
| function __construct($slug){ | |
| $this->slug = $slug; | |
| $this->condition = 'selected_time_between_time'; | |
| /* this adds the condition in set of rules dropdown */ | |
| add_filter("pi_".$this->slug."_condition", array($this, 'addRule')); | |
| /* this gives value field to store condition value either select or text box */ | |
| add_action( 'wp_ajax_pi_'.$this->slug.'_value_field_'.$this->condition, array( $this, 'ajaxCall' ) ); | |
| /* This gives our field with saved value */ | |
| add_filter('pi_'.$this->slug.'_saved_values_'.$this->condition, array($this, 'savedDropdown'), 10, 3); | |
| /* This perform condition check */ | |
| add_filter('pi_'.$this->slug.'_condition_check_'.$this->condition,array($this,'conditionCheck'),10,4); | |
| /* This gives out logic dropdown */ | |
| add_action('pi_'.$this->slug.'_logic_'.$this->condition, array($this, 'logicDropdown')); | |
| /* This give saved logic dropdown */ | |
| add_filter('pi_'.$this->slug.'_saved_logic_'.$this->condition, array($this, 'savedLogic'),10,3); | |
| add_action('wp_enqueue_scripts', [$this, 'enqueueAssets']); | |
| } | |
| function enqueueAssets(){ | |
| wp_add_inline_script('jquery', " | |
| jQuery(document).on('change', 'input[name=\"pi_delivery_time\"]', function() { | |
| jQuery(document.body).trigger('update_checkout'); | |
| }); | |
| "); | |
| } | |
| function addRule($rules){ | |
| $rules[$this->condition] = array( | |
| 'name'=>__('Selected Time-Based', 'conditional-extra-fees-woocommerce'), | |
| 'group'=>'order_date_time_plugin', | |
| 'condition'=>$this->condition | |
| ); | |
| return $rules; | |
| } | |
| function logicDropdown(){ | |
| $html = ""; | |
| $html .= 'var pi_logic_'.$this->condition.'= "<select class=\'form-control\' name=\'pi_selection[{count}][pi_'.$this->slug.'_logic]\'>'; | |
| $html .= '<option value=\'Between\'>Between start and end time</option>'; | |
| $html .= '</select>";'; | |
| echo $html; | |
| } | |
| function savedLogic($html_in, $saved_logic, $count){ | |
| $html = ""; | |
| $html .= '<select class="form-control" name="pi_selection['.$count.'][pi_'.$this->slug.'_logic]">'; | |
| $html .= '<option value=\'Between\'>Between start and end time</option>'; | |
| $html .= '</select>'; | |
| return $html; | |
| } | |
| function ajaxCall(){ | |
| if(!current_user_can( 'manage_options' )) { | |
| return; | |
| die; | |
| } | |
| $count = sanitize_text_field(filter_input(INPUT_POST,'count')); | |
| $html_start = self::createTextField($count, null,'start_time', $this->condition); | |
| $html_end = self::createTextField($count, null, 'end_time', $this->condition); | |
| echo self::bootstrapRow($html_start, $html_end); | |
| die; | |
| } | |
| static function bootstrapRow($left, $right){ | |
| return sprintf('<div class="row"><div class="col-6">Start time <br>%s <a href="javascript:void(0)" onclick="jQuery(this).closest(\'.col-6\').find(\'input.time-picker\').val(\'\').trigger(\'change\'); return false;" class="pi-clear-time">Clear value</a></div><div class="col-6">End time<br>%s<a href="javascript:void(0)" onclick="jQuery(this).closest(\'.col-6\').find(\'input.time-picker\').val(\'\').trigger(\'change\'); return false;" class="pi-clear-time">Clear value</a></div></div>', $left, $right); | |
| } | |
| function savedDropdown($html, $values, $count){ | |
| $html_start = self::createTextField($count, $values['start_time'],'start_time' ,$this->condition ); | |
| $html_end = self::createTextField($count, $values['end_time'], 'end_time', $this->condition); | |
| return self::bootstrapRow($html_start,$html_end); | |
| } | |
| static function createTextField($count, $value, $name, $condition =""){ | |
| if(!empty($value)){ | |
| $value_attr = ' value="'.esc_attr($value).'" '; | |
| }else{ | |
| $value_attr = ""; | |
| } | |
| $html = '<input type="time" class="form-control time-picker" data-condition="'.$condition.'" name="pi_selection['.$count.'][pi_'.PI_CEFW_SELECTION_RULE_SLUG.'_condition_value]['.$name.']" '.$value_attr.' >'; | |
| return $html; | |
| } | |
| function selected_time($package){ | |
| if(is_a($package, 'WC_Cart')){ | |
| if(!isset($_POST['post_data']) && !isset($_POST['pi_delivery_time'])){ | |
| $get_from_session = WC()->session->get('pi_selected_time'); | |
| if(!empty($get_from_session)){ | |
| return self::breakingTime($get_from_session); | |
| } | |
| return false; | |
| } | |
| if(isset($_POST['pi_delivery_time'])){ | |
| $values['pi_delivery_time'] = $_POST['pi_delivery_time']; | |
| }else{ | |
| parse_str($_POST['post_data'], $values); | |
| } | |
| if(!empty($values['pi_delivery_time'])){ | |
| return self::breakingTime($values['pi_delivery_time']); | |
| } | |
| }elseif(is_a($package, 'WC_Order')){ | |
| return self::breakingTime($package->get_meta('pi_delivery_time', true)); | |
| } | |
| return false; | |
| } | |
| static function breakingTime($time){ | |
| $time_parts = explode('-', $time); | |
| if(count($time_parts) > 1){ | |
| return trim($time_parts[1]); | |
| } | |
| return trim($time_parts[0]); | |
| } | |
| function conditionCheck($result, $package, $logic, $values){ | |
| $or_result = false; | |
| $start_time = !empty($values['start_time']) ? $values['start_time'] : ""; | |
| $end_time = !empty($values['end_time']) ? $values['end_time'] : ""; | |
| $current_time = $this->selected_time($package); | |
| if(!empty($start_time) && !empty($end_time)){ | |
| if(strtotime($current_time) >= strtotime($start_time) && strtotime($end_time) >= strtotime($current_time)){ | |
| return true; | |
| }else{ | |
| return false; | |
| } | |
| }elseif(empty($start_time) && empty($end_time)){ | |
| return false; | |
| }elseif(!empty($start_time) && empty($end_time)){ | |
| if(strtotime($current_time) >= strtotime($start_time)){ | |
| return true; | |
| }else{ | |
| return false; | |
| } | |
| }elseif(empty($start_time) && !empty($end_time)){ | |
| if(strtotime($end_time) >= strtotime($current_time)){ | |
| return true; | |
| }else{ | |
| return false; | |
| } | |
| } | |
| return $or_result; | |
| } | |
| } | |
| new Pi_cefw_selection_rule_selected_time_between_time(PI_CEFW_SELECTION_RULE_SLUG); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment