【问题标题】:Disable cancel subscription for a specific product in WooCommerce在 WooCommerce 中禁用特定产品的取消订阅
【发布时间】:2021-09-09 11:20:47
【问题描述】:

我正在研究 Woocommerce 订阅。此代码禁用用户帐户上的取消订阅按钮:

<?php
/**
 * Plugin Name: Remove Subscription Action Buttons from My Account
 * Plugin URI: https://gist.github.com/thenbrent/8851287/
 * Description: Remove any given button from the <a href="http://docs.woothemes.com/document/subscriptions/customers-view/#section-2">My Subscriptions</a> table on the My Account page. By default, only the "Change Payment Method" button is removed, but you can uncomment additional actions to remove those buttons also.
 * Author: Brent Shepherd
 * Author URI:
 * Version: 2.0
 */

/**
 * 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 ) {

    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 );

原文来源:https://gist.github.com/thenbrent/8851287

但我想对此例外:取消按钮必须出现在特定产品(1 个月订阅产品)中

我怎样才能做到这一点?

【问题讨论】:

    标签: php wordpress woocommerce woocommerce-subscriptions


    【解决方案1】:

    在你的函数声明之后:

    function eg_remove_my_subscriptions_button( $actions, $subscription ) {
    // etc.
    

    添加这个:

    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() == 123456 ) {
                $is_my_product = true;
                break;
             }
         }
       }
       if ( $is_my_product ) return $actions;
    

    【讨论】:

    • 显然,将 123456 更改为您的目标产品 ID
    • 谢谢,它的工作就像一个魅力!如果您不介意回答,我在下面还有一个问题!再次感谢。
    猜你喜欢
    • 2017-12-03
    • 2023-01-25
    • 2021-01-21
    • 1970-01-01
    • 2019-07-29
    • 2021-01-25
    • 2021-02-09
    • 2021-01-10
    • 2017-06-18
    相关资源
    最近更新 更多