【问题标题】:Exclude specific product ids from a category price suffix change in WooCommerce从 WooCommerce 中的类别价格后缀更改中排除特定产品 ID
【发布时间】:2020-09-11 10:45:49
【问题描述】:

根据Custom product price suffix for selected product categories in WooCommerce 答案代码,我使用以下方法将特定产品类别中的价格后缀更改为“每公斤”。但是,我需要排除此类别中的两种产品。谁能告诉我这是否可能。

add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
    // HERE define your product categories (can be IDs, slugs or names)
    $product_categories = array('cheese');

    if( has_term( $product_categories, 'product_cat', $product->get_id() ) )
$price .= ' ' . __('per kg');
    
return $price;
}

非常感谢

【问题讨论】:

    标签: php wordpress woocommerce prefix price


    【解决方案1】:

    使用以下(您将在其中定义要排除的产品ID)

    add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
    function conditional_price_suffix( $price, $product ) {
        // HERE define your product categories (can be IDs, slugs or names)
        $product_categories = array('cheese');
    
        // HERE define your products Ids to be excluded
        $excluded_ids = array(37, 57);
    
        if( has_term( $product_categories, 'product_cat', $product->get_id() ) 
        && ! in_array( $product->get_id(), $excluded_ids ) ) {
            $price .= ' ' . __('per kg');
        }
        
        return $price;
    }
    

    代码进入您的活动子主题(活动主题)的 function.php 文件中。它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2021-07-22
      • 2021-05-19
      • 2019-04-25
      • 1970-01-01
      • 2018-11-09
      • 2013-08-11
      • 2017-12-10
      • 2018-01-31
      • 1970-01-01
      相关资源
      最近更新 更多