[英]Add customer email in a text on WooCommerce Order received page
In WooCommerce, on top of my thank you / order-received page, I've added a custom text, with the following code: 在WooCommerce中,在我的感谢/订单收到的页面之上,我添加了一个自定义文本,其中包含以下代码:
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? 如何将客户的电子邮件地址添加到自定义文本的末尾?
To get the customer billing email , you can use one of those:
要获取客户帐单电子邮件 ,您可以使用以下其中一种:
- The Woocommerce
WC_Order
methodget_billing_email()
Woocommerce
WC_Order
方法get_billing_email()
- The WordPress function
get_post_meta()
with the meta key_billing_email
from order ID.WordPress函数
get_post_meta()
带有来自订单ID的元键_billing_email
。
Now you can set the text in 2 different locations : 现在,您可以在两个不同的位置设置文本:
1) On top of Order received page: 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>' ;
}
Code goes in function.php file of your active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。 Tested and works.
经过测试和工作。
2) On bottom of Order received page: 2)在订单收到页面的底部:
Using the WC_Order
method get_billing_email()
this way: 以这种方式使用
WC_Order
方法get_billing_email()
:
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>' ;
}
Code goes in function.php file of your active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。 Tested and works.
经过测试和工作。
Alternatively, using WordPress get_post_meta()
function, replacing in the function: 或者,使用WordPress
get_post_meta()
函数,替换函数:
$order = wc_get_order( $order_id ); // Get an instance of the WC_Order Object
$email = $order->get_billing_email(); // Get Customer billing email
By the following line: 按以下行:
$email = get_post_meta( $order_id, '_billing_email', true ); // Get Customer billing email
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.