3

我正在使用 WooCommerce 会员资格的网站上工作。

我正在使用一个名为 的钩子wc_memberships_user_membership_saved,我想要的是显示我的订单的回顾。

我阅读了这份文档:https ://docs.woocommerce.com/document/woocommerce-memberships-admin-hook-reference/#wc_memberships_user_membership_created关于如何使用这个钩子。

我想测试这个钩子,所以这就是我在我的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' );

我应该收到一个布尔值:真或假。但我收到的是 WooCommerce 会员阵列产品。

问题是否来自参数?

4

2 回答 2

3

函数声明应该是:

function gu_memberships_user_membership_saved($plan, $args) {

然后$args将包含您引用的三个变量的数组,例如$args['user_id'].

于 2021-02-26T19:11:07.843 回答
2

您可以通过以下方式使用它,使用$body变量,您可以打印参数以查看它包含的内容

  • @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 );
于 2020-03-17T12:41:57.640 回答