【问题标题】:Show billing email from customer orders in WooCommerce My account Edit address在 WooCommerce 中显示来自客户订单的账单电子邮件 我的帐户 编辑地址
【发布时间】:2021-06-11 23:19:20
【问题描述】:

我想在每个订单中显示客户的元“电子邮件”,但这会引发错误。

add_action( 'woocommerce_before_edit_account_address_form', 'save_billing_email_to_user', 12, 1 );
function save_billing_email_to_user( $user_id ) {
    $query = new WC_Order_Query( 
        array( 
            'customer_id' => $user_id 
        )
    );
    $orders = $query->get_orders();
    foreach($orders as $ro => $inn){
             echo $ro['email'];
    }
}

我在这里做错了什么?

【问题讨论】:

    标签: php wordpress woocommerce metadata orders


    【解决方案1】:

    使用以下重新访问的代码来查询一个订单以获取账单电子邮件:

    add_action( 'woocommerce_before_edit_account_address_form', 'save_billing_email_to_user', 12 );
    function save_billing_email_to_user( $user_id ) {
        $orders = wc_get_orders(  array( 'customer_id' => $user_id , 'limit' => '1' ) );
        if ( ! empty( $orders ) ) {
            $order = reset( $orders );
            $order_id      = $order->get_id();
            $billing_email = $order->get_billing_email();
            echo $billing_email;
        }
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。


    或者您可以显示每个客户订单的帐单电子邮件地址,例如:

    add_action( 'woocommerce_before_edit_account_address_form', 'save_billing_email_to_user', 12 );
    function save_billing_email_to_user( $user_id ) {
        $orders = wc_get_orders(  array( 'customer_id' => $user_id ) );
        foreach( $orders as $order ){
            $order_id = $order->get_id();
            echo $order->get_billing_email() . '<br>';
        }
    }
    

    【讨论】:

    • 谢谢。第一个解决方案有效。第二是抛出wordpress错误。第二种解决方案更适合进一步定制。
    • 这是从最后一个订单中获取电子邮件还是随机抽取一个订单?
    • 接受。最后一个问题,如何在这里获取订单 ID?想要使用 wc_update_order_item_meta( $order_id, 'calculated_field', $calculatedValue ); 来更新元字段;
    • 已在下面发布了完整代码,请您查看。由于限制,无法发布新问题。
    • @MuhammadAdnanBashir 抱歉,StackOverFlow 的规则当时是一个问题(按线程)。因此,请提供您尝试过的代码,清晰详细地提出新问题。请删除下面的代码,因为这是为了回答,而不是为了提问。
    猜你喜欢
    • 1970-01-01
    • 2023-01-27
    • 2019-04-01
    • 2022-01-19
    • 2019-08-18
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多