• Asad

    (@assadali15)


    I am working in a scenario in my website using WordPress, for almost a week but not yet find a solution :(.

    It is an online food portal where a user cannot order any dishes/foods within two time frame like:

    first time slot -> 12:00 pm to 4:30 pm (Morning = lunch) user can not order lunch in this time frame but can order dinner in this time.
    second time slot -> 4:35 pm to 9:00 pm (Evening = dinner) user can not order dinner in this time frame but can order lunch in this time.
    In short, in between 12:00 pm to 4:30 pm user can not order lunch but can order other then this time. similarly in between 4:35 pm to 9:00 pm user can not order dinner but can order any other time

    I am using following hooks and functions:

    but not working yet:

    function add_the_date_validation( $passed ) { 
    
     $passed = true;
     date_default_timezone_set("Asia/Karachi");
        $day = date('l'); 
    
        //Getting Day:
    
         $time = date("h:i a");
    
         if(!empty($_POST['select-1614371742572'])) {
            
    
            $selected = $_POST['select-1614371742572'];
    
            $lunchstarttime = "12:10 pm";
            $lunchendtime = "04:28 pm";
    
            $dinnerstarttime = "04:30 PM";
            $dinnerendtime = "09:00 PM";
    
            //Check for Lunch
    
             if($selected == "Lunch")
             {
                echo $time; 
                 
                 if($time > $lunchstarttime_a && $time < $lunchendtime_a)
                 {
                
                     wc_add_notice( __( 'You can not order lunch between 12:00 PM to 4:30 PM', 'woocommerce' ), 'error' );
                     $passed = false;
                 }                  
             }
    
             //Check for Dinner
    
             if($selected == "Dinner")
             {
                echo $time; 
                 if($time > $dinnerstarttime &&  $time < $dinnerendtime){
                 
                 wc_add_notice( __( 'You can not order Dinner between 4:30 AM to 12 AM', 'woocommerce' ), 'error' );
                     echo "false before";
                 $passed = false;    
                
                }
             }
            
             return $passed;
        
         }else{
             $passed = true;
             return $passed;
         }
    
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'add_the_date_validation', 10, 5 );

    Please help me out i am stucking in this problem since last Monday and i have a lot of pressure for that

    thanks regards, asad

Viewing 2 replies - 1 through 2 (of 2 total)
  • Zack Krida

    (@zackkrida)

    Hi @assadali15, your problem is that when you are comparing the times, with these lines:

    if($time > $lunchstarttime && $time < $lunchendtime) and if($time > $dinnerstarttime && $time < $dinnerendtime){ you are comparing strings, not the actual dates. Your code here is asking things like ‘is “10:30pm” greater than “9:30am”‘, which can’t be handled with a string comparison. See the below example:

    
    $time = strtotime('10:30 pm');
    $time2 = strtotime('4:30 pm');
    
    echo $time > $time2; // returns 1 (true)
    echo '10:30 pm' > '4:30 pm'; // returns 0 (false)
    

    Without using the strtotime to turn your strings back into timestamps, you can’t reliably compare them. Try using strtotime on your date("h:i a") formatted string before comparing them in your if statements. Good luck!

    Thread Starter Asad

    (@assadali15)

    Hello Developers,

    I am again stucking in a problem!!

    i have created my wordpress website 4 months ago and the upper mentioned topic helped me alot
    not my client turned out new senario:

    they are saying that in website user can not order from night(10:30 pm) to morning (9:00
    am) Pakistan Standard Time.

    i used this following code:

    function add_the_date_validation( $passed, $product_id, $quantity ) {
    $passed = true;
    date_default_timezone_set(“Asia/Karachi”);

    $day = date(‘l’);

    $time = strtotime(date(‘h:i’));

    $lunchstarttime_a = strtotime(“10:30”);
    $lunchendtime_a = strtotime(“09:05”);

    $dinnerstarttime = strtotime(“10:30”);
    $dinnerendtime = strtotime(“09:05”);

    //Check for Lunch

    if($selected == “Lunch”)
    {
    if($time < $lunchstarttime_a && $time < $lunchendtime_a)
    {

    $passed = false;
    }
    if(!$passed)
    {
    wc_add_notice( __( ‘SORRY! YOU CANNOT ORDER FOR LUNCH & DINNER IN BETWEEN 10:30 PM TO 9 AM DUE TO MEAL BOX OPERATIONAL HOURS.THANK YOU FOR YOUR PATIENCE!.’, ‘woocommerce’ ), ‘error’ );
    }
    }
    }

    according to my senario any user/ customer should not order after 10:30 night and 9:05 morning

    please help me we are running out of time

    regards,
    asad

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘need help in time slot checking in wordpress’ is closed to new replies.