Skip to content
Advertisement

How to use WooCommerce Membership hook

I’m working on a website using WooCommerce membership.

I’m using a hook called wc_memberships_user_membership_saved, What I want is to display a recap of my order.

I read this documentation: https://docs.woocommerce.com/document/woocommerce-memberships-admin-hook-reference/#wc_memberships_user_membership_created on how to use this hook.

I want to test this hook so this is what I did in my functions.php

function gu_memberships_user_membership_saved($user_id,$user_membership_id,$is_update) {
    
    $to = 'mail@test.com';
    $subject = 'The subject';
    $body = '<pre>' . print_r($is_update,true) . '</pre>';
    $headers = array('Content-Type: text/html; charset=UTF-8');
    
    wp_mail( $to, $subject, $body, $headers );
  
}
add_action( 'wc_memberships_user_membership_saved', 'gu_memberships_user_membership_saved' );

I should receive a boolean: true or false. But I receive the WooCommerce membership array product instead.

Does the problem come from the params?

Advertisement

Answer

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 );
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement