D
D
Dmitry2019-09-24 20:23:52
WordPress
Dmitry, 2019-09-24 20:23:52

How to hide WooCommerce discounts for website guests?

Hello. I can not find more than one normal plugin for the implementation of this undertaking. That is, I want the site guests to not have the opportunity to buy goods at a discount, but let's say the "wholesalers" had such an opportunity. Roles so to speak on the site.
None of the plugins that I used help me. And I also tried to manually register, but I did not find where the get_the_price () function is executed (the information on the network is outdated on this matter)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2019-09-24
@refreak

If anyone needs it here is the code

// Variable and simple product displayed prices (removing sale price range)
add_filter( 'woocommerce_get_price_html', 'custom_get_price_html', 20, 2 );
function custom_get_price_html( $price, $product ) {

    if( $product->is_type('variable') )
    {
        if( is_user_logged_in() ){
            $price_min  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_sale_price('min') ) );
            $price_max  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_sale_price('max') ) );
        } else {
            $price_min  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_regular_price('min') ) );
            $price_max  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_regular_price('max') ) );
        }

        if( $price_min != $price_max ){
            if( $price_min == 0 && $price_max > 0 )
                $price = wc_price( $price_max );
            elseif( $price_min > 0 && $price_max == 0 )
                $price = wc_price( $price_min );
            else
                $price = wc_format_price_range( $price_min, $price_max );
        } else {
            if( $price_min > 0 )
                $price = wc_price( $price_min);
        }
    }
    elseif( $product->is_type('simple') )
    {
        if( is_user_logged_in() )
            $active_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
        else
            $active_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );

        if( $active_price > 0 )
            $price = wc_price($active_price);
    }
    return $price;
}

// Product Variation displayed prices
add_filter( 'woocommerce_available_variation', 'custom_variation_price', 10, 3);
function custom_variation_price( $data, $product, $variation ) {

    $reg_price = wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) );
    $sale_price = wc_get_price_to_display( $variation, array( 'price' => $variation->get_sale_price() ) );

    if( is_user_logged_in() )
        $data['price_html'] = wc_price( $sale_price );
    else
        $data['price_html'] = wc_price( $reg_price );

    return $data;
}

// Set the correct prices in cart
add_action( 'woocommerce_before_calculate_totals', 'set_item_cart_prices', 20, 1 );
function set_item_cart_prices( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ){
        if( ! is_user_logged_in() ){
            $cart_item['data']->set_price( $cart_item['data']->get_regular_price() );
        }
    }
}

// Remove sale badge
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );

If you want to do it for certain roles, then replace if( is_user_logged_in() ) with
if( current_user_can('author') OR current_user_can('administrator') OR current_user_can('contributor') OR current_user_can('editor') OR current_user_can('shop_manager') OR current_user_can('translator') OR current_user_can('vip'))

A
Alexander, 2019-09-24
@NeiroNx

https://docs.woocommerce.com/document/woocommerce-...
4 google link for "woocommerce sale price for role"
Is this really such a difficult task?
Or does this plugin not implement the discount for roles as described in its documentation?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question