gpt4 book ai didi

php - 在 woocommerce 结帐页面自定义字段中添加日期

转载 作者:行者123 更新时间:2023-12-02 20:53:10 25 4
gpt4 key购买 nike

我正在尝试在 woocommerce 结帐页面中添加自定义选择选项。它正在添加额外的字段,但我想在选择选项的值中添加日期。

有什么解决办法吗?

这是我在主题 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>';
}

最佳答案

使用date()strtotime(),您可以设置选项,如下所示:

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 格式保存日期。我在 customizing the checkout fields 上写了一篇教程您可能会发现有用。

关于php - 在 woocommerce 结帐页面自定义字段中添加日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41464207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com