【问题标题】:I want to edit the checkout field from the WooCommerce admin edit page我想从 WooCommerce 管理员编辑页面编辑结帐字段
【发布时间】:2021-05-04 13:10:00
【问题描述】:

我在 WooCommerce 结帐页面上添加了一些自定义字段。我已经保存了这些字段的数据并将其显示在管理订单页面上。就像图片 1 一样。一切正常。

现在我想编辑该字段的数据/文本/值。就像图片2一样。就像管理订单页面上的计费和运输一样......

我该如何解决?

function display_admin_order_meta ( $order ) {
$strt= $order->get_meta('_checkout_custom_name', true );
if( ! empty( $strt) ){
    $label = __( 'Phone' );
    if( is_admin() ){ 
        echo '<p><strong>' . $label . ' : </strong> ' . $strt. '</p>';
    }
    else {
        echo '<table class="woocommerce-table"><tbody><tr>
            <th>' . $label . ' : </th><td>' . $strt. '</td>
        </tr></tbody></table>';
    }
}

$strt= $order->get_meta('_checkout_others_address', true );
if( ! empty( $strt) ){
    $label = __( 'Address' );
    if( is_admin() ){ // Admin
        echo '<p><strong>' . $label . ' : </strong> ' . $strt. '</p>';
    }
    else {
        echo '<table class="woocommerce-table"><tbody><tr>
            <th>' . $label . ' : </th><td>' . $strt. '</td>
        </tr></tbody></table>';
    }
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_admin_order_meta', 20, 1); 

这是我创建的结帐字段数据。我想像上图一样编辑这些。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    这是一种方式(如果需要,可以自定义)

    使用 woocommerce_admin_billing_fields 过滤器添加自定义计费表单数据输入

    add_filter('woocommerce_admin_billing_fields', 'add_woocommerce_admin_billing_fields');
    function add_woocommerce_admin_billing_fields($billing_fields) {
        $billing_fields['_checkout_custom_name'] = array( 'label' => __('Name', 'woocommerce') );
        $billing_fields['_checkout_others_address'] = array( 'label' => __('Address', 'woocommerce') );
    
        return $billing_fields;
    }
    

    然后像这样保存自定义字段

    function cloudways_save_extra_details( $post_id, $post ){
        update_post_meta( $post_id, '_checkout_custom_name', $_POST[ '_checkout_custom_name' ] ) );
        update_post_meta( $post_id, '_checkout_custom_address', $_POST[ '_checkout_custom_address' ] ) );
    }
    add_action( 'woocommerce_process_shop_order_meta', 'cloudways_save_extra_details', 45, 2 );
    

    【讨论】:

    • 谢谢。首先,我不想添加任何新字段。我只想编辑从结帐页面字段中保存的数据并将其显示在管理面板中。其次,我不想在计费字段内进行编辑。我想编辑订单页面上的数据。
    • 在定义woocommerce_admin_order_data_after_billing_address钩子的输入字段中显示自定义数据,然后像stackoverflow.com/questions/45833977/…一样保存订单
    • 那很好。但我的想法有点不同。您提供的代码仅适用于 Amin 面板。在我的帐户页面或其他任何地方显示其输出的方式是什么?
    猜你喜欢
    • 1970-01-01
    • 2020-08-07
    • 2020-02-15
    • 2021-06-12
    • 1970-01-01
    • 2018-10-01
    • 2019-01-14
    • 2018-10-19
    • 2019-01-28
    相关资源
    最近更新 更多