我已将Woocommerce设置为在订单完成后自动注册新客户。
现在,我想使用API POST请求将密码和其他已生成的内容发送给第三方,以创建相同的用户帐户。
我需要从Woocommerce生成的帐户发送的信息:
有人可以教我一个例子如何做到这一点吗?或为我指明正确的方向?我只是找不到任何开始。
我想过将Curl添加到Woocommerce谢谢页面webhook。但这不会发送未加密的密码,而只是保持空白。
希望有人知道完成此任务的简单方法。
谢谢!
最新更新:在我的函数中使用的代码。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 = "info@mydomain.com";
$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 <info@mydomain.com>'."\r\n";
wp_mail( $email, $subject, $message, $headers);
// Reset content-type to avoid conflicts
remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
}
}
答案 0 :(得分:1)
看起来woocommerce_created_customer
(用于电子邮件和密码)和woocommerce_new_customer
(用于其他客户详细信息)的组合操作可以为您完成这项工作。
我的想法是,您可以做类似的事情。
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(...);
}
}
注意 您可能应该在发送后清除凭据,这样,如果出于某种原因用其他用户调用了woocommerce_new_customer
操作,则凭据不会发送给其他用户。您可能还应该出于自己的理智而添加一些错误检查。
更新
当您在使用woocommerce_created_customer
操作时遇到错误时,使用woocommerce_new_customer_data
过滤器可能会获得更好的结果。
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
*/
}
}