Skip to content

Instantly share code, notes, and snippets.

@rajeshsingh520
Created January 6, 2025 03:15
Show Gist options
  • Save rajeshsingh520/d685773d8ddc2c7e84d9207b55888fe8 to your computer and use it in GitHub Desktop.
Save rajeshsingh520/d685773d8ddc2c7e84d9207b55888fe8 to your computer and use it in GitHub Desktop.
class custom_20240106{
static $instance = null;
static $production_start_time = '10:00'; //time in 24hr format
static $production_end_time = '18:00'; //time in 24hr format
public $preparation_time;
public $current_time;
public $current_date;
public $next_date;
static function get_instance(){
if(is_null(self::$instance)){
self::$instance = new self();
}
return self::$instance;
}
function __construct(){
$this->preparation_time = get_option('pi_order_preparation_hours', 240);
$this->current_time = current_time('H:i');
$this->current_date = current_time('Y/m/d');
$this->next_date = date('Y/m/d', strtotime($this->current_date . ' +1 day'));
add_filter('pisol_dtt_custom_remove_time_slots', [$this, 'removeTimeSlots'], 10, 2);
}
function removeTimeSlots($slots, $date){
if($date == $this->next_date || $date == $this->current_date){
$this->removeSlots($slots);
}
return $slots;
}
function removeSlots(&$slots){
$production_end_time = $this->productionEndOn();
foreach($slots as $key => $slot){
if(strtotime($slot['from']) < strtotime($production_end_time)){
unset($slots[$key]);
}
}
return $slots;
}
function productionEndOn(){
$current_time = current_time('Y/m/d H:i');
$prodctuin_end_time = $this->current_date . ' ' . self::$production_end_time;
$prodctuin_start_time = $this->current_date . ' ' . self::$production_start_time;
$next_day_start_time = $this->next_date . ' ' . self::$production_start_time;
if(strtotime($current_time) > strtotime($prodctuin_end_time)){
$production_remaining = $this->preparation_time;
$production_end_time = $this->productionEndTime($production_remaining, $next_day_start_time);
}
if(strtotime($current_time) < strtotime($prodctuin_start_time)){
$production_remaining = $this->preparation_time;
$production_end_time = $this->productionEndTime($production_remaining, $prodctuin_start_time);
}
if(strtotime($current_time) >= strtotime($prodctuin_start_time) && strtotime($current_time) <= strtotime($prodctuin_end_time)){
$production_minutes_available_today = (strtotime($prodctuin_end_time) - strtotime($current_time)) / 60;
if($production_minutes_available_today >= $this->preparation_time){
$production_remaining = 0;
$production_end_time = $this->productionEndTime($production_remaining, current_time('Y/m/d H:i'));
}else{
$production_remaining = $this->preparation_time - $production_minutes_available_today;
$production_end_time = $this->productionEndTime($production_remaining, $next_day_start_time);
}
}
return $production_end_time;
}
function productionEndTime($production_remaining, $production_start_time){
$production_end_time = date('Y/m/d H:i', strtotime($production_start_time . ' +'.$production_remaining.' minutes'));
return $production_end_time;
}
}
custom_20240106::get_instance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment