在woocommerce结帐页面自定义字段中添加日期 [英] Add date in woocommerce checkout page custom field
本文介绍了在woocommerce结帐页面自定义字段中添加日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在woocommerce结帐页面中添加自定义选择选项.它正在添加额外的字段,但我想在选择选项的值中添加日期.
I am trying to add custom select option in woocommerce checkout page. It is adding the extra field but I want to add the date in the value of the select option.
对此有什么解决办法吗?
Is there any solution for this?
这是我在主题函数中添加的代码.php
$today = new DateTime();
$tomorrow = new DateTime();
$tomorrow->modify('+1 day');
$dayAfterTomorrow = new DateTime();
$dayAfterTomorrow->modify('+2 day');
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __(''),
'options' => array(
'Today' => __("This should be today's date"),
'Tomorrow' => __('This should be tomorrow date'),
'Day After Tomorrow' => __('This should be Day After Tomorrow Date')
)), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
推荐答案
使用 date()
和 strtotime()
,您可以设置选项
如下:
Using date()
and strtotime()
you can set the options
as follows:
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';
$today = strtotime('today');
$tomorrow = strtotime('tomorrow');
$dayAfterTomorrow = strtotime('+2 days');
woocommerce_form_field( 'my_field_name', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __(''),
'options' => array(
date( 'yyyy-mm-dd', $today ) => date( get_option('date_format'), $today ),
date( 'yyyy-mm-dd', $tomorrow ) => date( get_option('date_format'), $tomorrow ),
date( 'yyyy-mm-dd', $dayAfterTomorrow ) => date( get_option('date_format'), $dayAfterTomorrow ),
)));
echo '</div>';
}
这将使您以后以 YYYY-MM-DD 格式保存日期.我在自定义结帐字段上写了一篇您可能会觉得有用的教程.
This will allow you to later on save dates as in the format YYYY-MM-DD. I wrote a tutorial on customizing the checkout fields that you may find useful.
这篇关于在woocommerce结帐页面自定义字段中添加日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文