我在 woocommerce 中有 3 个自定义字段:
案件参考编号、发票编号和律师处理案件(代码接下来,如果您不想看到它只需向下滚动,它并不那么重要)
add_action('woocommerce_after_order_notes', 'matter_reference_number_func');
function matter_reference_number_func( $checkout ) {
echo '<div id="matter_ref"><h3>'.__('Matter reference number').'</h3>';
woocommerce_form_field( 'matter_reference_number', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'placeholder' => __('Matter reference number'),
), $checkout->get_value( 'matter_ref_num' ));
echo '</div>';
}
add_action('woocommerce_after_order_notes', 'invoice_number_func');
function invoice_number_func( $checkout ) {
echo '<div id="inv_num"><h3>'.__('Invoice number').'</h3>';
woocommerce_form_field( 'invoice_number', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'placeholder' => __('Invoice number'),
), $checkout->get_value( 'invoice_num' ));
echo '</div>';
}
add_action('woocommerce_after_order_notes', 'sol_deal_func');
function sol_deal_func( $checkout ) {
echo '<div id="sol_deal"><h3>'.__('Solicitor dealing with matter').'</h3>';
woocommerce_form_field( 'matter_reference_number', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'placeholder' => __('Solicitor dealing with matter'),
), $checkout->get_value( 'sol_deal' ));
echo '</div>';
}
但是,现在当我尝试保存值时,它们不会出现在发给客户的电子邮件中,也不会出现在管理员中,甚至不会出现在确认页面上。根据文档,这应该足以做到这一点;但它不是。
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'matter_ref_num_checkout_field_update_order_meta');
function matter_ref_num_checkout_field_update_order_meta( $order_id ) {
if ($_POST['matter_ref_num']) update_post_meta( $order_id, 'Matter Reference Number', esc_attr($_POST['matter_ref_num']));
}
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'invoice_num_checkout_field_update_order_meta');
function invoice_num_checkout_field_update_order_meta( $order_id ) {
if ($_POST['invoice_num']) update_post_meta( $order_id, 'Invoice Number', esc_attr($_POST['invoice_num']));
}
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'sol_deal_checkout_field_update_order_meta');
function sol_deal_checkout_field_update_order_meta( $order_id ) {
if ($_POST['sol_deal']) update_post_meta( $order_id, 'Solicitor Dealing With Matter', esc_attr($_POST['sol_deal']));
}