[英]Changing user role after purchasing a products in WooCommerce
我需要在我的網站上建立兩個計划(付費)。 如果用戶購買黃金計划,它應該創建一個用戶(角色)黃金並給他 20% 的旅行套餐折扣。 如果用戶購買白金 wp 應為該客戶創建“白金”用戶角色。 現在我在網上找到了代碼,但它不起作用:
add_action( 'woocommerce_order_status_completed',
'wpglorify_change_role_on_purchase' );
function wpglorify_change_role_on_purchase( $order_id ) {
// get order object and items
$order = new WC_Order( $order_id );
$items = $order->get_items();
$product_id = 85; // that's a specific product ID
foreach ( $items as $item ) {
if( $product_id == $item['product_id'] && $order->user_id ) {
$user = new WP_User( $order->user_id );
// Remove role
$user->remove_role( 'customer' );
// Add role
$user->add_role( 'gold' );
}
}
$product_id = 86; // that's a specific product ID
foreach ( $items as $item ) {
if( $product_id == $item['product_id'] && $order->user_id ) {
$user = new WP_User( $order->user_id );
// Remove role
$user->remove_role( 'customer' );
// Add role
$user->add_role( 'platinum' );
}
}
現在我已將此代碼放在當前活動(子)主題的 function.php 文件中,但是當我測試它並購買產品時,wordpress 繼續創建“客戶”用戶。 我的代碼有問題嗎?
更新
您的代碼已過時且存在一些錯誤。 嘗試以下操作,這將在訂單完成(“已完成”狀態)時根據購買的產品更改用戶角色:
add_action( 'woocommerce_order_status_completed', 'wpglorify_change_role_on_purchase', 10, 2 );
function wpglorify_change_role_on_purchase( $order_id, $order ) {
$gold_product_id = 85; // specific product ID for "gold" user role
$platinium_product_id = 86; // specific product ID for "platinium" user role
if( $user_id = $order->get_customer_id() ) {
// Get the WP_User Object
$wp_user = new WP_User( $user_id );
foreach ( $order->get_items() as $item ) {
// For "gold" user role
if ( $gold_product_id == $item->get_product_id() && $order->get_user_id() > 0 ) {
$user->remove_role( 'customer' ); // Remove 'customer' user role
$user->add_role( 'gold' ); // Add 'gold' user role
}
// For "platinum" user role
elseif ( $platinium_product_id == $item->get_product_id() && $order->get_user_id() > 0 ) {
$user->remove_role( 'customer' ); // Remove 'customer' user role
$user->add_role( 'platinum' ); // Add 'platinum' user role
}
}
}
}
代碼位於活動子主題(或活動主題)的 function.php 文件中。 它現在應該可以工作了。
更新:當您使用以下代碼自動完成訂單時:
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$order->update_status( 'completed' );
}
您可以在其中包含基於特定產品的用戶角色更改。 因此,嘗試以下代碼將替換您現有的功能:
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
// Get an instance of the WC_Order Object
$order = wc_get_order( $order_id );
// Only for logged in "customer" user role
if ( current_user_can( 'customer' ) ) {
$gold_product_id = 85; // specific product ID for "gold" user role
$platinium_product_id = 86; // specific product ID for "platinium" user role
$user_id = $order->get_customer_id(); // The user Id
// Get the WP_User Object
$wp_user = new WP_User( $user_id );
foreach ( $order->get_items() as $item ) {
// For "gold" user role
if ( $gold_product_id == $item->get_product_id() && $order->get_user_id() > 0 ) {
$user->remove_role( 'customer' ); // Remove 'customer' user role
$user->add_role( 'gold' ); // Add 'gold' user role
}
// For "platinum" user role
elseif ( $platinium_product_id == $item->get_product_id() && $order->get_user_id() > 0 ) {
$user->remove_role( 'customer' ); // Remove 'customer' user role
$user->add_role( 'platinum' ); // Add 'platinum' user role
}
}
}
$order->update_status( 'completed' );
}
代碼位於活動子主題(或活動主題)的 function.php 文件中。 這也應該有效,將兩個功能合二為一。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.