/** * Programmatically create a user membership * Example: automatically grant membership access to a plan at WP user registration * Requires Memberships 1.3+ * * @param int $user_id the ID of the newly created user */ function sv_wc_memberships_user_membership_at_registration($user_id) { // bail if Memberships isn't active if (!function_exists('wc_memberships')) { return; } $args = array('plan_id' => 253, 'user_id' => $user_id); // magic! wc_memberships_create_user_membership($args); // Optional: get the new membership and add a note so we know how this was registered. $user_membership = wc_memberships_get_user_membership($user_id, $args['plan_id']); $user_membership->add_note('Membership access granted automatically from registration.'); }
/** * Grant a user access to this plan from a purchase * * @since 1.0.0 * @param int $user_id User ID * @param int $product_id Product ID * @param int $order_id Order ID * @return int|null New/Existing User Membership ID or null on failure */ public function grant_access_from_purchase($user_id, $product_id, $order_id) { $user_membership_id = null; $action = 'create'; // Check if user is perhaps a member, but membership is expired/cancelled if (wc_memberships_is_user_member($user_id, $this->get_id())) { $user_membership = wc_memberships_get_user_membership($user_id, $this->get_id()); $user_membership_id = $user_membership->get_id(); // Do not allow the same order to renew or reactivate the membership. This // prevents admins changing order statuses from extending/reactivating the // membership. $order_ids = get_post_meta($user_membership_id, '_order_id'); if (!empty($order_ids) && in_array($order_id, $order_ids)) { return null; } // Otherwise... continue as usual $action = 'reactivate'; if (wc_memberships_is_user_active_member($user_id, $this->get_id())) { /** * Filter whether an already active membership will be renewed * * @since 1.0.0 * @param bool $renew * @param WC_Memberships_Membership_Plan $plan * @param array $args */ $renew_membership = apply_filters('wc_memberships_renew_membership', (bool) $this->get_access_length_amount(), $this, array('user_id' => $user_id, 'product_id' => $product_id, 'order_id' => $order_id)); if (!$renew_membership) { return null; } $action = 'renew'; } } // Create/update the user membership $user_membership = wc_memberships_create_user_membership(array('user_membership_id' => $user_membership_id, 'user_id' => $user_id, 'product_id' => $product_id, 'order_id' => $order_id, 'plan_id' => $this->get_id()), $action); // Add membership note $product = wc_get_product($product_id); $order = wc_get_order($order_id); $user_membership->add_note(sprintf(__('Membership access granted from purchasing %s (Order %s)'), $product->get_title(), $order->get_order_number())); /** * Fires after a user has been granted membership access from a purchase * * @since 1.0.0 * @param WC_Memberships_Membership_Plan $membership_plan The plan that user was granted access to * @param array $args */ do_action('wc_memberships_grant_membership_access_from_purchase', $this, array('user_id' => $user_id, 'product_id' => $product_id, 'order_id' => $order_id, 'user_membership_id' => $user_membership->get_id())); return $user_membership->get_id(); }