在 WooCommerce 收到订单页面上的文本中添加客户电子邮件

2024-03-24

在 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>' ;

}

如何将客户的电子邮件地址添加到自定义文本末尾?


To get 客户账单电子邮件,您可以使用其中之一:

  • WoocommerceWC_Order method get_billing_email() https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html#_get_billing_email
  • WordPress 功能get_post_meta() https://developer.wordpress.org/reference/functions/get_post_meta/使用元键_billing_email来自订单 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 文件中。经过测试并有效。


2) 在订单接收页面底部:

使用WC_Order method get_billing_email() https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html#_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>' ;
}

代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。


或者,使用 WordPressget_post_meta() https://developer.wordpress.org/reference/functions/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

通过以下行:

$email = get_post_meta( $order_id, '_billing_email', true ); // Get Customer billing email
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 WooCommerce 收到订单页面上的文本中添加客户电子邮件 的相关文章

随机推荐