【问题标题】:wrong date appearance as custom field错误的日期外观为自定义字段
【发布时间】:2021-04-01 01:00:38
【问题描述】:

我只是尝试根据thisthis 线程向 WooCommerce 结帐流程添加“交货日期”。

这是在订单元数据上打印交货日期并在感谢页面和管理订单页面上查看的相关代码:

//Shipping (Delivery) Date


// Add custom checkout datepicker field

add_action( 'woocommerce_before_order_notes', 'checkout_display_datepicker_custom_field' );
function checkout_display_datepicker_custom_field( $checkout ) {
    $field_id = 'my_datepicker';

    echo '<div id="datepicker-wrapper">';

    $today = strtotime('today');
    $tomorrow = strtotime('tomorrow');
    $dayAfterTomorrow = strtotime('+2 days');

    woocommerce_form_field(  $field_id, array(
        'type'          => 'select',
        'class'         => array('my-field-class form-row-wide'),
        'label' => __('Choose a date'),
        'placeholder'   => __('Select delivery date'),
        'required' => true, // Or false
        'options'     => array(
            '' => 'Select',
            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 '<br></div>';

}



// Save field

add_action( 'woocommerce_checkout_create_order', 'save_datepicker_custom_field_value', 10, 2 );
function save_datepicker_custom_field_value( $order, $data ){
    $field_id = 'my_datepicker';
    $meta_key = '_'.$field_id;

    if ( isset($_POST[$field_id]) && ! empty($_POST[$field_id]) ) {
        $date = esc_attr($_POST[$field_id]);
        
        $order->update_meta_data( $meta_key, $date ); // Save date as order meta data
        
        $note = sprintf(__("Chosen date for Thank you page: %s.", "woocommerce"), $date );
        $note = isset($data['order_comments']) && ! empty($data['order_comments']) ? $data['order_comments'] . '. ' . $note : $note;
        
        // Save date on customer order note
        $order->set_customer_note( $note );

    }
}

它打印选择的日期,但格式错误,例如:20202020-1212-2424

如何修改这个错误?

【问题讨论】:

    标签: php date woocommerce action


    【解决方案1】:

    您在 date() 函数中使用了错误的格式。 看看date/time format docs。

    在你的情况下,yyyy-mm-dd:

    • y 年份的两位数表示
    • m 月份的数字表示,前导零
    • d 日期,2 位数字,前导零

    因此,20202020-1212-2424

    你的意思可能是2020-12-24,应该是Y-m-d

    • Y 年份的完整数字表示,4 位数字

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 2019-10-04
      • 2013-02-17
      • 2021-04-04
      • 1970-01-01
      相关资源
      最近更新 更多