Change user role based on account spend woocommerce
-
Hello. I have been working on an issue for a little while and can’t seem to find the solution. I am attempting to develop a function that changes a customer’s account type (ie from ‘customer’ to something else) once their account spend hits a certain amount.
Here is what I have tried so far. I very new to writing php and functions so I apologize upfront for the mess this may be. Any help would be appreciated.
/** * Get total spent by customer * @param int $user_id * @return string */ function wc_get_customer_total_spent( $user_id ) { if ( ! $spent = get_user_meta( $user_id, '_money_spent' < 50 ) ) { global $wpdb; // Remove role $user->remove_role( 'customer' ); // Add role $user->add_role( 'procon' ); $spent = $wpdb->get_var( "SELECT SUM(meta2.meta_value) FROM $wpdb->posts as posts LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id LEFT JOIN {$wpdb->postmeta} AS meta2 ON posts.ID = meta2.post_id WHERE meta.meta_key = '_customer_user' AND meta.meta_value = $user_id AND posts.post_type IN ('" . implode( "','", wc_get_order_types( 'reports' ) ) . "') AND posts.post_status IN ( 'wc-completed', 'wc-processing' ) AND meta2.meta_key = '_order_total' " ); update_user_meta( $user_id, '_money_spent', $spent ); } return $spent; }
Viewing 9 replies - 1 through 9 (of 9 total)
Viewing 9 replies - 1 through 9 (of 9 total)
- The topic ‘Change user role based on account spend woocommerce’ is closed to new replies.