我的WooCommerce网站上有3个订阅。订阅计划之一是“年度付费每月”。这很重要,意味着至少有12个月的期限,但要按月支付。 WooCommerce本身不支持此功能。
我想做的是检测用户是否在特定的订阅上,如果是,隐藏“取消”按钮,直到订阅超过11个月。
我在下面发现了这种情况,在所有情况下都隐藏着“取消”按钮。我正在寻找一种检查订阅是否为XXX的方法,如果是,请隐藏“取消”按钮
/**
* Only copy the opening php tag if needed
*/
function sv_edit_my_memberships_actions( $actions ) {
// remove the "Cancel" action for members
unset( $actions['cancel'] );
return $actions;
}
add_filter( 'wc_memberships_members_area_my-memberships_actions', 'sv_edit_my_memberships_actions' );
add_filter( 'wc_memberships_members_area_my-membership-details_actions', 'sv_edit_my_memberships_actions' );
答案 0 :(得分:3)
首先,您需要枚举用户当前的活动订阅,并检测我们将使用 wc_memberships_get_user_active_memberships 进行过滤的订阅,并将开始日期与当前日期进行比较。我提供了一段代码,可能会对您有帮助:)
function sv_edit_my_memberships_actions( $actions ) {
// Get the current active user
$user_id = wp_get_current_user();
if(!$user_id) // No valid user, abort
return $actions;
// Only query active subscriptions
$memberships_info = wc_memberships_get_user_active_memberships($user_id, array(
'status' => array( 'active' ),
));
// Loop through each active subscription
foreach ($memberships_info as $membership) {
$subscription_start_date = date("Y/m/d", strtotime($membership->get_start_date()));
//$subscription_end_date = date("Y/m/d", strtotime($membership->get_end_date()));
//$subscription_name = $membership->get_plan()->get_name();
//$subscription_id = $membership->get_plan()->get_id();
if($subscription_id == 'YOUR_ID') { // Active subscription
// Compare the starting date of the subscription with the current date
$datetime1 = date_create($subscription_start_date);
$datetime2 = date_create(date(time()));
$interval = date_diff($datetime1, $datetime2);
if($interval->format('%m') <= 11) {
// remove the "Cancel" action for members
unset( $actions['cancel'] );
}
}
}
return $actions;
}
add_filter( 'wc_memberships_members_area_my-memberships_actions', 'sv_edit_my_memberships_actions' );
add_filter( 'wc_memberships_members_area_my-membership-details_actions', 'sv_edit_my_memberships_actions' );