Advertisement
Guest User

Untitled

a guest
Aug 27th, 2021
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.48 KB | None | 0 0
  1. //Following code adds a customer as a amelia customer if they purchase a product with a given product id
  2. add_action( 'woocommerce_order_status_completed', 'register_student_amelia', 10, 1 );
  3.  
  4. //Checks if the relevant product is being purchased to update customer
  5. function register_student_amelia_and_year_group($order_id){
  6.     $wc_product_id = 4623; //replace with your product id
  7.     $order = new WC_Order($order_id);
  8.     $items = $order->get_items();
  9.     foreach($items as $item){
  10.         if ($wc_product_id == $item['product_id']) {
  11.             add_user_as_amelia_customer($order);
  12.         }
  13.     }
  14. }
  15.  
  16. function add_user_as_amelia_customer($order){
  17.     global $wpdb;
  18.     $user_id = $order->customer_id;
  19.     $user = new WP_User( $user_id );
  20.     $user_info = get_userdata($user_id);
  21.     $user_email = $user_info->user_email;
  22.     $first_name = get_user_meta( $user_id, 'first_name', true);
  23.     $last_name = get_user_meta( $user_id, 'last_name', true);
  24.     $phone_number = $order->get_billing_phone();
  25.     if(!in_array('wpamelia-customer',$user->roles)){
  26.     //Add amelia customer
  27.         $user -> add_role( 'wpamelia-customer' );
  28.         $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
  29.         'firstName' => $first_name,
  30.         'lastName' => $last_name,
  31.         'email' => $user_email,
  32.         'phone' => $phone_number,
  33.         'type' => 'customer',
  34.         'externalId' => $user_id,
  35.         'note' => NULL
  36.         ), array('%s','%s','%s','%s','%s','%d','%s','%s'));
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement