php - Woocommerce:选择日期选择器时如何触发操作
问题描述
我在 woocommerce 结帐页面中添加了 2 个日期选择器字段。现在我想执行一个脚本来计算两个 datepicker 字段值之间的天数。
但是在选择了两个日期选择器并且变量“order_pickup_date”和“order_pickup_EndDate”确实有值之后,我正在努力调用计算脚本的基础知识。
这是 2 个日期选择器字段的代码:
// Call datepicker functionality in custom text field
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field', 10, 1);
function my_custom_checkout_field( $checkout ) {
date_default_timezone_set('Europe/Berlin');
$mydateoptions = array('' => __('Select PickupDate', 'woocommerce' ));
echo '<div id="my_custom_checkout_field">
<h3>'.__('Date pre-order').'</h3>';
echo '
<script>
jQuery(function($){
$("#datepicker").datepicker( {
minDate:1,
beforeShowDay: function(date) {
var show = true;
if(date.getDay()==1||date.getDay()==1 ) show=false
return [show];
}
} )});
</script>';
echo '
<script>
jQuery(function($){
$("#datepickerEnd").datepicker( {
minDate:($("#order_pickup_date")+3),
beforeShowDay: function(date) {
var show = true;
if(date.getDay()==1) show=false
return [show];
}} )});
</script>';
woocommerce_form_field( 'order_pickup_date', array(
'type' => 'text',
'class' => array('form-row-first my-datepicker'),
'id' => 'datepicker',
'required' => true,
'label' => __('Start pre-order'),
'placeholder' => __('Select Date'),
'options' => $mydateoptions
),$checkout->get_value( 'order_pickup_date' ));
woocommerce_form_field( 'order_pickup_EndDate', array(
'type' => 'text',
'class' => array('form-row-first my-datepicker'),
'id' => 'datepickerEnd',
'required' => true,
'label' => __('Ende pre-order'),
'placeholder' => __('Select Date'),
'options' => $mydateoptions
),$checkout->get_value( 'order_pickup_EndDate' ));
echo '</div>';
}
现在我想在两个日期选择器都有值之后调用下面的脚本:
function wnd_checkout_code( ) {
$from= $_POST['order_pickup_date'];
$to=$_POST['order_pickup_EndDate'];
$start='Sunday';
$end='Monday';
$day_num=['Monday'=>1,'Tuesday'=>2,'Wednesday'=>3,'Thursday'=>4,'Friday'=>5,'Saturday'=>6,'Sunday'=>7];
$start_num=$day_num[$start];
$end_num=$day_num[$end];
$period=new DatePeriod(date_create($from),date_interval_create_from_date_string('1 days'),date_create($to));
$nRange=0;
if($start_num===$end_num){
$valid_range=array_values($day_num);//keep the number of each day of the week
}
elseif($start_num>$end_num){
$valid_range=array_values($day_num);//keep the number of each day of the week
while($valid_range[0]!==$start_num){//rotate the values to keep the start day first
$pop=array_shift($valid_range);
array_push($valid_range,$pop);
}
$valid_range=array_slice($valid_range,array_search($start_num,$valid_range),$start_num-$end_num);//keep only the days in the chosen range
}else{
$valid_range=array_values($day_num);//keep the number of each day of the week
$valid_range=array_slice($valid_range,array_search($start_num,$valid_range),$end_num-$start_num); //keep only the days in the chosen range
}
$valid_range=array_flip($valid_range);//flip the array to make the search more fast
foreach($period as $date){
if(isset($valid_range[(int)$date->format('N')])){
$nRange++;
}
}
print_r($nRange+1);//output the number of valid days
}
你能帮我吗,因为我是一个初学者。
解决方案
推荐阅读
- babeljs - 捆绑的所有组件
- python - 有没有办法使用 Stripe API 批量删除客户列表?
- vue.js - Vue html 渲染添加了额外的双引号
- dialogflow-es - 捕获对话流建议芯片响应
- python - 在加权图中找到每个节点的输出边数
- itext - 透明度组软蒙版和PNG图像蒙版的错误组合
- r - R中的正则表达式:在字符串开头提取一个单词直到一个特殊字符
- r - 使用`lapply`和`grepl`修剪变量内的嵌套列表
- php - PHP / Angular-为什么在用@if语句包围HTML之后我的视图中出现了意想不到的'<'?
- r - 如何将 data.table 转换为矩阵?