自定义 phone 数字字段未显示在 WooCommerce 订单电子邮件中
Custom phone number field not showing in WooCommerce order email
我已成功将 phone 号码字段添加到不同的地址, phone 号码也显示在后端。但是我没有收到电子邮件中的 phone 号码。
请帮忙
此代码帮助我添加字段:
add_filter( 'woocommerce_checkout_fields', 'bbloomer_shipping_phone_checkout' );
function bbloomer_shipping_phone_checkout( $fields ) {
$fields['shipping']['shipping_phone'] = array(
'label' => 'Phone',
'required' => true,
'class' => array( 'form-row-wide' ),
);
return $fields;
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'bbloomer_shipping_phone_checkout_display' );
function bbloomer_shipping_phone_checkout_display( $order ){
echo '<p><b>Shipping Phone:</b> ' . get_post_meta( $order->get_id(), '_shipping_phone', true ) . '</p>';
}
我尝试在上面的代码中添加额外的代码(如下所示)以在电子邮件中显示 phone 号码。还是不行!
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['meta_key'] = array(
'label' => __( 'Shipping Phone' ),
'value' => get_post_meta( $order->id, '_shipping_phone', true ),
);
return $fields;
}
你错过了正确的meta_key
,我也稍微修改了你现有的代码。
注意: 通过调整 priority
参数,您可以将字段设置在正确的位置。
所以你得到:
// Shipping field on my account edit-addresses and checkout
function filter_woocommerce_shipping_fields( $fields ) {
$fields['shipping_phone'] = array(
'label' => __( 'Shipping Phone', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-wide' ),
'priority' => 55
);
return $fields;
}
add_filter( 'woocommerce_shipping_fields' , 'filter_woocommerce_shipping_fields', 10, 1 );
// Display on the order edit page (backend)
function action_woocommerce_admin_order_data_after_shipping_address( $order ) {
if ( $value = $order->get_meta( '_shipping_phone' ) ) {
echo '<p><strong>' . __( 'Shipping Phone', 'woocommerce' ) . ':</strong> ' . $value . '</p>';
}
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 );
// Display on email notifications
function filter_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
// Get meta
$shipping_phone = $order->get_meta( '_shipping_phone' );
// NOT empty
if ( ! empty( $shipping_phone ) ) {
$fields['_shipping_phone'] = array(
'label' => __( 'Shipping Phone', 'woocommerce' ),
'value' => $shipping_phone,
);
}
return $fields;
}
add_filter( 'woocommerce_email_order_meta_fields', 'filter_woocommerce_email_order_meta_fields', 10, 3 );
我已成功将 phone 号码字段添加到不同的地址, phone 号码也显示在后端。但是我没有收到电子邮件中的 phone 号码。 请帮忙
此代码帮助我添加字段:
add_filter( 'woocommerce_checkout_fields', 'bbloomer_shipping_phone_checkout' );
function bbloomer_shipping_phone_checkout( $fields ) {
$fields['shipping']['shipping_phone'] = array(
'label' => 'Phone',
'required' => true,
'class' => array( 'form-row-wide' ),
);
return $fields;
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'bbloomer_shipping_phone_checkout_display' );
function bbloomer_shipping_phone_checkout_display( $order ){
echo '<p><b>Shipping Phone:</b> ' . get_post_meta( $order->get_id(), '_shipping_phone', true ) . '</p>';
}
我尝试在上面的代码中添加额外的代码(如下所示)以在电子邮件中显示 phone 号码。还是不行!
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['meta_key'] = array(
'label' => __( 'Shipping Phone' ),
'value' => get_post_meta( $order->id, '_shipping_phone', true ),
);
return $fields;
}
你错过了正确的meta_key
,我也稍微修改了你现有的代码。
注意: 通过调整 priority
参数,您可以将字段设置在正确的位置。
所以你得到:
// Shipping field on my account edit-addresses and checkout
function filter_woocommerce_shipping_fields( $fields ) {
$fields['shipping_phone'] = array(
'label' => __( 'Shipping Phone', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-wide' ),
'priority' => 55
);
return $fields;
}
add_filter( 'woocommerce_shipping_fields' , 'filter_woocommerce_shipping_fields', 10, 1 );
// Display on the order edit page (backend)
function action_woocommerce_admin_order_data_after_shipping_address( $order ) {
if ( $value = $order->get_meta( '_shipping_phone' ) ) {
echo '<p><strong>' . __( 'Shipping Phone', 'woocommerce' ) . ':</strong> ' . $value . '</p>';
}
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 );
// Display on email notifications
function filter_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
// Get meta
$shipping_phone = $order->get_meta( '_shipping_phone' );
// NOT empty
if ( ! empty( $shipping_phone ) ) {
$fields['_shipping_phone'] = array(
'label' => __( 'Shipping Phone', 'woocommerce' ),
'value' => $shipping_phone,
);
}
return $fields;
}
add_filter( 'woocommerce_email_order_meta_fields', 'filter_woocommerce_email_order_meta_fields', 10, 3 );