由于类WC_Subscription 方法WC_Abstract_Order
是从和类继承的WC_Order
,因此您可以这样使用get_user_id()
方法:
$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);
// ...
}
更新 (关于 OP 的评论)
因为参数$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 );
// ...
}