0

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?.

A. Angel
  • 3
  • 2

1 Answers1

1

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.

Kallmanation
  • 1,132
  • 7
  • 11
  • ok thanks i didnt knew what i was doing, i changed the code do you know how can i identify between local_pickup or Flat rate? – A. Angel Nov 08 '17 at 19:12
  • If you want a dynamic message to appear before they submit, you will have to use javascript. But don't worry, it won't be too hard. If you really do want to stick with php, your checking code will have to execute when they submit and will have to redirect them back to the form with the notice. The appropriate information should come across in the `$_POST` variable. – Kallmanation Nov 08 '17 at 19:28
  • i tried to do all wit $_POST variable but i dont know how because the radio buttons are not in form tag.. – A. Angel Nov 08 '17 at 19:40
  • i could do it with JS but i dont know how to make a hour, day, timezone function to set Open/close Delivery Hours – A. Angel Nov 08 '17 at 19:48
  • Good work! I think that should get what you need for now. If you could re-edit your answer to include some of the earlier/intermediate steps and accept my answer that would help anyone else coming here with similar questions to find what they need by seeing where we started and how we solved it. Thanks! – Kallmanation Nov 09 '17 at 01:55
  • hi i have problems with Javascript!!, the first time i press a radio button it works but the cart tab reload and remove the .onclick function and enable again the checkout button so javascript is not a solution i guess!.. and idk how to do with $_POST['shipping_method']!. – A. Angel Nov 09 '17 at 22:37