获得WooCommerce客户订单 [英] Get Woocommerce customer orders

查看:0
本文介绍了获得WooCommerce客户订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能获得当前和以前的客户订单?也就是说,我需要每个客户当前订单的账单地址和以前订单的账单地址。

例如,我需要获取每个客户订单的数组,即我需要每个客户订单的帐单地址。

所以我有一些代码可以打印管理面板中编辑订单页面上的任何文本。

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'edit_woocommerce_order_page', 10, 1 );

function edit_woocommerce_order_page($order){
global $post_id;
$order = new WC_Order( $post_id );
echo '<p><strong>Some text here</strong> ' . get_post_meta($order->get_id(), '_shipping_field_value', true ) . '</p>'; 
}

如您所见,它为每个用户显示一些文本。我想我应该得到一些每个客户的数组,并显示订单ID和帐单地址1的订单数组。请检查屏幕截图 Code above adds text in Edit Order page

可以吗?

推荐答案

如果客户不是客人,则以下函数将获取所有已完成的订单。

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'edit_woocommerce_order_page', 10, 1 );

function edit_woocommerce_order_page($order){
$customer_id =  $order->get_user_id();
//Check if its guest or not
if($customer_id != 0):
    $args = array(
        'customer_id' => $customer_id,
        'status' => array('wc-completed'), //Change if needed
        'exclude' => array( $order->get_id() ), // We dont need current order
    );
    $orders = wc_get_orders( $args );
    if($orders):
        foreach($orders as $k=>$order):
            echo '<p><strong>Some text here</strong>'.$order->get_billing_address_1().'</p>';
        endforeach;
    endif;
endif;
}

限制为最后两个订单

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'edit_woocommerce_order_page', 10, 1 );
    
    function edit_woocommerce_order_page($order){
    $customer_id =  $order->get_user_id();
    //Check if its guest or not
    if($customer_id != 0):
        $args = array(
            'customer_id' => $customer_id,
            'status' => array('wc-completed'), //Change if needed
            'limit' => 2,
        );
        $orders = wc_get_orders( $args );
        if($orders):
            foreach($orders as $k=>$order):
                echo '<p><strong>Some text here</strong>'.$order->get_billing_address_1().'</p>';
            endforeach;
        endif;
    endif;
    }

仅获取当前订单和上一订单

function edit_woocommerce_order_page($order){
    $customer_id =  $order->get_user_id();
    //We need current order id to know where we start
    $order_id = $order->get_id();
    if($customer_id != 0):
        $args = array(
            'customer_id' => $customer_id,
            'status' => array('wc-completed'), //Change if needed
            'return' => 'ids', // Grab all order ids for customer 
            'posts_per_page' => -1
        );
        $all_order_ids = wc_get_orders( $args );
        //Find current order key 
        $all_order_id_keys = array_flip(array_keys($all_order_ids));
        $current_order_key = array_keys($all_order_ids, $order_id);
        //Grab all values from our array
        $all_order_id_values = array_values($all_order_ids);
        //From all values we look for current order key and we increase that key with 1 to grab prev order id by key
        $previous_order_id = $all_order_id_values[$all_order_id_keys[$current_order_key[0]]+1];
        $order_args = array(
            'post__in' => array($order_id,$previous_order_id),
        );
        $orders = wc_get_orders( $order_args );
        if($orders):
            foreach($orders as $k=>$order):
                echo $order->get_id(); // For testing 
                echo '<p><strong>Some text here</strong>'.$order->get_billing_address_1().'</p>';
            endforeach;
        endif;
    endif;
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'edit_woocommerce_order_page', 10, 1 ); 

这篇关于获得WooCommerce客户订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆