我在此代码中有一个错误(按顺序添加编辑页面可编辑的自定义结算字段):
add_filter(‘woocommerce_admin_billing_fields’,‘order_admin_custom_fields’);功能……
问题是 $theorder 没有定义,你不能使用 get_id() 方法就可以了。
$theorder
get_id()
但是,代码中的主要问题首先来自结帐帐单字段。应按如下设置,显示和保存它们 (特别照顾 meta_keys 在保存数据时使用) :
meta_keys
// Frontend: Display the custom billing fields (in checkout and my account) add_filter( 'woocommerce_billing_fields' ,'add_custom_billing_fields', 20, 1 ); function add_custom_billing_fields( $fields ) { $fields['billing_address_3'] = array( 'label' => __( 'Home', 'woocommerce' ), 'placeholder' => _x('Fill in your Home', 'placeholder', 'woocommerce'), 'required' => true, 'class' => array('form-row-wide'), 'clear' => true ); $fields['billing_address_4'] = array( 'label' => __( 'Entrance', 'woocommerce' ), 'placeholder' => _x('Fill in your Entrance', 'placeholder', 'woocommerce'), 'required' => true, 'class' => array('form-row-wide'), 'clear' => true ); $fields['billing_address_5'] = array( 'label' => __( 'Floor', 'woocommerce' ), 'placeholder' => _x('Fill in your Floor', 'placeholder', 'woocommerce'), 'required' => true, 'class' => array('form-row-wide'), 'clear' => true ); return $fields; } // Save the custom billing fields (once order is placed) add_action( 'woocommerce_checkout_create_order', 'save_custom_billingt_fields', 20, 2 ); function save_custom_billingt_fields( $order, $data ) { if ( isset( $_POST['billing_address_3'] ) && ! empty( $_POST['billing_address_3'] ) ) { $order->update_meta_data('_billing_address_3', sanitize_text_field( $_POST['billing_address_3'] ) ); update_user_meta( $order->get_customer_id(), 'billing_address_3', sanitize_text_field( $_POST['billing_address_3'] ) ); } if ( isset( $_POST['billing_address_4'] ) && ! empty( $_POST['billing_address_4'] ) ) { $order->update_meta_data('_billing_address_4', sanitize_text_field( $_POST['billing_address_4'] ) ); update_user_meta( $order->get_customer_id(), 'billing_address_4', sanitize_text_field( $_POST['billing_address_4'] ) ); } if ( isset( $_POST['billing_address_5'] ) && ! empty( $_POST['billing_address_5'] ) ) { $order->update_meta_data('_billing_address_5', sanitize_text_field( $_POST['billing_address_5'] ) ); update_user_meta( $order->get_customer_id(), 'billing_address_5', sanitize_text_field( $_POST['billing_address_5'] ) ); } }
您会看到这些自定义字段也在我的帐户中>地址>修改帐单邮寄地址相关的一切都是自动同步。无需额外验证或保存代码......
现在在管理员订单页面中,用于 woocommerce_admin_billing_fields 管理员钩, 'value' 字段数组中不存在键 的 这是有罪的 强> 。
woocommerce_admin_billing_fields
'value'
由于您已正确设置和保存自定义结帐字段,因此您不需要任何其他字段 'value' 数组键,因为数据将是 的 自动填充 强> ,如果它存在。所以你的代码将是:
// Backend: Display editable custom billing fields add_filter( 'woocommerce_admin_billing_fields' , 'order_admin_custom_fields' ); function order_admin_custom_fields( $fields ) { global $the_order; $fields['address_3'] = array( 'label' => __( 'Home', 'woocommerce' ), 'show' => true, 'wrapper_class' => 'form-field-wide', 'style' => '', ); $fields['address_4'] = array( 'label' => __( 'Entrance', 'woocommerce' ), 'show' => true, 'wrapper_class' => 'form-field-wide', 'style' => '', ); $fields['address_5'] = array( 'label' => __( 'Floor', 'woocommerce' ), 'show' => true, 'wrapper_class' => 'form-field-wide', 'style' => '', ); return $fields; }
代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。
错误现在肯定消失了