WooCommerce电子邮件模板中的高级自定义字段 [英] Advanced Custom Fields in WooCommerce Email templates
问题描述
我正在使用高级自定义字段(ACF)插件在WooCommerce中,我设置了一个名为 tracking-no的自定义字段。
I am using Advanced Custom Fields (ACF) plugin in WooCommerce and I have set a custom field named "tracking-no".
如何在Woocommerce模板 emails / customer-completed-order.php
?
How can I display the value of this custom field in the Woocommerce template emails/customer-completed-order.php
?
我正在使用此代码:
<?php
if(get_field('tracking-no'))
{
echo '<p>' . get_field('tracking-no') . '</p>';
}
?>
但是我什么也没得到。
谢谢
推荐答案
在WooCommerce模板中,您应该首先在> get_field()
:
In your WooCommerce template you should get first the order ID as argument in get_field()
:
<?php
// Get the $order ID (WooCommerce version compatibility)
if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
$order_id = int_val( $order->id ); // Older than 3.0
} else {
$order_id = int_val( $order->get_id() ); // 3.0+
}
$tracking_num = get_field('tracking-no', $order_id );
if( $tracking_num ){
echo '<p>' . $tracking_num . '</p>';
}
?>
您还可以使用任何电子邮件通知挂钩您可以通过以下方式在此模板上找到:
You can also use instead any email notification hook that you can find on this template, this way:
add_action( 'woocommerce_email_order_details', 'my_custom_field_in_completed_notification', 10, 4 );
function my_custom_field_in_completed_notification( $order, $sent_to_admin, $plain_text, $email ){
// Get the $order ID (WooCommerce version compatibility)
if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
$order_id = int_val( $order->id ); // Older than 3.0
} else {
$order_id = int_val( $order->get_id() ); // 3.0+
}
$tracking_num = get_field('tracking-no', $order_id );
if( $tracking_num ){
echo '<p>' . $tracking_num . '</p>';
}
}
代码进入function.php文件您的活动子主题(或主题)或任何插件文件中。
在这种情况下,我们不会覆盖WooCommerce模板。您还可以使用两个类似的钩子:
In this case we don't override WooCommerce templates. You can also use that 2 similar hooks:
woocommerce_email_order_meta
woocommerce_email_customer_details
这篇关于WooCommerce电子邮件模板中的高级自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!