我找到了一种方法,可以手动将订单详细信息上的付款日期添加到WooCommerce的后台. 我已经在账单地址中创建了一个字段,我找到了一个日期拾取器的方法. 一切工作正常:我能够编辑帐单字段和打开日期 Select 器.

我的自定义代码:

// Edit Order details to add payment date
add_action( 'woocommerce_admin_order_data_after_billing_address', 'misha_editable_order_meta_billing' );
function misha_editable_order_meta_billing( $order ){
    $billingdate = $order->get_meta( 'billingdate' );
    ?>
        <div class="address">
            <p<?php if( empty( $billingdate ) ) { echo ' class="none_set"'; } ?>>
                <strong>Date deadline:</strong>
                <?php echo ! empty( $billingdate ) ? $billingdate : 'Anytime.' ?>
            </p>
        </div>
        <div class="edit_address">
            <?php
                woocommerce_wp_text_input( array(
                    'id' => 'paymentdate',
                    'label' => 'Date deadline',
                    'wrapper_class' => 'form-field-wide',
                    'class' => 'date-picker',
                    'style' => 'width:100%',
                    'value' => $billingdate,
                    'description' => 'Payment date.'
                ) );
            ?>
        </div>
    <?php
}

add_action( 'woocommerce_process_shop_order_meta', 'misha_save_general_details' );
function misha_save_general_details( $order_id ){
    update_post_meta( $order_id, 'billingdate', wc_clean( $_POST[ 'billingdate' ] ) );
}

当我点击"更新"按钮时,我可以编辑这个字段,but I cannot save the field value.

有人能告诉我出什么事了吗?如何保存此字段值?

推荐答案

我已经完全重温了你的代码...

  • 首先,我已将您的"帐单付款"meta key重命名为WooCommerce帐单元密钥约定.
  • 此外,随着WooCommerce逐步迁移到定制表,我已经改变了您try 保存该字段的方式.

代码:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'add_admin_order_editable_billing_date' );
function add_admin_order_editable_billing_date( $order ){
    $billing_date    = $order->get_meta( '_billing_date' );
    $billing_date_fr = (new WC_DateTime($billing_date))->date('d/m/Y');

    printf('<div class="address"><p%s><strong>%s:</strong> %s</p></div>
        <div class="edit_address">', 
        !$billing_date ? ' class="none_set"' : '',
        __('Date deadline', 'woocommerce'),
        $billing_date ? $billing_date_fr : __('Anytime.', 'woocommerce') 
    );

    woocommerce_wp_text_input( array(
        'id'            => '_billing_date',
        'label'         => 'Date deadline (payment date)',
        'wrapper_class' => 'form-field-wide',
        'class'         => 'date-picker',
        'style'         => 'width:100%',
        'value'         => $billing_date,
    ) );

    echo '</div>';
}

add_action( 'woocommerce_process_shop_order_meta', 'save_admin_order_billing_date' );
function save_admin_order_billing_date( $order_id ){
    if ( isset($_POST[ '_billing_date' ]) ) {
        $order = wc_get_order( $order_id );
        $order->update_meta_data( '_billing_date', wc_clean( wp_unslash( $_POST[ '_billing_date' ] ) ) );
        $order->save();
    }
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.

Php相关问答推荐

PHP在将数组子集赋给变量时是否复制数组子集?

Yahoo Finance API文件_GET_CONTENTS 429请求太多

我需要多个SELECT语句的结果(使用由php ECHO创建的表中显示的Limit(X)

仅在WooCommerce管理订单视图上显示订单项自定义元数据

Laravel关系-为用户获取属于组织的所有团队

在Laravel中修改JSON输出

在PHP中处理Linux输入事件(/dev/input/Event*)

如何配置我的.env文件以在laravel的一个项目中连接两个不同的数据库?

使用简码在 WooCommerce 我的帐户中显示一些用户数据

在 PHP MySQL 中自动生成账单编号

Font Awesome 加载图标未显示在 WooCommerce 管理员列表中

laravel Eloquent 模型的问题

将一个表中的行连接为不同表MYSQL中的一列

过滤会员计划标题和标签

使用 phpseclib 验证 RSA-PSS

致命错误:未捕获 Google_Exception:客户端机密 JSON 文件无效.在 /somepath/lib/vendor/google/apiclient/src/Google/Client.php

根据所选付款方式更改 WooCommerce 提交结帐按钮文本

如何在不解压缩/重新压缩的情况下连接(连接)两个缩小的值

如何在字符串constant-string-NUMBER-*中获取 NUMBER?

使用 OOP PHP 创建动态 WHERE 子句.如何实现 BETWEEN 运算符?