gpt4 book ai didi

php - 在 WooCommerce 订阅中获取用户 ID

转载 作者:可可西里 更新时间:2023-11-01 00:56:04 29 4
gpt4 key购买 nike

我正在使用 WooCommerce 订阅插件,我正在尝试获取给定 wc_subscription 的客户或用户 ID。

这是我一直在使用但失败的代码:

add_action( 'woocommerce_scheduled_subscription_trial_end', 'registration_trial_expired', 100 );
function registration_trial_expired( $wc_subscription ) {
mail("example@gmail.com", "Expired", "Someone's order has expired");
$userid = $wc_subscription->customer_user;
mail("example@gmail.com", "Expired", "Someone's order has expired with customer".$userid);
...
}

我以为 $wc_subscription->customer_user 会有用户标识,但它是空的。实际上阻​​止了代码继续。

如何使用 $wc_subscription 获取用户 ID?

最佳答案

As class WC_Subscription methods are inherited from WC_Abstract_Order and WC_Order classes, you can use get_user_id() method this way:

$userid = $wc_subscription->get_user_id();

此代码已经过测试并可与 WC_Subscription 实例对象一起使用

所以你的代码将是:

add_action( 'woocommerce_scheduled_subscription_trial_end', 'registration_trial_expired', 100 );
function registration_trial_expired( $wc_subscription ) {
mail("example@gmail.com", "Expired", "Someone's order has expired");
$userid = $wc_subscription->get_user_id(); // <= HERE
mail("example@gmail.com", "Expired", "Someone's order has expired with customer".$userid);
// ...
}

Update (on OP's comment)

因为参数 $wc_subscription 是订阅 ID (而不是订阅对象)

所以我将代码更改为:

add_action( 'woocommerce_scheduled_subscription_trial_end', 'registration_trial_expired', 100 );
function registration_trial_expired( $subscription_id ) {

// Get an occurrence of the WC_Subscription object
$subscription = wcs_get_subscription( $subscription_id );

// Get the user ID (or customer ID)
$user_id = $subscription->get_user_id();

// The email adress
$email = 'example@gmail.com';

// The theme domain (replace with your theme domain for localisable strings)
$domain = 'woocommerce';

mail( $email, 'Expired', __("Someone’s order has expired", $domain);
mail( $email, 'Expired', __("Someone’s order has expired with customer", $domain) . $user_id );

// ...
}

关于php - 在 WooCommerce 订阅中获取用户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43108453/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com