How to automatically create an account if a product in the order belongs to a certain category in WooCommerce

↘锁芯ラ 提交于 2021-01-28 05:54:35

问题


I have a WooCommerce shop where customers checkout as guests. I now want to sell a couple of virtual products and in that case I want to auto create an account.

So I have two working code snippets.

  • One to check if a product in cart is within a specific category (online)
  • One to auto create an account from guest checkout.

But I can't figure out how to combine these two so they work together. Or is it a better solution for this? Any ideas?

Here is the two code snippets I started with and then tried to combine into one function.

Source: Check for Product Category in cart items with WooCommerce

add_action('woocommerce_before_cart', 'action_before_cart');
function action_before_cart() {
    $categories   = array('online');
    $has_category = false;

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // Check for product categories
        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $has_category = true;
            break;
        }
    }

    // Testing output (display a notice)
    if ( $has_category ) { 
        wc_print_notice( sprintf( 'Product category "%s" is in cart!', reset($categories)), 'notice' );
    }
}

Source: Send the reset password email notifications programatically in Woocommerce

function wc_register_guests( $order_id ) {
  // get all the order data
  $order = new WC_Order($order_id);
  
  //get the user email from the order
  $order_email = $order->billing_email;
    
  // check if there are any users with the billing email as user or email
  $email = email_exists( $order_email );  
  $user = username_exists( $order_email );
  
  // if the UID is null, then it's a guest checkout
  if( $user == false && $email == false ){
    
    // random password with 12 chars
    $random_password = wp_generate_password();
    
    // create new user with email as username & newly created pw
    $user_id = wp_create_user( $order_email, $random_password, $order_email );
    
    //WC guest customer identification
    update_user_meta( $user_id, 'guest', 'yes' );
 
    //user's billing data
    update_user_meta( $user_id, 'billing_address_1', $order->billing_address_1 );
    update_user_meta( $user_id, 'billing_address_2', $order->billing_address_2 );
    update_user_meta( $user_id, 'billing_city', $order->billing_city );
    update_user_meta( $user_id, 'billing_company', $order->billing_company );
    update_user_meta( $user_id, 'billing_country', $order->billing_country );
    update_user_meta( $user_id, 'billing_email', $order->billing_email );
    update_user_meta( $user_id, 'billing_first_name', $order->billing_first_name );
    update_user_meta( $user_id, 'billing_last_name', $order->billing_last_name );
    update_user_meta( $user_id, 'billing_phone', $order->billing_phone );
    update_user_meta( $user_id, 'billing_postcode', $order->billing_postcode );
    update_user_meta( $user_id, 'billing_state', $order->billing_state );
 
    // user's shipping data
    update_user_meta( $user_id, 'shipping_address_1', $order->shipping_address_1 );
    update_user_meta( $user_id, 'shipping_address_2', $order->shipping_address_2 );
    update_user_meta( $user_id, 'shipping_city', $order->shipping_city );
    update_user_meta( $user_id, 'shipping_company', $order->shipping_company );
    update_user_meta( $user_id, 'shipping_country', $order->shipping_country );
    update_user_meta( $user_id, 'shipping_first_name', $order->shipping_first_name );
    update_user_meta( $user_id, 'shipping_last_name', $order->shipping_last_name );
    update_user_meta( $user_id, 'shipping_method', $order->shipping_method );
    update_user_meta( $user_id, 'shipping_postcode', $order->shipping_postcode );
    update_user_meta( $user_id, 'shipping_state', $order->shipping_state );
    
    // link past orders to this newly created customer
    wc_update_new_customer_past_orders( $user_id );
  }
  
}
 
//add this newly created function to the thank you page
add_action( 'woocommerce_thankyou', 'wc_register_guests', 10, 1 ); 

回答1:


I would not automatically create an account. I would ensure that the order cannot be placed as long as one does not have an account. (If that particular product is present in the shopping cart).

The creation of an account and associated actions such as emails, etc.. are already provided in WooCommerce.

Otherwise you will have to write extra code for it and you will soon notice that there is always some option missing (such as sending an email with the user password) that requires you to write some more extra code. In short, keep it as simple as possible.


But because your question was about creating an account yourself, you can do this with the following code

  • Explanation via comment tags added in the code
  • wp_insert_user() - Insert a user into the database
function wc_register_guests( $order_id ) {
    // Determines whether the current visitor is a logged in user.
    if( is_user_logged_in() ) return;
    
    // Get $order object
    $order = wc_get_order( $order_id );
    
    // Get the user email from the order
    $order_email = $order->billing_email;

    // Check if there are any users with the billing email as user or email
    $email = email_exists( $order_email );  
    $user = username_exists( $order_email );

    // If the UID is null, then it's a guest checkout (new user)
    if( $user == false && $email == false ) {
        // Check on category ( multiple categories can be entered, separated by a comma )
        $categories = array( 'online', 'categorie-1' );

        // Flag
        $has_category = false;
        
        // Loop through order items
        foreach ( $order->get_items() as $item_key => $item ) {
            if ( has_term( $categories, 'product_cat', $item['product_id'] ) ) {
                $has_category = true;
                break;
            }
        }
        
        // True
        if ( $has_category ) {
            // Random password with 12 chars
            $random_password = wp_generate_password();
            
            // Firstname
            $first_name = $order->get_billing_first_name();
            
            // Lastname
            $last_name = $order->get_billing_last_name();
            
            // Role
            $role = 'customer';

            // Create new user with email as username, newly created password and userrole          
            $user_id = wp_insert_user(
                array(
                    'user_email' => $order_email,
                    'user_login' => $order_email,
                    'user_pass'  => $random_password,
                    'first_name' => $first_name,
                    'last_name'  => $last_name,
                    'role'       => $role,
                )
            );

            // (Optional) WC guest customer identification
            update_user_meta( $user_id, 'guest', 'yes' );

            // User's billing data
            update_user_meta( $user_id, 'billing_address_1', $order->billing_address_1 );
            update_user_meta( $user_id, 'billing_address_2', $order->billing_address_2 );
            update_user_meta( $user_id, 'billing_city', $order->billing_city );
            update_user_meta( $user_id, 'billing_company', $order->billing_company );
            update_user_meta( $user_id, 'billing_country', $order->billing_country );
            update_user_meta( $user_id, 'billing_email', $order->billing_email );
            update_user_meta( $user_id, 'billing_first_name', $order->billing_first_name );
            update_user_meta( $user_id, 'billing_last_name', $order->billing_last_name );
            update_user_meta( $user_id, 'billing_phone', $order->billing_phone );
            update_user_meta( $user_id, 'billing_postcode', $order->billing_postcode );
            update_user_meta( $user_id, 'billing_state', $order->billing_state );

            // User's shipping data
            update_user_meta( $user_id, 'shipping_address_1', $order->shipping_address_1 );
            update_user_meta( $user_id, 'shipping_address_2', $order->shipping_address_2 );
            update_user_meta( $user_id, 'shipping_city', $order->shipping_city );
            update_user_meta( $user_id, 'shipping_company', $order->shipping_company );
            update_user_meta( $user_id, 'shipping_country', $order->shipping_country );
            update_user_meta( $user_id, 'shipping_first_name', $order->shipping_first_name );
            update_user_meta( $user_id, 'shipping_last_name', $order->shipping_last_name );
            update_user_meta( $user_id, 'shipping_method', $order->shipping_method );
            update_user_meta( $user_id, 'shipping_postcode', $order->shipping_postcode );
            update_user_meta( $user_id, 'shipping_state', $order->shipping_state );

            // Link past orders to this newly created customer
            wc_update_new_customer_past_orders( $user_id );
        }
    }  
}
add_action( 'woocommerce_thankyou', 'wc_register_guests', 10, 1 ); 


来源:https://stackoverflow.com/questions/64041540/how-to-automatically-create-an-account-if-a-product-in-the-order-belongs-to-a-ce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!