如果订阅活动少于 3 个月,则暂时删除取消按钮
Temporarily Remove Cancel Button if Subscription Active less than 3 months
我需要一些帮助,暂时删除 "My Account" 中 "My Subscription" 页面中的取消按钮。我想隐藏取消按钮,直到用户订阅至少 3 个月(或 90 天)。在他们订阅 3 个月后,取消按钮将再次出现。
使用:Woocommerce 以及 Woo 订阅和 Woo 会员资格
我发现另一个问题已经回答了这个问题,但无论我如何编辑代码,我似乎都无法正常工作 ()。 下面的第一段代码来自 link.
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);
我已阅读找到的相关开发人员文档 here 并更改了您的代码,以便它使用当前站点日期并将其与订阅日期进行比较。
如果相差少于 3 个月,取消按钮将保持隐藏状态,直到相差至少 3 个月。
请注意,我使用 'last_payment'
日期进行比较,其他可能使用的选项是 'start'
、'trial_end'
、'next_payment'
、'last_payment'
或 'end'
。
阅读更多相关信息 here。
/**
* Remove cancel button ( When last payment was less then 3 months ago )
*
* @param array $actions, action array.
* @param int $subscription_id, the id of the current subscription.
* @return array $actions, the altered action array.
*/
function remove_cancel_button( $actions, $subscription_id ) {
// Gets the subscription object on subscription id
$subscription = new WC_Subscription( $subscription_id );
// Get last payment date from subscription object, uses the sites timezone setting
$last_payment_date = $subscription->get_date( 'last_payment', 'site' );
$last_payment_date = new DateTime( $last_payment_date );
// The current date/time, uses the sites timezone setting
$today = new DateTime( current_time('mysql') );
// Get the difference in date
$interval = $today->diff( $last_payment_date );
// Check if interval is less then 3 months
if( $interval->m < 3 ){
unset( $actions['cancel'] );
}
// Return the actions
return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'remove_cancel_button', 100, 2);
希望这对您有所帮助,如果有任何不清楚的地方,请告诉我。
我需要一些帮助,暂时删除 "My Account" 中 "My Subscription" 页面中的取消按钮。我想隐藏取消按钮,直到用户订阅至少 3 个月(或 90 天)。在他们订阅 3 个月后,取消按钮将再次出现。
使用:Woocommerce 以及 Woo 订阅和 Woo 会员资格
我发现另一个问题已经回答了这个问题,但无论我如何编辑代码,我似乎都无法正常工作 (
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);
我已阅读找到的相关开发人员文档 here 并更改了您的代码,以便它使用当前站点日期并将其与订阅日期进行比较。
如果相差少于 3 个月,取消按钮将保持隐藏状态,直到相差至少 3 个月。
请注意,我使用 'last_payment'
日期进行比较,其他可能使用的选项是 'start'
、'trial_end'
、'next_payment'
、'last_payment'
或 'end'
。
阅读更多相关信息 here。
/**
* Remove cancel button ( When last payment was less then 3 months ago )
*
* @param array $actions, action array.
* @param int $subscription_id, the id of the current subscription.
* @return array $actions, the altered action array.
*/
function remove_cancel_button( $actions, $subscription_id ) {
// Gets the subscription object on subscription id
$subscription = new WC_Subscription( $subscription_id );
// Get last payment date from subscription object, uses the sites timezone setting
$last_payment_date = $subscription->get_date( 'last_payment', 'site' );
$last_payment_date = new DateTime( $last_payment_date );
// The current date/time, uses the sites timezone setting
$today = new DateTime( current_time('mysql') );
// Get the difference in date
$interval = $today->diff( $last_payment_date );
// Check if interval is less then 3 months
if( $interval->m < 3 ){
unset( $actions['cancel'] );
}
// Return the actions
return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'remove_cancel_button', 100, 2);
希望这对您有所帮助,如果有任何不清楚的地方,请告诉我。