简体   繁体   中英

How can I show regular price for a specific product category in WooCommerce

How can I display the regular price and not discount price on my product listing for a specific category? It always shows me the discount price.

Here is my code:

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

I'm far from the solution?

The hook woocommerce_get_regular_price is obsolete and deprecated… Try the following instead:

// 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;
}

Code goes in functions.php file of your active child theme (or active theme). It should works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM