如何在WooCommerce手动创建订单的shop_order页面显示自定义账单字段
WooCommerce手动创建订单页面展示自定义账单字段实现方案
实现逻辑说明
你现有代码仅适配已保存订单的详情页字段展示,要支持手动新建订单场景,需要额外新增字段渲染、字段保存两个环节的逻辑,同时还需要修复原有展示代码的已知问题。
步骤1:修复原有展示代码的问题
原有代码存在两个问题:一是判断非空的逻辑写法错误,二是调用了未定义的$billing_title变量,修复后代码如下:
// 已保存订单详情页展示自定义账单字段 add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 ); add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 ); function my_custom_checkout_field_display_admin_order_meta( $order ){ $billing_complex_address = $order->get_meta('_billing_complex_address'); $billing_complex_name = $order->get_meta('_billing_complex_name'); $billing_complex_address_inside = $order->get_meta('_billing_complex_address_inside'); $billing_suburb = $order->get_meta('_billing_suburb'); // 修复非空判断逻辑,移除未定义的$billing_title输出 if( ! empty( $billing_complex_address ) || ! empty( $billing_complex_name ) ) { echo '<p><strong>'.__('Complex Address').':</strong><br>' . $billing_complex_address .'</p>'; echo '<p><strong>'.__('Complex Name').':</strong><br>' . $billing_complex_name .'</p>'; echo '<p><strong>'.__('Complex Address Inside').':</strong><br>' . $billing_complex_address_inside .'</p>'; echo '<p><strong>'.__('Suburb').':</strong><br>' . $billing_suburb .'</p>'; } }
步骤2:新增手动创建订单页的字段渲染逻辑
通过woocommerce_admin_order_billing_fields钩子将自定义字段插入到新建订单的账单字段组中:
// 手动创建订单页渲染自定义账单字段 add_filter( 'woocommerce_admin_order_billing_fields', 'add_custom_billing_fields_to_admin_create_order', 10, 2 ); function add_custom_billing_fields_to_admin_create_order( $fields, $order ) { $fields['_billing_complex_address'] = array( 'label' => __('Complex Address'), 'show' => true, 'wrapper_class' => 'form-field-wide', 'type' => 'text', ); $fields['_billing_complex_name'] = array( 'label' => __('Complex Name'), 'show' => true, 'wrapper_class' => 'form-field-wide', 'type' => 'text', ); $fields['_billing_complex_address_inside'] = array( 'label' => __('Complex Address Inside'), 'show' => true, 'wrapper_class' => 'form-field-wide', 'type' => 'text', ); $fields['_billing_suburb'] = array( 'label' => __('Suburb'), 'show' => true, 'wrapper_class' => 'form-field-wide', 'type' => 'text', ); return $fields; }
步骤3:新增手动创建订单的字段保存逻辑
提交新建订单时将自定义字段值存入订单元数据:
// 保存手动创建订单的自定义账单字段值 add_action( 'woocommerce_process_shop_order_meta', 'save_custom_billing_fields_from_admin_create_order', 10, 2 ); function save_custom_billing_fields_from_admin_create_order( $order_id, $order ) { if ( isset( $_POST['_billing_complex_address'] ) ) { $order->update_meta_data( '_billing_complex_address', sanitize_text_field( $_POST['_billing_complex_address'] ) ); } if ( isset( $_POST['_billing_complex_name'] ) ) { $order->update_meta_data( '_billing_complex_name', sanitize_text_field( $_POST['_billing_complex_name'] ) ); } if ( isset( $_POST['_billing_complex_address_inside'] ) ) { $order->update_meta_data( '_billing_complex_address_inside', sanitize_text_field( $_POST['_billing_complex_address_inside'] ) ); } if ( isset( $_POST['_billing_suburb'] ) ) { $order->update_meta_data( '_billing_suburb', sanitize_text_field( $_POST['_billing_suburb'] ) ); } $order->save(); }
内容的提问来源于stack exchange,提问作者RawalD
相关产品推荐
相关产品推荐

