3

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?

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
user3653409
  • 129
  • 11

2 Answers2

3

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'].

John Conde
  • 217,595
  • 99
  • 455
  • 496
Jess Izen
  • 31
  • 1
  • This was OP's question, just clarifying usage. The other answer might be referring to an older method reference - it's definitely expecting two variable declarations, one of which is an object, the other of which is an array. – Jess Izen Mar 01 '21 at 15:19
  • I think this should be the accepted answer. – ice cream Mar 03 '21 at 01:33
2

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 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50