Search code examples
phpwordpresswoocommerceproduct

Discount for a specific product and a specific customer in WooCommerce


I am trying to change the price for one logged in customer for one product. When using this code, the price does change, but only on the product page. I need the price to change everywhere (archive, product page, cart etc.)

Here's the code that I am trying to make work.

add_filter( 'woocommerce_product_get_price', 'special_discount_for_product_cat_for_logged_in_customers', 10, 2 );
function special_discount_for_product_cat_for_logged_in_customers( $price, $product ) {

    $current_user_id = get_current_user_id();
    if( $current_user_id == 433 && is_user_logged_in() ) {
        if (is_product( '11323', $product->get_id())) {
            $price = 9.95;
        }
    }
    return $price;
}

Any help is appreciated.


Solution

  • Try the following instead, that will change everywhere the product price for a specific product and a specific user Id:

    add_filter( 'woocommerce_product_get_price', 'special_discount_for_product_cat_for_logged_in_customers', 10, 2 );
    function special_discount_for_product_cat_for_logged_in_customers( $price, $product ) {
        // YOUR SETTINGS
        $targeted_user_id    = 433;
        $targeted_product_id = 11323;
    
        if( get_current_user_id() == $targeted_user_id && $product->get_id() == $targeted_product_id ) {
            $price = 9.95;
        }
        return $price;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and work.