Hi @BOLA
I’m currently adding monthly membership to my WC Vendors website and encountered the same issue as you. I have noticed a few posts from you trying to figure this one out, so I thought I would add how I got around this to help you out 🙂
Firstly, I took the code that @FERVOUS kindly provided, and then added a couple of extra functions within her if statements.
Basically what happens here is this: When a user cancels their membership they are downgraded to customer role (as per FERVOUS’ code), and then an extra function is fired that automatically marks all their products to draft (so not to show them in the shop). Now, a membership might be cancelled due to a declined payment and not because the vendor actually cancelled their membership. If the vendor signs up again with a new (or corrected) payment method, we don’t want the vendor to have to go through and manually publish all their products again. Therefor we add the same function again within the function that sets their role to vendor, but doing the opposite (set all products to published again).
*I am not a very good coder*, I simply managed to get this function to work through trial and error – but it does seem to work exactly how I need it to on my development site.
NOTE: I have removed the option for vendors to save draft products. If you allow them to save draft products, this could cause an issue as they would also get auto published too.
Perhaps @FERVOUS and @BEN could have a quick look over to see if you spot anything that could pose any potential issues?
// PAID MEMBERSHIPS PRO — UPGRADE TO VENDOR, DOWNGRADE TO SUBSCRIBER – AND UPDATE PRODUCT STATUS
/*
Members signing up for membership level #1 get “Vendor” role.
Members cancelling are given the customer role.
Admin users are ignored.
Vendor products status is updated to draft if membership cancelled or published if active.
*/
function my_pmpro_after_change_membership_level($level_id, $user_id)
{
//get user object
$wp_user_object = new WP_User($user_id);
//ignore admins
if(in_array(“administrator”, $wp_user_object->roles))
return;
if($level_id == 1)
{
function update_product_status( $user_id, $new_role, $old_roles ) {
global $wpdb;
$update = array(
‘post_status’ => ‘publish’
);
$where = array(
‘post_type’ => ‘product’,
‘post_status’ => ‘draft’,
‘post_author’ => $user_id
);
$updated = $wpdb->update( $wpdb->posts, $update, $where );
}
add_action( ‘set_user_role’, ‘update_product_status’, 10, 3 );
//New member of level #1. Give them Vendor role.
$wp_user_object->set_role(‘vendor’);
}
elseif($level_id == 0)
{
function update_product_status( $user_id, $new_role, $old_roles ) {
global $wpdb;
$update = array(
‘post_status’ => ‘draft’
);
$where = array(
‘post_type’ => ‘product’,
‘post_status’ => ‘publish’,
‘post_author’ => $user_id
);
$updated = $wpdb->update( $wpdb->posts, $update, $where );
}
add_action( ‘set_user_role’, ‘update_product_status’, 10, 3 );
//Cancelling. Give them Customer role.
$wp_user_object->set_role(‘customer’);
}
}
add_action(‘pmpro_after_change_membership_level’, ‘my_pmpro_after_change_membership_level’, 10, 2);