I am using the woocommerce plugin in the theme. I need functionality where I need to check the total amount of orders made by a client. Based on this, I offer them a discount in coupons. Part of the coupons is understandable. But I can’t get the total amount of the entire order made by the user.
what's my plan: if the user’s purchase exceeds $ 300, we will give him a coupon. I use this action for this. But with difficulties with the database query for the amount of orders by the 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' );
I am also trying to get an order by the user ID that I received from my-orders.php in the woocommerce / myaccount folder. I am trying to run this code in function.php, but returning an 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. , . , .