如果客户已登录,Woocommerce 对简单产品的全球百分比折扣

Woocommerce global percentage discount on simple products if customer is logged in

我正在就以下函数的问题寻求建议。

我在此示例中的目标是对所有 WooCommerce 简单产品应用 50% 的折扣,只要用户已登录。

function tier_pricing_logic() {  

    if ( is_user_logged_in() ) {  

        function assign_tier_pricing( $price, $product ) {
            $price = $price * 0.5; // Set all prices for simple products to 50% off.    
        }   
        return $price; 

        add_filter('woocommerce_product_get_price', 'assign_tier_pricing', 90, 2 );
        add_filter('woocommerce_product_get_regular_price', 'assign_tier_pricing', 90, 2 );     
    }

}                  
add_action( 'init', 'tier_pricing_logic' );

这个功能对价格没有影响,我是不是完全错了?

这里你不需要 init 钩子,你的 IF 语句需要在钩子函数内部,所以试试这个(对于简单的产品):

add_filter('woocommerce_product_get_price', 'assign_tier_pricing', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'assign_tier_pricing', 90, 2 );
function assign_tier_pricing( $price, $product ) {
    if ( is_user_logged_in() && $product->is_type('simple') ) { 
        $price *= 0.5; // Set all prices for simple products to 50% off.    
    }   
    return $price;   
}