dqfaom241360 2017-01-04 12:46
浏览 48
已采纳

在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?

Here is the code I added in my theme 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>';
}
  • 写回答

1条回答 默认 最新

  • dttvb115151 2017-01-04 15:57
    关注

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 thymeleaf 动态列头 渲染
  • ¥15 matlab透明图叠加
  • ¥50 基于stm32l4系列 使用blunrg-ms的ble gatt 创建 hid 服务失败
  • ¥15 关于#服务器#的问题:在VMware虚拟机中有一个域控服务器,克隆这个服务器后,使用克隆在另外一台服务器VMware虚拟机上打开,修改IP地址, 原域控服务器关闭
  • ¥150 计算DC/DC变换器平均模型中的参数mu
  • ¥25 C语言代码,大家帮帮我
  • ¥15 请问以下文字内容及对应编码是用了什么加密算法或压缩算法呢?
  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间