我需要一些帮助,以暂时删除“我的帐户”中“我的订阅”页面中的取消按钮。我想隐藏取消按钮,直到用户订阅至少3个月(或90天)为止。订阅3个月后,取消按钮将再次出现。
使用:Woocommerce以及Woo订阅和Woo会员资格
我发现了另一个已回答了这个问题的问题,但是无论我进行多少编辑(Disable Cancel Subscription for single subscription in WooCommerce),我似乎都无法使代码正常工作。 下面的第一段代码来自链接。
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;
}
我已经能够在下面的代码中隐藏“取消”按钮,但是它会无限期地隐藏它:
function remove_cancel_button( $actions, $subscription ) {
foreach ( $actions as $action_key => $action ) {
switch ( $action_key ) {
case 'cancel': // Remove the cancel button
unset( $actions[ $action_key ] );
break;
default:
error_log( '-- $action = ' . print_r( $action, true ) );
break;
}
}
return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'remove_cancel_button', 100, 2);