首页 > 解决方案 > 在每月 20 日和 3 日之间隐藏取消按钮(Woocommerce 订阅)

问题描述

我正在研究 Woocommerce 订阅。

下面的代码隐藏了“取消”和“重新激活”按钮,特定产品 (ID:1812) 除外。

我想更深入地隐藏每月 20 日和 3 日之间的“取消”和“重新激活”按钮

你能帮我实现吗?

/**
 * Remove the "Change Payment Method" button from the My Subscriptions table.
 *
 * This isn't actually necessary because @see eg_subscription_payment_method_cannot_be_changed()
 * will prevent the button being displayed, however, it is included here as an example of how to
 * remove just the button but allow the change payment method process.
 */
function eg_remove_my_subscriptions_button( $actions, $subscription ) {
   $is_my_product = false;
   if ( sizeof( $subscription_items = $subscription->get_items() ) > 0 ) {
     foreach ( $subscription_items as $item_id => $item ) {
         $product = $item->get_product();
         if ( $product->get_id() == 1812 ) {
            $is_my_product = true;
            break;
         }
     }
   }
   if ( $is_my_product ) return $actions;

    foreach ( $actions as $action_key => $action ) {
        switch ( $action_key ) {
            case 'change_payment_method':   // Hide "Change Payment Method" button?
//          case 'change_address':      // Hide "Change Address" button?
//          case 'switch':          // Hide "Switch Subscription" button?
//          case 'resubscribe':     // Hide "Resubscribe" button from an expired or cancelled subscription?
//          case 'pay':         // Hide "Pay" button on subscriptions that are "on-hold" as they require payment?
            case 'reactivate':      // Hide "Reactive" button on subscriptions that are "on-hold"?
            case 'cancel':          // Hide "Cancel" button on subscriptions that are "active" or "on-hold"?
                unset( $actions[ $action_key ] );
                break;
            default: 
                error_log( '-- $action = ' . print_r( $action, true ) );
                break;
        }
    }

    return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'eg_remove_my_subscriptions_button', 100, 2 );

谢谢!

标签: phpwordpresswoocommercewoocommerce-subscriptions

解决方案


您可以像这样检查今天的日期是在当月 19 日之后还是在当月 4 日之前。

获取今天的日期:

date( 'j' )

检查是否在 $n->$m 范围内:

date( 'j' ) > $n-1 && date( 'j' ) < $m+1

最后:

function eg_remove_my_subscriptions_button( $actions, $subscription ) {
   $is_my_product = false;
   if ( sizeof( $subscription_items = $subscription->get_items() ) > 0 ) {
     foreach ( $subscription_items as $item_id => $item ) {
         $product = $item->get_product();
         if ( $product->get_id() == 1812 ) {
            $is_my_product = true;
            break;
         }
     }
   }
   if ( $is_my_product ) return $actions;

   if ( date( 'j' ) > 3 && date( 'j' ) < 20 ) return $actions;

    foreach ( $actions as $action_key => $action ) {
        switch ( $action_key ) {
            case 'change_payment_method':   // Hide "Change Payment Method" button?
//          case 'change_address':      // Hide "Change Address" button?
//          case 'switch':          // Hide "Switch Subscription" button?
//          case 'resubscribe':     // Hide "Resubscribe" button from an expired or cancelled subscription?
//          case 'pay':         // Hide "Pay" button on subscriptions that are "on-hold" as they require payment?
            case 'reactivate':      // Hide "Reactive" button on subscriptions that are "on-hold"?
            case 'cancel':          // Hide "Cancel" button on subscriptions that are "active" or "on-hold"?
                unset( $actions[ $action_key ] );
                break;
            default: 
                error_log( '-- $action = ' . print_r( $action, true ) );
                break;
        }
    }

    return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'eg_remove_my_subscriptions_button', 100, 2 );

推荐阅读