![](/img/trans.png)
[英]Auto change order status to completed for specific products in WooCommerce
[英]Change the user role on purchase for specific products when order status is completed
所以我幫助某人推出了一個網站,當有人購買特定產品時,他們想要一個打折產品。 我找到了一個解決方案並實施了它,它在網站發布時起作用,並且在購買產品時不再改變客戶的角色。 我試圖獲得Woothemes的支持,他們不支持自定義,並希望他們購買129美元的擴展來處理這個問題。
有沒有人有這個仍然有效的解決方案?
這是我的代碼:
// Update User on purchase https://gist.github.com/troydean/9322593
function lgbk_add_member( $order_id ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
}
if ( $order->user_id > 0 && $product_id == '247' || $order->user_id > 0 && $product_id == '255') {
update_user_meta( $order->user_id, 'paying_customer', 1 );
$user = new WP_User( $order->user_id );
// Remove role
$user->remove_role( 'customer' );
// Add role
$user->add_role( 'author' );
}
}
add_action( 'woocommerce_order_status_completed', 'lgbk_add_member' );
UPDATE
通常,此更新的代碼版本應與工作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個特定產品之一的客戶將從'customer
更改其角色'customer
當訂單狀態設置為'completed'
, 'customer
'到'author'
,我建議您嘗試使用此電子郵件通知掛鈎(如果第一個代碼段不起作用)。
例如,我在這里使用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文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.