【问题标题】:Add date in woocommerce checkout page custom field在 woocommerce 结帐页面自定义字段中添加日期
【发布时间】:2017-01-04 12:46:36
【问题描述】:

我正在尝试在 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>';
}

【问题讨论】:

  • 你能详细说明一下吗?我不太明白你在问什么?您发布的代码有什么问题?
  • 我希望选项值“今天”是今天的日期。
  • 好的,所以你需要把日期放在options数组中。 'key' 是 current “Today”... 这可能是您要保存在数据库中的内容,而数组值是页面上显示的内容。另请参阅[date()](php.net/manual/en/function.date.php) 函数。
  • 另外,请参阅this,因为它非常相似。

标签: php wordpress datetime woocommerce e-commerce


【解决方案1】:

使用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>';
}

这将允许您稍后以 YYYY-MM-DD 格式保存日期。我在customizing the checkout fields 上写了一篇教程,您可能会觉得很有用。

【讨论】:

    猜你喜欢
    • 2019-04-01
    • 2018-05-15
    • 2021-11-18
    • 2012-09-19
    • 2021-04-30
    • 2021-05-30
    • 2019-08-01
    • 2016-07-29
    • 1970-01-01
    相关资源
    最近更新 更多