Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Following code adds a customer as a amelia customer if they purchase a product with a given product id
- add_action( 'woocommerce_order_status_completed', 'register_student_amelia', 10, 1 );
- //Checks if the relevant product is being purchased to update customer
- function register_student_amelia_and_year_group($order_id){
- $wc_product_id = 4623; //replace with your product id
- $order = new WC_Order($order_id);
- $items = $order->get_items();
- foreach($items as $item){
- if ($wc_product_id == $item['product_id']) {
- add_user_as_amelia_customer($order);
- }
- }
- }
- function add_user_as_amelia_customer($order){
- global $wpdb;
- $user_id = $order->customer_id;
- $user = new WP_User( $user_id );
- $user_info = get_userdata($user_id);
- $user_email = $user_info->user_email;
- $first_name = get_user_meta( $user_id, 'first_name', true);
- $last_name = get_user_meta( $user_id, 'last_name', true);
- $phone_number = $order->get_billing_phone();
- if(!in_array('wpamelia-customer',$user->roles)){
- //Add amelia customer
- $user -> add_role( 'wpamelia-customer' );
- $wpdb -> insert('wphx_amelia_users', array( //wphx_amelia_users was my table name but you need to navigate to MySQL database in you cPanel to see what your amelia users table name is
- 'firstName' => $first_name,
- 'lastName' => $last_name,
- 'email' => $user_email,
- 'phone' => $phone_number,
- 'type' => 'customer',
- 'externalId' => $user_id,
- 'note' => NULL
- ), array('%s','%s','%s','%s','%s','%d','%s','%s'));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement