How To Automatically Add Newly Registered Users on Admin’s Address Book

  • Copy and paste this code:
    
    function jdl_generate_save_storage_code( $user_id, $user_data ){
    
      /**
       * save registered user on address book</div>
       */
    
      $first_name        = isset( $user_data['first_name'] ) && !empty( $user_data['first_name'] ) ? sanitize_text_field( $user_data['first_name'] ) : '';
      $last_name         = isset( $user_data['last_name'] ) && !empty( $user_data['last_name'] ) ? sanitize_text_field( $user_data['last_name'] ) : '';
      $full_name         = $first_name.' '.$last_name;
      $phone             = isset( $user_data['phone'] ) && !empty( $user_data['phone'] ) ? $user_data['phone'] : '';
      $billing_email     = isset( $user_data['billing_email'] ) && !empty( $user_data['billing_email'] ) ? sanitize_text_field( $user_data['billing_email'] ) : '';
      $billing_address_1 = isset( $user_data['billing_address_1'] ) && !empty( $user_data['billing_address_1'] ) ? sanitize_text_field( $user_data['billing_address_1'] ) : '';
    
      /**
       * check if post exists
       */
    
       $shipper_book_args = array(
        'post_type' => 'wpc_address_book',
        'post_status' => 'publish',
        'meta_query' => array(
          array(
            'key' => 'book',
            'value' => 'shipper'
          ),
          array(
            'key' => 'wpcargo_shipper_email',
            'value' => $billing_email
          ),
        )
      );
    
      $receiver_book_args = array(
        'post_type' => 'wpc_address_book',
        'post_status' => 'publish',
        'meta_query' => array(
          array(
            'key' => 'book',
            'value' => 'receiver'
          ),
          array(
            'key' => 'wpcargo_receiver_email',
            'value' => $billing_email
          ),
        )
      );
    
      $shipper_book_result = new WP_Query( $shipper_book_args );
    
      $receiver_book_result = new WP_Query( $receiver_book_args );
    
      if( !$shipper_book_result->have_posts() ) {
    
        $shipper_ab_id = wp_insert_post( array( 'post_status'  => 'publish', 'post_type' => 'wpc_address_book', 'post_author' => 1 ) );
    
        $shipper_reg_metakeys = array(
          'book'                    => 'shipper',
          '_assigned_to'            => 1,
          'wpcargo_shipper_name'    => $full_name,
          'wpcargo_shipper_phone'   => $phone,
          'wpcargo_shipper_address' => $billing_address_1,
          'wpcargo_shipper_email'   => $billing_email,
          'public_shipper'          => 'Public'
        );
    
        if( $shipper_ab_id ) {
    
          foreach( $shipper_reg_metakeys as $meta_key => $meta_value ) {
    
            update_post_meta( $shipper_ab_id, $meta_key, $meta_value );
    
          }
    
        }
    
      }
    
      if( !$receiver_book_result->have_posts() ){
    
        $receiver_ab_id = wp_insert_post( array( 'post_status'  => 'publish', 'post_type' => 'wpc_address_book', 'post_author' => 1 ) );
    
        $receiver_reg_metakeys = array(
          'book'                     => 'receiver',
          '_assigned_to'             => 1,
          'wpcargo_receiver_name'    => $full_name,
          'wpcargo_receiver_phone'   => $phone,
          'wpcargo_receiver_address' => $billing_address_1,
          'wpcargo_receiver_email'   => $billing_email,
          'public_receiver'          => 'Public'
        );
    
        if( $receiver_ab_id ){
    
          foreach( $receiver_reg_metakeys as $meta_key => $meta_value ) {
    
            update_post_meta( $receiver_ab_id, $meta_key, $meta_value );
    
          }
    
        }
    
      }
    
    }
    add_action( 'wpcfe_after_user_registration_success', 'jdl_generate_save_storage_code', 10, 2 );
    
    
0
    0
    Your Cart
    Your cart is emptyReturn to Shop