简体   繁体   中英

Auto upgrade Wordpress user role for 1 year after WooCommerce purchase

I am looking for a way to upgrade a user's user role for 1 year after they buy a specific item in my WooCommerce store on my Wordpress website.

I already have the code in place to upgrade the user role after a purchase which is located in the functions.php file of the theme i am using:

add_action('woocommerce_thankyou', 'change_user_role_on_order_success');
function change_user_role_on_order_success($order_id ) {
$order = new WC_Order( $order_id );
$user_id = $order->user_id;
$wp_user_object = new WP_User($user_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 ( $product_id == '686' && $wp_user_object->roles[0] != "administrator"){ // Do not change admin role
wp_update_user( array( 'ID' => $wp_user_object->ID, 'role' => "55a015e6994e2" ) );
} else
if ( $product_id == '690' && $wp_user_object->roles[0] != "administrator"){ // Do not change admin role
wp_update_user( array( 'ID' => $wp_user_object->ID, 'role' => "55a011d082d75" ) );
}
}

As you can see in the code a user will get user role 55a015e6994e2 if they buy product id 686, and they get user role 55a011d082d75 when they buy product id 690.

This is all working fine. However i want the user role to revert back to role X after 1 year, and i've got no clue how to accomplish this.

I hope anyone with more Wordpress knowledge can help me out on this? Or point me in the right direction? Any help would be greatly appreciated.

I have already been looking at the Expire Users plugin which in fact can revert a user's user role back to user role x after x amount of time. But i've got no idea how i can apply it using a function or action in the functions.php file.

I have been fiddling around whit this some more and i was able to set a new user registration date using the user_registered function. The code now looks like this:

add_action('woocommerce_thankyou', 'change_user_role_on_order_success');
function change_user_role_on_order_success($order_id ) {
$today = current_time('timestamp');
$order = new WC_Order( $order_id );
$user_id = $order->user_id;
$wp_user_object = new WP_User($user_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 ( $product_id == '686' && $wp_user_object->roles[0] != "administrator"){ // Do not change admin role
wp_update_user( array( 'ID' => $wp_user_object->ID, 'role' => "55a015e6994e2", 'user_registered' => $today ) );
} else
if ( $product_id == '690' && $wp_user_object->roles[0] != "administrator"){ // Do not change admin role
wp_update_user( array( 'ID' => $wp_user_object->ID, 'role' => "55a011d082d75", 'user_registered' => $today ) );
}
}

So i thought if i set the Expire Users plugin to automatically expire users after one year it may do the trick but no such luck. The expire user plugin still shows the expiry date as one year after the user first registered, not one year from registration date. Bummer!

Anyone else out there has any ideas?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM