我在结帐页面上为 Woocommerce 创建了一些自定义字段。我可以在WP管理区域,电子邮件和发票中显示它们,但不能在“用户仪表板”的前端显示它们。
这是我用来在function.php
文件的CHILD-THEM中创建“ CODICE SNEP”字段的代码:
// Create hook - CODICE SNEP
add_filter( 'woocommerce_checkout_fields' , 'codice_snep' );
function codice_snep ( $fields ) {
$fields['billing']['codice_snep'] = array(
'label' => __('Codice Snep', 'woocommerce'),
'placeholder' => _x('Codice Snep', 'placeholder', 'woocommerce'),
'required' => true,
'clear' => true
);
return $fields;
}
add_action( 'woocommerce_checkout_update_order_meta', 'codice_snep_order_meta' );
function codice_snep_order_meta( $order_id ) {
if ( ! empty( $_POST['codice_snep'] ) ) {
update_post_meta( $order_id, 'Codice Snep', sanitize_text_field( $_POST['codice_snep'] ) );
}
}
// Save the custom field 'codice_snep'
add_action( 'woocommerce_save_account_details', 'save_codice_snep_account_details', 12, 1 );
function save_codice_snep_account_details( $user_id ) {
// For Codice Snep
if( isset( $_POST['codice_snep'] ) )
update_user_meta( $user_id, 'codice_snep', sanitize_text_field( $_POST['codice_snep'] ) );
}
此后,如果您不填写此字段,则会显示错误消息:
// Show error if you don't insert CODICE SNEP
add_action('woocommerce_checkout_process', 'required_codice_snep_checkout_field_process');
function required_codice_snep_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['codice_snep'] )
wc_add_notice( __( 'Compila il campo Codice SNEP .' ), 'error' );
}
比起我在WC的“管理”区域,电子邮件和发票中显示代码:
// Show field codice snep in the Wc pdf invoices plugin
add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_codice_snep', 10, 2 );
function wpo_wcpdf_codice_snep ($template_type, $order) {
$document = wcpdf_get_document( $template_type, $order );
if ($template_type == 'invoice') {
?>
<tr>
<th>Codice Snep:</th>
<td><?php $document->custom_field('Codice Snep'); ?></td>
</tr>
<?php
}
}
// Show CODICE SNEP in Back End Wp
add_action( 'woocommerce_admin_order_data_after_billing_address', 'codice_snep_order_meta_admin', 10, 1 );
function codice_snep_order_meta_admin($order){
echo '<p><strong>'.__('Codice Snep').':</strong> ' . get_post_meta( $order->id, 'Codice Snep', true ) . '</p>';
}
// Show codoce snep in the order email
add_filter('woocommerce_email_order_meta_keys', 'my_custom_codice_snep_order_meta_keys');
function my_custom_codice_snep_order_meta_keys( $keys ) {
$keys[] = 'Codice Snep';
return $keys;
}
直到这里一切都可以正常工作。
现在的问题是仅在仪表板前端显示(必须不可编辑)此字段(codice_snep
)。
现在我正在尝试此操作,但只能看到没有字段值的标签。
// Show codice snep in the Dashboard of the user in front-end.
add_action( 'woocommerce_account_dashboard', 'codice_snep_order_dashboard', 12, 1 );
function codice_snep_order_dashboard ($order){
echo '<p><strong>'.__('Codice Snep').':</strong> ' . get_post_meta( $order->id, 'Codice Snep', true ) . '</p>';
}
会发生这种情况:Field not showing on dashboard
您能帮我解决此问题,以便显示用户在结帐时插入的value
吗?
答案 0 :(得分:0)
您正在使用的代码有些过时和/或不建议使用某些挂钩(即使它们来自Woocommerce文档)。
下面的代码使用不同的钩子。它允许在结帐和“我的帐户”>“地址”>“帐单”字段中显示此字段和相关数据(如果存在),而无需其他验证代码。
也可以在下订单时通过一个钩子将数据保存在订单中,并同时保存在用户元数据中。因此,代码更加紧凑,并使用了Woocommerce 3中引入的正确的钩子with the new CRUD methods。
您重新访问的代码:
// Display a custom field on checkout and on My account > edit billing address
add_filter( 'woocommerce_billing_fields' , 'adding_billing_codice_snep', 20, 1 );
function adding_billing_codice_snep ( $fields ) {
$fields['billing_codice_snep'] = array(
'label' => __('Codice Snep', 'woocommerce'),
'placeholder' => _x('Codice Snep', 'placeholder', 'woocommerce'),
'class' => array('form-row-wide'),
'required' => true,
'clear' => true,
);
return $fields;
}
// Save the custom field data to the order meta data and to user meta data
add_action( 'woocommerce_checkout_create_order', 'codice_snep_order_meta', 20, 2 );
function codice_snep_order_meta( $order, $data ) {
if ( isset( $_POST['billing_codice_snep'] ) && ! empty( $_POST['billing_codice_snep'] ) ) {
$order->update_meta_data('_billing_codice_snep', sanitize_text_field( $_POST['billing_codice_snep'] ) );
update_user_meta( $order->get_customer_id(), 'billing_codice_snep', sanitize_text_field( $_POST['billing_codice_snep'] ) );
}
}
// Order pages (frontend and admin): Display custom field "codice snep"
add_filter( 'woocommerce_order_details_after_order_table' , 'display_admin_order_meta_codice_snep', 20, 1 ); // Front
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_admin_order_meta_codice_snep', 20, 1 ); // Admin
function display_admin_order_meta_codice_snep( $order ){
$codice_snep = $order->get_meta('_billing_codice_snep', true );
if( ! empty( $codice_snep ) ){
$label = __('Codice Snep');
if( is_admin() ){ // Admin
echo '<p><strong>' . $label . ':</strong> ' . $codice_snep . '</p>';
}
else { // Front end: order view and Order received (thankyou)
echo '<table class="woocommerce-table"><tbody><tr>
<th>' . $label . ':</th><td>' . $codice_snep . '</td>
</tr></tbody></table>';
}
}
}
// Email notifications: Display custom field "codice snep"
add_filter( 'woocommerce_email_order_meta_fields' , 'display_email_codice_snep', 20, 3 );
function display_email_codice_snep ( $fields, $sent_to_admin, $order ) {
$codice_snep = $order->get_meta('_billing_codice_snep', true );
if( ! empty( $codice_snep ) )
$fields['codice_snep'] = array(
'label' => __("Codice Snep"),
'value' => $codice_snep,
);
return $fields;
}
// PDF Invoices: Display custom field "codice snep"
add_action( 'wpo_wcpdf_after_order_data', 'display_pdf_invoice_codice_snep', 20, 2 );
function display_pdf_invoice_codice_snep ($template_type, $order) {
$document = wcpdf_get_document( $template_type, $order );
if ($template_type == 'invoice') {
echo '<tr>
<th>' . __("Codice Snep") . ':</th>
<td>' . $order->get_meta('_billing_codice_snep', true ) . '</td>
</tr>';
}
}
代码会出现在您活动的子主题(或主题)的function.php文件中,或者出现在任何插件文件中。
订单视图:
我的帐户>地址>结算