How to use WooCommerce Membership hook How to use WooCommerce Membership hook wordpress wordpress

How to use WooCommerce Membership hook


The function declaration should be:

function gu_memberships_user_membership_saved($plan, $args) {

Then the $args will contain an array of the three variables that you are referencing, such as $args['user_id'].


You can use it in the following way, with the $body variable, you can print the parameter(s) to see what it contains

  • @type int|string $user_id user ID for the membership
  • @type int|string $user_membership_id post ID for the new user membership
  • @type bool $is_update true if the membership is being updated, false if new
/** * Fires after a user has been granted membership access * * This hook is similar to wc_memberships_user_membership_created * but will also fire when a membership is manually created in admin * * @since 1.3.8 * @param WC_Memberships_Membership_Plan $membership_plan The plan that user was granted access to * @param array $args */function action_wc_memberships_user_membership_saved( $user_id, $user_membership_id, $is_update = 0 ) {    $to = 'mail@test.com';    $subject = 'The subject';    $body = '<pre>', print_r( $user_membership_id, 1 ), '</pre>';    $headers = array('Content-Type: text/html; charset=UTF-8');    wp_mail( $to, $subject, $body, $headers );}add_action( 'wc_memberships_user_membership_saved', 'action_wc_memberships_user_membership_saved', 10, 3 );