Answer the question
In order to leave comments, you need to log in
How to display WooCommece product variation only for a specific user role?
Good day!
It is necessary that one of the product variations is displayed only for wholesale buyers with their price, and for all other buyers either this variation is not there or instead of the price there is an inscription "Only for professional use" without the ability to add to the cart. I tried a bunch of different plugins that hide the empty price from different buyer roles, but none of these plugins worked as they should.
To customize variations, I use the Product Variations Swatches plugin.
Answer the question
In order to leave comments, you need to log in
Actually, I solved the issue myself, here is the code that needs to be inserted into functions.php (it’s better to 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.
//Текст, который будет вместо цены
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 questionAsk a Question
731 491 924 answers to any question