0
votes

that is what i have now!.

theme/deliverHours.js:

Update

   window.onload = function () {
    getHour= new Date().toLocaleTimeString("en-US",{timeZone:"America/New_York",hour12: false});
    getDay= new Date().toLocaleTimeString("en-US",{timeZone:"America/New_York",hour12: false,weekday:'short'});
    day  = getDay.split(' ');
    arrayDate = getHour.split(':');
  document.getElementById('shipping_method_0_local_pickup8').onclick=function() {
        if(document.getElementById('shipping_method_0_local_pickup8').checked){
            console.log('local Pickup!');
            console.log(getDay);
            if(!(arrayDate[0] >= 7 && arrayDate[0] <= 16 && day[0] == 'Sun')) {
                        document.getElementById('msg').innerHTML = "Sorry,We are Closed";
              document.getElementById('checkOut').disabled = true
                }else{
          document.getElementById('checkOut').disabled = false
          console.log('enjoy your food');
        }
        }
    } 

    document.getElementById('shipping_method_0_flat_rate1').onclick=function(){
        if(document.getElementById('shipping_method_0_flat_rate1').checked){
            console.log('flat rate!');
            console.log(getDay);

          if(!(arrayDate[0] >= 7 && arrayDate[0] <= 16 && day[0] == 'Sun')) {
                      document.getElementById('msg').innerHTML = "sorry,we are closed. "
                    document.getElementById('checkOut').disabled = true
                  }else{
          document.getElementById('checkOut').disabled = false
          console.log('enjoy your food');
        }
}
}
}

theme/function.php:

function deliver_business_hours(){
   wp_register_script('deliverHours', plugins_url('deliverHours.js'), array(),'1.0', true);

wp_enqueue_script('deliverHours');
}




add_action( 'wp_enqueue_script', 'deliver_business_hours');

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. this text is useless...

that should work right?.

1
So, what's your question/problem? - Scott Marcus
ok thanks i didnt knew what i was doing, i changed the code, so do you know how can i identify between local_pickup or Flat rate? - A. Angel

1 Answers

1
votes

This is an unfortunately common problem for beginners. Let's walk through and see why this doesn't work:

  1. When the checkout page is visited, the user's browser sends a request to the server with any relevant cookies, etc.
  2. The server begins executing php (a.k.a. WordPress), eventually your little function will be executed.
  3. Your echo will inject everything in the string into the html that gets sent back to the user.
  4. The user's browser sees javascript and begins executing. Inside the javascript the browser sees this weird tag <?php>. Browsers do not know how to execute php. Their default is to simply ignore anything they don't understand. That is why it doesn't work, by echoing php code, you've told the browser to do something that it doesn't understand.

PHP is a server-side language. If you want to write php it must be executed by the server in the course of an HTTP request. If you need something executed on the client side, it must be written in all javascript.

So this leaves you with two options:

  1. Rewrite all your logic to be handled by JS.
  2. Write a minimal amount of JS to make a call back to the server to process in PHP.

In your case, I think a little bit of javascript should be able to notify the customer of your delivery hours.


Side notes on programming style:

You really shouldn't ever echo any html tags, especially <script> and <style> tags. You should have separate .js and .css files that you then include into the html document. WordPress provides a handy function: wp_enqueue_script that will do this for you in all the appropriate ways.


Update:

For PHP: You will need to get the radio buttons into the form that is submitted. Probably using the appropriate hook/filter for woocommerce. After that the value can be accessed on submission from $_POST['shipping_method'].

For JS: Here is a question about getting a timezone specific datetime from the browser. It seems like this is a very local webstore though, so I'll mention that instantiating javascript's Date object gets the current local datetime. So:

var now = new Date();
var hour = now.getHours();
var day = now.getDay();

Should get you the hour and day as you've been using them in php.