有条件地改变Woocommerce中的多个细节产品价格

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

我正在使用此处的代码:Conditionally alter specific product price in Woocommerce将特定产品的价格提高10美元,但特定类别的产品页面除外,如果购物车包含该类别的任何内容。

但是现在我有多个产品需要我提高价格。我尝试改变第7行:

if( $product->get_id() != 87 ) return $price_html;

喜欢的东西

if( $product->get_id() != 87 || $product->get_id() != 2799 ) return $price_html;

目标产品87或2799,但它只是破坏了代码,甚至产品87不再显示为10美元以上。我试过||的变种或者,但我没有做任何事情。

非常感谢:)

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

对于多个产品ID,而不是使用以下内容:

add_filter( 'some_hook', 'some_function' );
function some_function( $price_html, $product ){
    if( $product->get_id() != 87 || $product->get_id() != 2799 ) return $price_html;

    // The function code 

    return $price_html; // at the end
}

您将使用以下内容:

add_filter( 'some_hook', 'some_function' );
function some_function( $price_html, $product ){
    if( in_array( $product->get_id(), array( 87, 2799 ) ) ){

        // The function code 

    }

    return $price_html; // at the end
}

它适用于多种产品


0
投票

你的if条件没有意义,因为它总会返回true。尝试替换或使用和:

if( $product->get_id() != 87 && $product->get_id() != 2799 ) return $price_html;
© www.soinside.com 2019 - 2024. All rights reserved.