首页 > 解决方案 > 我想从 WooCommerce 管理员编辑页面编辑结帐字段

问题描述

我在 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); 

在此处输入图像描述

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

在此处输入图像描述

标签: wordpresswoocommerce

解决方案


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

使用 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 );

推荐阅读