如何将wordpress / php功能应用于特定的产品类别

时间:2016-04-08 18:08:39

标签: php wordpress woocommerce

重新。 Wordpress,WooCommerce,Woo订阅

我想使用以下功能(https://gist.github.com/thenbrent/8851287)隐藏某些“操作”按钮,但我只想将其应用于我的wordpress产品的一个类别。

我该怎么做?

<?php
 * 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 );

更多信息:

有问题的页面是WooCommerce订阅附带的视图订阅页面:/ mysite / my-account / view-subscription / 4606 /

以下建议基于捕获当前类别ID,例如:

$cat_id = $wp_query->get_queried_object()->term_id; // get current category id).

我担心这不起作用,因为我们实际上是在查看Woocommerce订阅管理页面,而不是实际的Woocommerce产品页面(可能有类别)。

1 个答案:

答案 0 :(得分:0)

试试这个

function eg_remove_my_subscriptions_button( $actions, $subscription ) {
    global $wp_query;
    $cat_id = $wp_query->get_queried_object()->term_id; // get current category id

    if($cat_id == 26) { // check if we're in the category
                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 );