禁用所有周末和特定日期,并启用母亲节

时间:2019-05-04 07:53:07

标签: wordpress jquery-ui woocommerce

我的以下代码在functions.php中已经运行良好-我想启用母亲节(2019年5月12日),该日期为星期日。如何将其添加到返回字符串中?

    function custom_adjust_datepicker_range () {
        if ( is_checkout() ) {

    ?>

    <script type="text/javascript">
    var disabledDays = [
     "1-1-2019","1-1-2020","2-1-2019","28-1-2019","27-1-2020","4-3-2019","2-3-2020","19-4-2019","10-4-2020","22-4-2019","13-4-2020","25-4-2019","25-4-2020","27-4-2020","3-6-2019","1-6-2019","30-9-2019","28-9-2020","25-12-2018","25-12-2019","25-12-2020","26-12-2018","26-12-2019","26-12-2020","27-12-2018"
    ];
    jQuery( "#delivery_date" ).datepicker({
        minDate: 2,
  beforeShowDay: function(date) {
    var day = date.getDay();
    var string = jQuery.datepicker.formatDate('d-m-yy', date);
    var isDisabled = (jQuery.inArray(string, disabledDays) != -1);
    return [(day != 1 && day != 0 && !isDisabled), ''];
}
});
    </script>
    <?php
        }
    } // End custom_adjust_datepicker_range()
    add_action( 'wp_footer', 'custom_adjust_datepicker_range', 50 );

1 个答案:

答案 0 :(得分:0)

请考虑以下示例。

jQuery(function() {
  var disabledDays = [
    "1-1-2019", "1-1-2020", "2-1-2019", "28-1-2019", "27-1-2020", "4-3-2019", "2-3-2020", "19-4-2019", "10-4-2020", "22-4-2019", "13-4-2020", "25-4-2019", "25-4-2020", "27-4-2020", "3-6-2019", "1-6-2019", "30-9-2019", "28-9-2020", "25-12-2018", "25-12-2019", "25-12-2020", "26-12-2018", "26-12-2019", "26-12-2020", "27-12-2018"
  ];

  var dtf = 'd-m-yy';

  function getMothersDay(y) {
    var mayFirst = new Date(y, 4, 1);
    var dayOfWeek = mayFirst.getUTCDay();
    var firstSunday;
    if (dayOfWeek == 0) {
      firstSunday = mayFirst;
    } else {
      firstSunday = new Date(y, 4, 1 + (7 - dayOfWeek));
    }
    var mothersDay = new Date(y, 4, firstSunday.getDate() + 7);
    return mothersDay;
  }

  function isMothersDay(dt) {
    return (jQuery.datepicker.formatDate(dtf, dt) == jQuery.datepicker.formatDate(dtf, getMothersDay(dt.getFullYear())));
  }

  jQuery("#delivery_date").datepicker({
    minDate: 2,
    beforeShowDay: function(date) {
      var day = date.getDay();
      var string = jQuery.datepicker.formatDate(dtf, date);
      var isDisabled = jQuery.inArray(string, disabledDays);
      var result = [
        true,
        "",
        ""
      ];
      switch (true) {
        case isMothersDay(date):
          // Mother's Day
          result = [
            true,
            "mothers-day",
            "Mother's Day"
          ];
          break;
        case (isDisabled >= 0):
          // Disable Days
          result[0] = false;
          break;
        case (day == 0):
        case (day == 6):
          // Weekends
          result[0] = false;
          break;
      }
      return result;
    }
  });
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<p>Delivery Date: <input type="text" id="delivery_date"></p>

查看更多:How to calculate Mother’s Day in JavaScript

此示例基于我执行的第一个Google搜索结果。将来,您可能需要搜索基本示例。

这不适用于WordPress,因此您需要根据自己的需要修改代码。

您有3个基本检查:

  • 是母亲节的日子
  • 是日期数组中的日期
  • 该日期是周末吗

您可能还会出现其他情况,因此总体上使用switch()可能会更容易。我从最不常见的情况转到最常见的情况。您可以添加更多内容,并且仅检查当月是五月,并且它们是在前两周之内的母亲节吗:(date.getMonth() == 4 && date.getDate() < 15)

希望这会有所帮助。