0
votes

I have setup Woocommerce to automatically register a new customer when the order is completed.

Now I want to send the password and other things that has been generated to a third party using a API POST request to create the same user account.

Things I need to send from the Woocommerce generated account:

  • Email address (used for email and login on third party website)
  • First name
  • Last name
  • Password (Needs to be unhashed, same as used in new account email)

Can someone show me an example how to get this done? Or point me in to the right direction? I just can't find anything to start with.

I thought by adding Curl to the Woocommerce thank you page webhook. But this won't send the password unhashed, it just stay blank.

Hope someone knows an easy way to get this done.

Thanks!


Latest UPDATE: CODE USING IN MY FUNCTIONS.PHP

class NewCustomerRegistration
{
    private $credentials;

    public function __construct()
    {
        // Use PHP_INT_MAX so that it is the last filter run on this data
        // in case there are other filter changing the password, for example
        add_filter(
            'woocommerce_new_customer_data',
            [$this, 'when_user_is_created'],
            1,
            PHP_INT_MAX
        );
        add_action('woocommerce_new_customer', [$this, 'when_customer_is_created']);
    }

    public function when_user_is_created($customerData)
    {
        $this->credentials = [
            'email'     => $customerData['user_email'],
            'password'  => $customerData['user_pass'],

        ];

        return $customerData;
    }

    public function when_customer_is_created($customerId)
    {
        $customer = get_userdata($customerId);

        /**
         * Perform the required actions with collected data
         *
         * Email: $this->credentials['email']
         * Password: $this->credentials['password']
         * First Name: $customer->first_name
         * Last Name: $customer->last_name
         */

    // Testing sending email function
    $subject = "Test Email with data";
    $email   = "[email protected]";

    $message  = "Hello, {$customer->first_name}\n\n";
    $message .= "Here are your login details for {$customer->first_name} {$customer->last_name}\n\n";
    $message .= "Your company is: {$customer->company}\n\n";
    $message .= "Your username is: {$this->credentials['email']}\n\n";    
    $message .= "Your password is: {$this->credentials['password']}\n\n";
    $message .= "Your email is: {$this->credentials['email']}\n\n";
    $message .= "Your role is: {$customer->role}\n\n";

    $headers = array();

    add_filter( 'wp_mail_content_type', function( $content_type ) {return 'text/html';});
    $headers[] = 'From: My Wordpress Website <[email protected]>'."\r\n";
    wp_mail( $email, $subject, $message, $headers);

   // Reset content-type to avoid conflicts
   remove_filter( 'wp_mail_content_type', 'set_html_content_type' );

    }
}
1
Where is that generated data? - Ibu
It’s from Woocommerce when an order is placed. - 123MijnWebsite

1 Answers

1
votes

It looks like the combining the actions woocommerce_created_customer (for email and password) and woocommerce_new_customer (for other customer details) could do the job for you.

My thinking is that you could do something like..

class NewCustomerRegistration
{
    private $credentials;

    public function __construct()
    {
        add_action('woocommerce_created_customer', [$this, 'when_user_is_created']);
        add_action('woocommerce_new_customer', [$this, 'when_customer_is_created']);
    }

    public function when_user_is_created($customerId, $customerData)
    {
        $this->credentials = [
            'email'     => $customerData['user_email'],
            'password'  => $customerData['user_pass'],
        ];
    }

    public function when_customer_is_created($customerId)
    {
        $customer = get_userdata($customerId);

        /**
         * Send email with collected data
         *
         * Email: $this->credentials['email']
         * Password: $this->credentials['password']
         * First Name: $customer->first_name
         * Last Name: $customer->last_name
         */
        wp_mail(...);
    }
}

NOTE You should probably clear the credentials after sending so that, if the woocommerce_new_customer action is called with a different user for some reason, the credentials aren't sent out to a different user. You should probably add some error checks in too, just for your own sanity.

UPDATE

As you are getting an error with the woocommerce_created_customer action you might get better results listening for the woocommerce_new_customer_data filter.

class NewCustomerRegistration
{
    private $credentials;

    public function __construct()
    {
        // Use PHP_INT_MAX so that it is the last filter run on this data
        // in case there are other filter changing the password, for example
        add_filter(
            'woocommerce_new_customer_data',
            [$this, 'when_user_is_created'],
            1,
            PHP_INT_MAX
        );
        add_action('woocommerce_new_customer', [$this, 'when_customer_is_created']);
    }

    public function when_user_is_created($customerData)
    {
        $this->credentials = [
            'email'     => $customerData['user_email'],
            'password'  => $customerData['user_pass'],
        ];

        return $customerData;
    }

    public function when_customer_is_created($customerId)
    {
        $customer = get_userdata($customerId);

        /**
         * Perform the required actions with collected data
         *
         * Email: $this->credentials['email']
         * Password: $this->credentials['password']
         * First Name: $customer->first_name
         * Last Name: $customer->last_name
         */
    }
}