我在 Woocommerce 结帐页面的帐单地址中添加了自定义字段,效果很好。 现在,我想在电子邮件的“帐单地址”框中以及“我的帐户”的订单摘要页面上显示这些字段自定义字段的值。 这是我正在使用的代码,它工作正常,但我唯一缺少的是将字段定位在电子邮件和订单摘要视图中,并使管理页面中的字段可编辑(如 woocomerce 中的其他计费字段) 。 是否有挂钩或方法可以显示“帐单地址”框中这些自定义字段的值? 我做不到,如果你帮助我,我将非常感激
创建自定义字段
add_filter( 'woocommerce_checkout_fields', 'custom_fields_checkout' );
function custom_fields_checkout( $fields ) {
$fields['billing']['codice_fiscale'] = array(
'label' => 'Codice fiscale',
'type' => 'text',
'required' => true,
'class' => array( 'form-row-last' ),
'priority' => 30,
);
$fields['billing']['partita_iva'] = array(
'label' => 'Partita iva',
'type' => 'text',
'required' => false,
'class' => array( 'form-row-first' ),
'priority' => 31,
);
$fields['billing']['codice_univoco'] = array(
'label' => 'Codice Univoco',
'type' => 'text',
'required' => false,
'class' => array( 'form-row-last' ),
'priority' => 32,
);
return $fields;
}
保存字段
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['codice_fiscale'] ) ) {
update_post_meta( $order_id, 'codice_fiscale', sanitize_text_field( $_POST['codice_fiscale'] ) );
}
if ( ! empty( $_POST['partita_iva'] ) ) {
update_post_meta( $order_id, 'partita_iva', sanitize_text_field( $_POST['partita_iva'] ) );
}
if ( ! empty( $_POST['codice_univoco'] ) ) {
update_post_meta( $order_id, 'codice_univoco', sanitize_text_field( $_POST['codice_univoco'] ) );
}
}
显示订单管理页面中的字段
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_field_on_order_edit_pages', 10, 1 );
function display_custom_field_on_order_edit_pages( $order ){
if($codice_fiscale = get_post_meta( $order->get_id(), 'codice_fiscale', true ));
echo '<p><strong>Codice fiscale: </strong>' . $codice_fiscale . '</p>';
if($partita_iva = get_post_meta( $order->get_id(), 'partita_iva', true ));
echo '<p><strong>Partita iva: </strong>' . $partita_iva .'</p>';
if($codice_univoco = get_post_meta( $order->get_id(), 'codice_univoco', true ));
echo '<p><strong>Codice univoco: </strong>' . $codice_univoco . '</p>';
}
显示电子邮件中的字段
add_action( 'woocommerce_email_after_order_table', 'ts_email_after_order_table', 10, 4 );
function ts_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ) {
if($codice_fiscale = get_post_meta( $order->get_id(), 'codice_fiscale', true ));
echo '<p><strong>Codice fiscale: </strong>' . $codice_fiscale . '</p>';
if($partita_iva = get_post_meta( $order->get_id(), 'partita_iva', true ));
echo '<p><strong>Partita iva: </strong>' . $partita_iva .'</p>';
if($codice_univoco = get_post_meta( $order->get_id(), 'codice_univoco', true ));
echo '<p><strong>Codice univoco: </strong>' . $codice_univoco . '</p>';
}
测试修改后的代码,我哪里错了?
function add_custom_billing_fields( $fields ) {
$fields['billing_codice_fiscale'] = array(
'label' => ( 'Codice fiscale'),
'type' => 'text',
'required' => true,
'class' => array( 'form-row-last' ),
'priority' => 30,
);
$fields['billing_partita_iva'] = array(
'label' => 'Partita iva',
'type' => 'text',
'required' => false,
'class' => array('form-row-wide'),
'priority' => 31,
);
$fields['billing_codice_univoco'] = array(
'label' => 'Codice Univoco',
'type' => 'text',
'required' => false,
'class' => array( 'form-row-wide' ),
'priority' => 32,
);
return $fields;
}
add_filter( 'woocommerce_billing_fields', 'add_custom_billing_fields' );
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['billing_codice_fiscale'] ) ) {
update_post_meta( $order_id, 'billing_codice_fiscale', sanitize_text_field( $_POST['billing_codice_fiscale'] ) );
}
if ( ! empty( $_POST['billing_partita_iva'] ) ) {
update_post_meta( $order_id, 'billing_partita_iva', sanitize_text_field( $_POST['billing_partita_iva'] ) );
}
if ( ! empty( $_POST['billing_codice_univoco'] ) ) {
update_post_meta( $order_id, 'billing_codice_univoco', sanitize_text_field( $_POST['billing_codice_univoco'] ) );
}
}
尝试以下完全重新审视的代码(优化且完整):
// Utility function: Here are your fields settings
function get_custom_billing_field_settings(){
return array(
'fiscal_code' => array(
'label' => __('Codice fiscale', 'woocommerce'),
'required' => true,
'class' => 'last'
),
'vat_number' => array(
'label' => __('Partita iva', 'woocommerce'),
'required' => false,
'class' => 'first'
),
'unique_code' => array(
'label' => __('Codice Univoco', 'woocommerce'),
'required' => false,
'class' => 'last'
),
);
}
// Display fields on checkout (and in My account > Edit Billing Address)
add_filter( 'woocommerce_billing_fields', 'add_custom_billing_fields' );
function add_custom_billing_fields( $fields ) {
$fields['billing_company']['class'] = array( 'form-row-first' );
$priority = 30;
foreach( get_custom_billing_field_settings() as $key => $values ) {
$priority++;
$fields[$key] = array(
'label' => $values['label'],
'type' => 'text',
'required' => $values['required'],
'class' => array( 'form-row-'.$values['class'] ),
'priority' => $priority,
);
}
return $fields;
}
// Save custom fields data to the order
add_action( 'woocommerce_checkout_create_order', 'save_custom_billing_fields_order_meta', 10, 2 );
function save_custom_billing_fields_order_meta( $order, $data ) {
foreach( get_custom_billing_field_settings() as $key => $values ) {
if ( isset($data[$key]) && ! empty($data[$key]) ) {
$order->add_meta_data('_'.$key, sanitize_text_field($data[$key]) );
}
}
}
// Save custom fields data to user metadata
add_action( 'woocommerce_checkout_update_customer', 'save_custom_billing_fields_user_meta', 10, 2 );
function save_custom_billing_fields_user_meta( $customer, $data ) {
foreach( get_custom_billing_field_settings() as $key => $values ) {
if ( isset($data[$key]) && ! empty($data[$key]) ) {
$customer->add_meta_data($key, sanitize_text_field($data[$key]) );
}
}
}
// Display custom fields data on customer orders and admin orders
add_action('woocommerce_after_order_details', 'display_event_data_on_orders', 10, 1);
add_action('woocommerce_admin_order_data_after_billing_address', 'display_event_data_on_orders', 10, 1 );
function display_event_data_on_orders( $order ) {
$fiscal_data = array();
foreach( get_custom_billing_field_settings() as $key => $data ) {
if( $value = $order->get_meta('_'.$key) ) {
$fiscal_data[$key] = array( 'label' => $data['label'], 'value' => $value );
}
}
if ( ! empty($fiscal_data) ) {
$title = __('Dettagli fiscali', 'woocommerce');
// On admin orders
if( is_admin() ) {
foreach ( $fiscal_data as $key => $values ) {
echo '<strong>' . $values['label'] .':</strong> ' . $values['value'] . '<br>';
}
echo '</p>';
}
// On customer orders and thankyou
else {
echo '<h2 class="woocommerce-column__title">'.$title.'</h2>
<table class="woocommerce-table woocommerce-table--fiscal-details shop_table fiscal_details"><tfoot>';
foreach ( $fiscal_data as $key => $values ) {
echo '<tr class="fiscal-data"><th>'.$values['label'].'</th><td>'.$values['value'].'</td></tr>';
}
echo '</tfoot></table>';
}
}
}
// Display custom fields data on email notifications
add_action('woocommerce_email_after_order_table', 'display_fiscal_data_on_email_notifications', 10, 1);
function display_fiscal_data_on_email_notifications( $order ) {
$fiscal_data = array();
foreach( get_custom_billing_field_settings() as $key => $data ) {
if( $value = $order->get_meta('_'.$key) ) {
$fiscal_data[$key] = array( 'label' => $data['label'], 'value' => $value );
}
}
if ( ! empty($fiscal_data) ) {
$title = __('Dettagli fiscali', 'woocommerce');
echo '<style>
.fiscal-info table{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
.fiscal-info table th, table.fiscal-info td{text-align: left; border-top-width: 4px;
color: #737373; border: 1px solid #e4e4e4; padding: 12px; width:58%;}
.fiscal-info table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
</style>';
echo '<div class="fiscal-info"><h2>'.$title.'</h2>';
echo '<table cellspacing="0" cellpadding="6"><tbody>';
foreach ( $fiscal_data as $label => $values ) {
echo '<tr"><th>'.$values['label'].'</th><td>'.$values['value'].'</td></tr>';
}
echo '</tbody></table></div><br>';
}
}
代码位于活动子主题(或活动主题)的functions.php 文件中。已测试并有效。