简体   繁体   中英

Add a suffix to the displayed price of mostly all products in Woocommerce

I tried adding is_product and if ( $product->get_id() !== xxx ) to a function but that crashes the function.

Here's the function I am trying to add to all products except one.

function change_product_price_html($price){
    if ( $product->get_id() !== 555 ) {
    $newPrice   = $price;
    $newPrice   .= " / m3";
    return $newPrice;
    }
}

add_filter('woocommerce_get_price_html', 'change_product_price_html');

Do I maybe need an array first to get the product ID?

You have forgotten the $product argument in the function and other mistakes. try instead:

add_filter('woocommerce_get_price_html', 'change_product_price_html', 10, 2 );
function change_product_price_html( $price, $product ){
    if ( $product->get_id() !== 555 ) {
        $price .= __(" / m3");
    }
    return $price;
}

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


For single product pages only using is_product() conditional tag:

add_filter('woocommerce_get_price_html', 'change_product_price_html', 10, 2 );
function change_product_price_html( $price, $product ){
    if ( is_product() && $product->get_id() !== 555 ) {
        $price .= __(" / m3");
    }
    return $price;
}

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

call $product as global variable. Thats all

function change_product_price_html($price){
global $product
if ( $product->ID !== 555 ) {
$newPrice   = $price;
$newPrice   .= " / m3";
return $newPrice;
}
}

add_filter('woocommerce_get_price_html', 'change_product_price_html');

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