简体   繁体   中英

Exclude specific product ids from a category price suffix change in WooCommerce

Based on Custom product price suffix for selected product categories in WooCommerce answer code, I have used the following to change the price suffix in a specific product category to 'per kg'. However, there are two products in this category I need to exclude from this. Could anyone tell me whether this is possible please.

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

Many thanks

Use the following (where you will define the product ids to be excluded) :

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

Code goes in function.php file of your active child theme (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