S
S
Sell_Your_Soul2021-01-21 15:41:47
WordPress
Sell_Your_Soul, 2021-01-21 15:41:47

How to display label instead of Woocommerce price?

You need to display an inscription instead of a price for certain products via PHP.
The problem is that I don’t know how to set a condition so that unauthorized users and authorized ordinary buyers show an inscription instead of the price of certain goods, and wholesale buyers (a separate role) show their price (it is set through a separate Wholesale Prices plugin).
Also interested in whether it is possible to do the same for a variable product, namely for one of the variations?
Please help, I tried a lot of things, but I did not find the right solution.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sell_Your_Soul, 2021-01-22
@Sell_Your_Soul

The issue is resolved, here is the code that needs to be inserted into functions.php (or better, create a snippet through the plugin and leave functions.php alone).
Code found on CodeRoad by LoicTheAztec. I slightly modified it to suit my needs.
Unregistered users and users with the "Customer" role will see an inscription instead of a price for products with weight = 1.

//Текст, который будет вместо цены
function product_price_replacement(){
    return 'Для профессионального использования';
}

// Замена цены на текст
add_filter( 'woocommerce_get_price_html', 'filter_get_price_html_callback', 10, 2 );
function filter_get_price_html_callback( $price, $product ){
    if(( $product->get_weight()== 1 ) and ((!is_user_logged_in() or current_user_can('customer')) )) {
        $price = product_price_replacement();
    }
    return $price;

}

// Отключение возможности добавить в корзину
add_filter( 'woocommerce_variation_is_purchasable', 'woocommerce_is_purchasable_filter_callback', 10, 2 );
add_filter('woocommerce_is_purchasable', 'woocommerce_is_purchasable_filter_callback', 10, 2 );
function woocommerce_is_purchasable_filter_callback( $purchasable, $product ) {
    $product_id = $product->get_parent_id() > 0 ? $product->get_parent_id() : $product->get_id();
    if( (( $product->get_weight()== 1 ) and ((!is_user_logged_in() or current_user_can('customer')) ))) {
        $purchasable = false;
    }
    return $purchasable;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question