2

I need to develop a script in function file that display sale price for specific categories for registered users. This code work fine.

I need to add sale price design to it

function thenga_customer_specific_pricing( $price, $product ) {    
    if ( ! is_user_logged_in() ) {
        return $price;
    }

    $id = $product->get_id();

    if( has_term( 'daniel-wellington', 'product_cat' ,$id ) ){
        // Give these customers a 20% discount.
        return $price * 0.8;
    } elseif( has_term( 'giardino-segreto', 'product_cat' ,$id ) ){
        return $price * 0.85;
    } else {
        return $price;
    }
}
add_filter( 'woocommerce_product_get_price', 'thenga_customer_specific_pricing', 10, 2 );

I expect an output like this:

Was: 100€ Now: 80€

I tried this filter :

function custom_dynamic_sale_price_html( $price_html, $product ) {
    if( $product->is_type('variable') ) return $price_html;

    $price_html = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display(  $product, array( 'price' => $product->get_sale_price() ) ) ) . $product->get_price_suffix();

    return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'custom_dynamic_sale_price_html', 20, 2 );

but It work in all products, how I can call this filter only in specific categories?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

2 Answers2

2

The following code will display your expected output (for simple products):

// Discount prices
add_filter( 'woocommerce_product_get_price', 'specific_discounted_product_prices', 10, 2 );
function specific_discounted_product_prices( $price, $product ) {
    // For logged in customers
    if ( is_user_logged_in() ) {
        if( has_term( 'daniel-wellington', 'product_cat' ,$product->get_id() ) ){
            $price *= 0.8; // 20% discount
        } elseif( has_term( 'giardino-segreto', 'product_cat' ,$product->get_id() ) ){
            $price *= 0.85; // 15% discount
        }
    }
    return $price;
}

// Display the discount
add_filter( 'woocommerce_get_price_html', 'specific_discounted_product_prices_display', 10, 2 );
function specific_discounted_product_prices_display( $price, $product ) {
    // For simple products and logged in customers
    if( $product->is_type('simple') && is_user_logged_in() ){
        $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
        $sale_price    = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
        $active_price  = wc_get_price_to_display( $product );

        if( $regular_price != $active_price ) {
            if( $product->is_on_sale() )
                $price = sprintf( 'Was: %s – Now: %s', wc_price($sale_price), wc_price($active_price) );
            else
                $price = sprintf( 'Was: %s – Now: %s', wc_price($regular_price), wc_price($active_price) );
        }
    }
    return $price;
}

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
0

In case you came here to make it work with variations, you need all the following filters:

add_filter( 'woocommerce_product_get_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_variation_prices_price', 'specific_discounted_product_prices', 10, 2 );
add_filter( 'woocommerce_variation_prices_sale_price', 'specific_discounted_product_prices', 10, 2 );

And you need to make sure you've changed the woocommerce_get_variation_prices_hash becasue of the stored transients.

You may skip the woocommerce_get_price_html if you use the woocommerce_product_get_sale_price filter as well.

You may find the gist I've created for a client useful

https://www.rent-a-ninja.org/programming/wordpress-plugin-entwickler-naehe-graz/woocommerce-custom-sale-price-for-custom-role/

Xandl
  • 122
  • 1
  • 7