如何在woocommerce中获得用户的订单总和? [英] How to get total sum of orders by a user in woocommerce?

查看:43
本文介绍了如何在woocommerce中获得用户的订单总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个主题中使用 woocommerce 插件.我想要一个功能,我需要检查客户下订单的总和.在此基础上,我将为他们提供优惠券折扣.优惠券部分很清楚.但是我无法获得用户所做的所有订单的总和.

I am using woocommerce plugin in a theme. I want a functionality where I need to check the total sum of orders made by a customer. On the basis of that i will offer them discount in coupons. Coupons part is clear. But I am not able to get total sum of all order made by a user.

我的计划是:如果用户购买超过300美元,我们将向他提供优惠券.为此,我正在使用此操作.但在查询表单数据库时遇到用户订单总和的困难.

what my plan is : if a users purchase will exceeds the $300 , we will provide him coupon. for this I am using this action. But having difficulty with query form database for sum of orders by user.

function so_27969258_track_orders_per_customer($order_id){


    $order = new WC_Order( $order_id );
    $myuser_id = (int)$order->user_id;
    $user_info = get_userdata($myuser_id);
    $items = $order->get_items();
    foreach ($items as $item) {

    }
    return $order_id;

}
add_action( 'woocommerce_payment_complete', 'so_27969258_track_orders_per_customer' );

我还试图通过我从 woocommerce/myaccount 文件夹中的 my-orders.php 获得的用户 ID 获取订单.我试图在 function.php 中运行此代码但返回空数组.

I am aslo trying to get the order by a user id which I got from the my-orders.php in woocommerce/myaccount folder. I am trying to run this code in function.php but returning the empty array.

$customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
    'numberposts' => $order_count,
    'meta_key'    => '_customer_user',
    'meta_value'  => get_current_user_id(),
    'post_type'   => wc_get_order_types( 'view-orders' ),
    'post_status' => array_keys( wc_get_order_statuses() )
) ) );


var_dump($customer_orders);
if ( $customer_orders ) : 


    foreach ( $customer_orders as $customer_order ) {
                $order = wc_get_order( $customer_order );
                $order->populate( $customer_order );
                $item_count = $order->get_item_count();

                 echo $order->get_order_number(); 
                 echo wc_get_order_status_name( $order->get_status() );
                 echo sprintf( _n( '%s for %s item', '%s for %s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count );

            }



 endif;

任何人都可以帮助我这段代码如何在 function.php 中工作.我知道我遗漏了很多东西.请提出建议.

Can anybody help me how this code will work in function.php .I know am missing lots of things. Please suggest.

推荐答案

您可以使用以下函数获取用户已完成订单的总和

You can use the following function to get total sum of completed orders of a user

public function get_customer_total_order() {
    $customer_orders = get_posts( array(
        'numberposts' => - 1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => array( 'shop_order' ),
        'post_status' => array( 'wc-completed' )
    ) );

    $total = 0;
    foreach ( $customer_orders as $customer_order ) {
        $order = wc_get_order( $customer_order );
        $total += $order->get_total();
    }

    return $total;
}

但是这个函数必须在 wp 完全加载时调用,所以如果你在你的 action woocommerce_payment_complete 上使用这个函数,它会起作用

But this function must be called when wp loaded completely, so if you use this function on your action woocommerce_payment_complete, it will work

希望能帮到你

这篇关于如何在woocommerce中获得用户的订单总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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