wc_update_new_customer_past_orders

Get past orders (by email) and update them.

Description

(int) wc_update_new_customer_past_orders( (int) $customer_id ); 

Returns (int)

Parameters (1)

0. $customer_id (int)
The customer id.

Usage

  1. if ( !function_exists( 'wc_update_new_customer_past_orders' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-user-functions.php'; 
  3.  
  4. // The customer id. 
  5. $customer_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wc_update_new_customer_past_orders($customer_id); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-user-functions.php  
  1. function wc_update_new_customer_past_orders( $customer_id ) { 
  2. $linked = 0; 
  3. $complete = 0; 
  4. $customer = get_user_by( 'id', absint( $customer_id ) ); 
  5. $customer_orders = wc_get_orders( array( 
  6. 'limit' => -1,  
  7. 'customer' => array( array( 0, $customer->user_email ) ),  
  8. 'return' => 'ids',  
  9. ) ); 
  10.  
  11. if ( ! empty( $customer_orders ) ) { 
  12. foreach ( $customer_orders as $order_id ) { 
  13. update_post_meta( $order_id, '_customer_user', $customer->ID ); 
  14.  
  15. do_action( 'woocommerce_update_new_customer_past_order', $order_id, $customer ); 
  16.  
  17. if ( get_post_status( $order_id ) === 'wc-completed' ) { 
  18. $complete++; 
  19.  
  20. $linked++; 
  21.  
  22. if ( $complete ) { 
  23. update_user_meta( $customer_id, 'paying_customer', 1 ); 
  24. update_user_meta( $customer_id, '_order_count', '' ); 
  25. update_user_meta( $customer_id, '_money_spent', '' ); 
  26.  
  27. return $linked;