问题
I have a need to build two plans (paid) on my site. If the user buys Gold Plan it should create a user (role) Gold and give him 20% discount on travel packages. If user buys platinum wp should create 'Platinum' user role for that customer. Now I have found the code online but it does not work:
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' );
}
}
Now I have put this code in function.php file of a current active (child) theme but when I test it and buy the product wordpress keeps on creating 'customer' user. Is something wrong with my code?
回答1:
Updated
Your code is outdated and with some mistakes. Try the following, that will change the user role based on purchased product when order is completed ("completed" status):
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
}
}
}
}
Code goes in function.php file of your active child theme (or active theme). It should work now.
Update: As you are using the following code to autocomplete orders:
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' );
}
You can include in it the user role change based on specific products. So try the following code will replace your existing function:
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' );
}
Code goes in function.php file of your active child theme (or active theme). This should also work, merging both functions in one.
来源:https://stackoverflow.com/questions/57639861/changing-user-role-after-purchasing-a-products-in-woocommerce