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

Posted

技术标签:

【中文标题】如何将 wordpress/php 功能应用于特定的产品类别【英文标题】:How to apply wordpress/php function to a specific product category 【发布时间】:2016-07-30 02:55:44 【问题描述】:

回复。 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】:

试试这个

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

【讨论】:

你是 Zipkundan。不幸的是,这不起作用。如果我删除 if 语句,代码就可以工作,所以我认为类别 ID 可能没有正确返回给函数。我是否应该考虑定位特定的产品 ID 而不是类别 ID? (重复)我在原始描述中添加了更多信息。我担心因为显示按钮的页面与woocommerce订阅/订单相关,所以原始产品的类别将不可用。

以上是关于如何将 wordpress/php 功能应用于特定的产品类别的主要内容,如果未能解决你的问题,请参考以下文章

如何仅将特定的 CSS 规则应用于 Chrome?

SwiftUI:如何仅将修饰符应用于特定元素?

如何将 groupBy 和聚合函数应用于 PySpark DataFrame 中的特定窗口?

如何在将特定 css 属性应用于 dom 时自动应用类

如何仅将加载状态应用于 livewire 中的特定组件

如何通过 CSS 将阴影过滤器应用于 SVG 特定元素/路径