[英]Add date in woocommerce checkout page custom field
I am trying to add custom select option in woocommerce checkout page.我正在尝试在 woocommerce 结帐页面中添加自定义选择选项。 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?有什么解决办法吗?
Here is the code I added in my theme function.php这是我在我的主题 function.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>';
}
Using date()
and strtotime()
you can set the options
as follows:使用
date()
和strtotime()
您可以设置如下options
:
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>';
}
This will allow you to later on save dates as in the format YYYY-MM-DD .这将允许您稍后以YYYY-MM-DD格式保存日期。 I wrote a tutorial on customizing the checkout fields that you may find useful.
我写了一个关于自定义结账字段的教程,您可能会发现它很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.