0

I have a registration form where I programmatically create the user.

I need to be able to add that user to a membership plan already setup in WooCommerce and my current context is $user = new WP_User($userId);

I've looked everywhere and can't find a solution, even after digging through the code.

To be clear, I'm not doing this from an admin interface perspective, that part is clear. I'm doing this within PHP, immediately after the user registers their attendees (we automatically create users for their attendees and add them to the correct membership)

mujuonly
  • 11,370
  • 5
  • 45
  • 75
zanderwar
  • 3,440
  • 3
  • 28
  • 46
  • Your *already setup* should be known and clear. – revo Apr 19 '18 at 21:44
  • You misunderstood the question, I'm not doing this from an admin interface perspective, that part is clear yes. I'm doing this within PHP, immediately after the user registers their attendees (we automatically create users for their attendees and add them to the correct membership) – zanderwar Apr 19 '18 at 22:18
  • What "membership plan" means? WooCommerce doesn't have membership function I think. – Harkály Gergő Apr 20 '18 at 09:25

1 Answers1

0

What you are looking for is the "wc_memberships_create_user_membership()" function. Here's an example from the documentation:

$args = array(
        // Enter the ID (post ID) of the plan to grant at registration
        'plan_id'   => 253,
        'user_id'   => $user_id,
    );
wc_memberships_create_user_membership( $args );
GerritElbrink
  • 195
  • 3
  • 9