为 Woocommerce 中大多数产品的显示价格添加后缀

问题描述 投票:0回答:2

我尝试将

is_product
if ( $product->get_id() !== xxx )
添加到函数中,但这会导致函数崩溃。

这是我试图添加到除一个产品之外的所有产品的功能。

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');

我是否需要先使用数组来获取产品 ID?

php wordpress woocommerce product product-price
2个回答
2
投票

您忘记了函数中的

$product
参数以及其他错误。尝试一下:

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

代码位于活动子主题(或活动主题)的 function.php 文件中。应该有效。


对于仅使用

is_product()
条件标签的单个产品页面:

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

代码位于活动子主题(或活动主题)的 function.php 文件中。应该有效。


0
投票

调用 $product 作为全局变量。仅此而已

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');
© www.soinside.com 2019 - 2024. All rights reserved.