首页 > 解决方案 > 如何在 WooCommerce 中显示特定产品类别的正常价格

问题描述

如何在特定类别的产品列表中显示正常价格而不是折扣价格?它总是向我显示折扣价。

这是我的代码:

function filter_woocommerce_get_regular_price() {
    global $product;
    if (is_product_category ('book-fair')) {
        return $product->get_regular_price();
    }
    return $product->get_sale_price();
}

我离解决方案还远吗?

标签: phpwordpresswoocommercepricetaxonomy-terms

解决方案


该钩子woocommerce_get_regular_price已过时且已弃用……请尝试以下操作:

// Custom regular price for specific product categories
add_filter('woocommerce_product_get_price', 'filter_woocommerce_product_get_price', 10, 2); 
function filter_woocommerce_product_get_price( $price, $product ) {

    if ( has_term( array('book-fair'), 'product_cat', $product->get_id() ) && $product->is_on_sale() ) {
        return $product->get_regular_price();
    }
    return $price;
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。它应该有效。


推荐阅读