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

一个人想着一个人 提交于 2019-12-22 09:46:16

问题


I'm looking for advice on what's wrong with the following function.

My goal in this example is to apply a 50% off discount to all WooCommerce simple products, as long as the user is logged in.

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

This function has no effect on the prices, am I approaching this all wrong?


回答1:


Here you don't need the init hook and your IF statement needs to be inside the hooked function, so try that instead (for simple products):

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


来源:https://stackoverflow.com/questions/52798268/woocommerce-global-percentage-discount-on-simple-products-if-customer-is-logged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!