【问题标题】:Hide cancel button between the 20th and the 3rd of the month (Woocommerce Subscriptions)在每月 20 日和 3 日之间隐藏取消按钮(Woocommerce 订阅)
【发布时间】:2021-11-09 21:14:10
【问题描述】:

我正在处理 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 );

谢谢!

【问题讨论】:

    标签: php wordpress woocommerce woocommerce-subscriptions


    【解决方案1】:

    您可以像这样检查今天的日期是在当月 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 );
    

    【讨论】:

    • 非常感谢您的帮助,但它不起作用:(我试了一下,发现了两个问题:1/取消按钮再次出现在所有产品上(不仅在产品“1812”上) ). 2/我尝试将“20”修改为“14”以检查取消按钮是否会消失,但它仍然在这里。
    • 嘿@AlexisPereira 感谢您的反馈!我的日期检查是在您的产品 ID 检查之后,所以问题 1/ 不应该发生。直到该行代码与您的完全相同,所以我很惊讶。您的代码是否可以在产品 1812 上禁用?
    • 关于问题 2/ 这很奇怪,我会在我的测试环境中检查一下
    • 我确认,问题 2/ 没有在我的测试网站上发生,如果我输入,取消按钮会被正确删除,例如"> 3 &&
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 2016-11-02
    相关资源
    最近更新 更多