ОБНОВЛЕНИЕ
Обычно эта обновленная версия кода должна работать с 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'
, если вы хотите быть уверены (во всех возможных случаях), что клиент, который купит один из ваших двух конкретных продуктов, будет иметь его роль изменилась с 'customer
' на 'author'
, когда статус заказа установлен на 'completed'
, я рекомендую вам попробовать использовать этот крючок для уведомлений по электронной почте ( если первый фрагмент кода не работает).
Например, здесь я использую хук 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-файлах плагина.
person
LoicTheAztec
schedule
07.01.2017