更新
通常,此更新的代码版本应该与 woocommerce_order_status_completed 一起使用,然后您应该在之前尝试此代码。
(此代码还与下一个即将推出的主要 WooCommerce 更新 2.7 兼容)。
代码如下:
add_action( 'woocommerce_order_status_completed', 'custom_action_on_completed_customer_email_notification' );
function custom_action_on_completed_customer_email_notification( $order_id ) {
// Set HERE your targetted products IDs:
$targetted_products = array( 247, 255 );
$order = wc_get_order( $order_id );
if ( $order->get_user_id() > 0 ) {
foreach ( $order->get_items() as $order_item ) {
// Here we detect if the a target product is part of this order items
if ( in_array( $order_item['product_id'], $targetted_products ) ){
// I think tha this is not really needed as it's set when an order has been paid…
update_user_meta( $order->get_user_id(), 'paying_customer', 1 ); // 1 => true
// Remove all roles and set 'editor' as user role (for current user)
$user = new WP_User( $order->get_user_id() );
$user->set_role( 'author' );
// Product is found, we break the loop…
break;
}
}
}
}
但由于我不知道您的订单如何更改为 'completed' 状态,如果您想确定(在所有可能的情况下)客户将购买您的 2特定产品,当订单状态设置为 'completed' 时,他的角色将从 'customer' 更改为 'author',我会建议您尝试使用此电子邮件通知挂钩(如果第一个代码 sn-p 不起作用)。
例如,这里我使用 woocommerce_email_before_order_table 钩子,它将在某些条件的帮助下在“已完成订单客户电子邮件通知”时执行和触发。
(此代码还与下一个即将推出的主要 WooCommerce 更新 2.7 兼容)。
这是您重新访问和测试的代码:
add_action( 'woocommerce_email_before_order_table', 'custom_action_on_completed_customer_email_notification', 10, 4 );
function custom_action_on_completed_customer_email_notification( $order, $sent_to_admin, $plain_text, $email ) {
if( 'customer_completed_order' == $email->id ){
// Set HERE your targetted products IDs:
$targetted_products = array( 247, 255 );
if ( $order->get_user_id() > 0 ) {
foreach ( $order->get_items() as $order_item ) {
// Here we detect if the a target product is part of this order items
if ( in_array( $order_item['product_id'], $targetted_products ) ){
// I think tha this is not really needed as it's set when an order has been paid…
update_user_meta( $order->get_user_id(), 'paying_customer', 1 ); // 1 => true
// Remove all roles and set 'editor' as user role (for current user)
$user = new WP_User( $order->get_user_id() );
$user->set_role( 'author' );
// Product is found, we break the loop…
break;
}
}
}
}
}
代码进入您活动的子主题(或主题)的 function.php 文件中。或者也可以在任何插件 php 文件中。