为此,您将需要:
- 检测当前用户是否有活动订阅的条件函数
woocommerce_subscriptions_message_store_url
一个挂在过滤器钩子中的自定义函数,您将在其中定义链接到您的产品的新 URL。
这是代码:
// Conditional function detecting if the current user has an active subscription
function has_active_subscription( $user_id=null ) {
// if the user_id is not set in function argument we get the current user ID
if( null == $user_id )
$user_id = get_current_user_id();
// Get all active subscrptions for a user ID
$active_subscriptions = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_subscription', // Subscription post type
'post_status' => 'wc-active', // Active subscription
) );
// if user has an active subscription
if( ! empty( $active_subscriptions ) ) return true;
else return false;
}
// Change woocommerce subscription “shop” link inside my account page
add_filter( 'woocommerce_subscriptions_message_store_url', 'custom_subscriptions_message_store_url', 10, 1 );
function custom_subscriptions_message_store_url( $url ){
// If current user has NO active subscriptions
if( ! has_active_subscription() ){
// HERE Define below the new URL linked to your product.
$url = home_url( "/product-category/myproduct/" );
}
return $url;
}
此代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。
测试和工作