我在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>';
}
现在,当我尝试保存这些值时,它们不会出现在客户端,管理员,甚至是确认页面的电子邮件中。根据{{3}},这应该足以做到这一点;但它不是。
/**
* 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']));
}
答案 0 :(得分:6)
您正在使用钩子更新帖子元(作为订单ID的帖子ID)。如果您检查数据库,您应该能够在相应的帖子ID元下看到这些字段。
如果你想将它们添加到电子邮件中,你还需要使用另一个钩子:
woocommerce_email_order_meta_keys
以下是woocommerce文档的参考资料:
/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {
$keys[] = 'My Field';
return $keys;
}
你真的只需要为每个动作使用一个钩子并将你的代码放在那里,例如:
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta');
function custom_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']));
if ($_POST['invoice_num']) update_post_meta( $order_id, 'Invoice Number', esc_attr($_POST['invoice_num']));
if ($_POST['sol_deal']) update_post_meta( $order_id, 'Solicitor Dealing With Matter', esc_attr($_POST['sol_deal']));
}
希望这有帮助。
答案 1 :(得分:0)
接受的答案对我不起作用。我不得不删除 update_post_meta 函数的第二个参数:
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_fields_update_order_meta' );
function custom_checkout_fields_update_order_meta( $order_id ) {
if ($_POST['_billing_vat_number']) update_post_meta( $order_id, '_billing_vat_number', sanitize_text_field( $_POST['_billing_vat_number'] ) );
}
我还使用了 wordpress 的清理功能,但这不是问题。