在收到的WooCommerce订单页面上以文本形式添加客户电子邮件 [英] Add customer email in a text on WooCommerce Order received page
问题描述
在WooCommerce中,在谢谢/订单接收页面的顶部,我添加了一个自定义文本,其内容如下:
In WooCommerce, on top of my thank you / order-received page, I've added a custom text, with the following code:
add_action( 'woocommerce_thankyou', 'my_order_received_text', 1, 0);
function my_order_received_text(){
echo '<div class="my_thankyou2"><p>' . __('Your download link was sent to: ') . '</p></div>' ;
}
如何将客户的电子邮件地址添加到自定义文本的末尾?
How can I get the email address of the customer added to the end of the custom text?
推荐答案
要获取客户帐单电子邮件 strong>,您可以使用以下其中一项:
To get the customer billing email, you can use one of those:
- Woocommerce
WC_Order
方法< a href = https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html#_get_billing_email rel = nofollow noreferrer>get_billing_email()
- WordPress功能
get_post_meta()
,并带有订单ID中的元键_billing_email
。
- The Woocommerce
WC_Order
methodget_billing_email()
- The WordPress function
get_post_meta()
with the meta key_billing_email
from order ID.
现在,您可以在 2个不同的位置设置文本:
1)在收到订单的页面顶部:
add_filter( 'woocommerce_thankyou_order_received_text', 'my_order_received_text', 10, 2 );
function my_order_received_text( $text, $order ){
if( ! is_a($order, 'WC_Order') ) {
return $text;
}
// Get Customer billing email
$email = $order->get_billing_email();
return $text . '<br>
<div class="my_thankyou2"><p>' . __('Your download link was sent to: ') . $email . '</p></div>' ;
}
代码进入活动子主题(或活动主题)的function.php文件)。经过测试并有效。
Code goes in function.php file of your active child theme (or active theme). Tested and works.
2)在收到订单页面的底部:
使用 WC_Order
方法 get_billing_email()
这样:
Using the WC_Order
method get_billing_email()
this way:
add_action( 'woocommerce_thankyou', 'my_order_received_text', 10, 1 );
function my_order_received_text( $order_id ){
if( ! $order_id ){
return;
}
$order = wc_get_order( $order_id ); // Get an instance of the WC_Order Object
$email = $order->get_billing_email(); // Get Customer billing email
echo '<div class="my_thankyou2"><p>' . __('Your download link was sent to: ') . $email . '</p></div>' ;
}
代码进入活动子主题(或活动主题)的function.php文件)。经过测试并有效。
Code goes in function.php file of your active child theme (or active theme). Tested and works.
或者,使用WordPress get_post_meta()
函数,替换为该函数:
Alternatively, using WordPress get_post_meta()
function, replacing in the function:
$order = wc_get_order( $order_id ); // Get an instance of the WC_Order Object
$email = $order->get_billing_email(); // Get Customer billing email
通过以下行:
$email = get_post_meta( $order_id, '_billing_email', true ); // Get Customer billing email
这篇关于在收到的WooCommerce订单页面上以文本形式添加客户电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!