phpwordpresswoocommercecheckoutuser-data

Auto create an user account after payment and auto login in WooCommerce


After others' previous questions, I have a question here: Automatically create an user account after WooCommerce checkout and auto login

First question:
How in this section that the message (Thank you. Your order has been received. An account has been automatically created for you and you are now <a href="https://yoursite.name/my-account/">logged in< /a>.You will receive an email about this) displays, should I display the password that was created along with the email that was entered to the customer? (Also, if the customer enters other fields such as first and last name, phone number, etc., it will be displayed here.)

Second question:
When I use this code:

$link = get_permalink( get_option( 'woocommerce_myaccount_page_id' ) );  

For me, it displays the value of the link as text, that is, it looks like this:

Thank you. Your order has been received. An account has been automatically created for you and you are now <a href="https://yoursite.name/my-account/">logged in</a>. You will receive an email about this.

How do I fix this problem? that the login value has a link href instead of being displayed as text


The main question is whether these codes are standard and up-to-date? Do they need to be modified or changed to improve performance?


Solution

  • First question: You never display any password, for security reasons, and an email is sent to the customer for this purpose. The customer doesn't enter anything else than the information on checkout fields, so all required information is already displayed in the order received page.

    Second question:

    woocommerce_thankyou_order_received_text hook is located in checkout/order-received.php template file as follows:

    /**
     * Filter the message shown after a checkout is complete.
     *
     * @since 2.2.0
     *
     * @param string         $message The message.
     * @param WC_Order|false $order   The order created during checkout, or false if order data is not available.
     */
    $message = apply_filters(
        'woocommerce_thankyou_order_received_text',
        __( 'Thank you. Your order has been received.', 'woocommerce' ),
        $order
    );
    
    echo esc_html( $message );
    

    As you can see the output is HTML escaped, so it's not possible for the moment to render any HTML. But this is a reported bug and will be solved in WooCommerce upcoming version 8.3

    So replace the 2nd function with:

    add_action( 'woocommerce_before_thankyou', 'action_woocommerce_before_thankyou' );
    function action_woocommerce_before_thankyou( $order_id ) {
        if ( is_user_logged_in() ) return;
        
        $order = wc_get_order($order_id);
        $order_email = $order->get_billing_email(); // Get the user email from the order
        
        // 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 ) {
            // Append to orginal string
            wc_print_notice( sprintf( 
                __('An account has been automatically created for you and you are now %s. You will receive an email about this.', 'woocommerce'), 
                '<a href="' . get_permalink( get_option('woocommerce_myaccount_page_id') ) . '">logged in</a>'
            ), 'notice' ); 
        }       
    }
    

    Related: Automatically create an user account after WooCommerce checkout and auto login