I’m using the Groups WooCommerce plugin to handle some group memberships and I’m trying to add a few groups through PHP. I’m using the code below to add the user to a group and set their membership duration. They’re successfully added, but the membership duration isn’t correctly applied. I had found some API documentation, but it doesn’t cover anything about setting a duration.

Groups_User_Group::create( array( 'user_id' => $userID, 'group_id' => $group ) );
Groups_WS_Terminator::schedule_termination( $length, $userID, $group );

I also tried adding some of the code directly from the class-groups-ws-handler.php class to my method to see if that would help. The order id and product id are being passed to this method.

$groups_product_groups[$order_id][$product_id]['version'] = GROUPS_WS_VERSION;
$groups_product_groups[$order_id][$product_id]['start']   = $start;
$groups_product_groups[$order_id][$product_id]['groups']  = $product_groups;
update_user_meta( $userID, '_groups_product_groups', $groups_product_groups );

$groups_product_groups[$order_id][$product_id]['duration'] = get_post_meta( $product_id, '_groups_duration', true );
$groups_product_groups[$order_id][$product_id]['duration_uom'] = get_post_meta( $product_id, '_groups_duration_uom', true );
update_user_meta( $userID, '_groups_product_groups', $groups_product_groups );

Still nothing.

I saw in the database that the _groups_buckets user meta key had array entries for the groups with the correct timestamps, but the backend user profile doesn’t reflect this, and the group is never terminated.

Does anyone have any suggestions on how to set the membership duration through PHP?

Read more here: Groups WooCommerce set membership duration programmatically


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: