在产品页面上设置相关产品的折扣价

时间:2020-05-31 07:32:28

标签: woocommerce

我正在尝试将“产品”页面上所有相关产品的价格降低10%= 0.9

目标是在产品页面上提供所有相关产品的折扣,但当被视为产品时,给出正常价格。

总的来说,这个想法是为了提供激励,以促进相关产品的向上销售。

我在这里要求两件事。 一个:在产品页面上更改相关产品的产品价格(10%折扣),两个:将该折扣价格携带到购物车中,并在相关折扣产品被购买时结帐已从产品页面添加到购物车。

我几乎把第一部分都讲完了,但是我试图工作的代码却给我一个错误,说:

Warning: A non-numeric value encountered

到目前为止,我的代码:

add_filter( 'woocommerce_get_price_html', 'related_product_price_discount', 100, 2 );
function related_product_price_discount( $price, $product ) {
    global $woocommerce_loop;

    // make sure this is a related product on product page
    if( is_product() && $woocommerce_loop['name'] == 'related' ){
        $price = $price * 0.9;
    }
    // return related product price with discount
    return apply_filters( 'woocommerce_get_price', $price );
}

1 个答案:

答案 0 :(得分:0)

StackOverFlow中的规则当时是一个问题,所以我只会回答您的第一个问题,这与您的代码问题...

请注意,挂钩woocommerce_get_price_html与显示的格式化价格有关。

为避免错误 “警告:遇到非数值” ,您将使用以下内容:

add_filter( 'woocommerce_get_price_html', 'related_product_price_discount', 100, 2 );
function related_product_price_discount( $price_html, $product ) {
    global $woocommerce_loop;

    // make sure this is a related product on product page
    if( is_product() && $woocommerce_loop['name'] == 'related' ){
        $price_html = wc_price( wc_get_price_to_display( $product ) * 0.9 );
    }
    // return related product displayed discounted formatted price
    return $price_html;
}

代码进入您的活动子主题(活动主题)的functions.php文件中。经过测试,可以正常工作。