1. Home
  2. Knowledge Base
  3. WooCommerce Fees and Discounts
  4. Display alternative price for specific role
  1. Home
  2. Knowledge Base
  3. Role based pricing
  4. Display alternative price for specific role

Display alternative price for specific role

If you’d like to display discounted prices for a specific role under the standard price for non-logged-in users, maybe as an incentive for users to sign up, then use the following snippet:

<?php
/**
* Display alternative price for specific role
*/
function my_prefix_filter_price_html( $price_html, $price, $product ) {
if( ! is_user_logged_in() ) {
$role = 'administrator';
if( $product->get_type() == 'variable' ) {
// Get the price for a specific user role
$variation_prices = wcfad_get_variation_prices_by_role( $product, $price, $role );
$min_variation_price = current( $variation_prices );
$max_variation_price = end( $variation_prices );
$best_price = $min_variation_price;
$min_max = array( $min_variation_price, $max_variation_price );
if( $min_max[0] != $min_max[1] ) {
// Display the formatted price range
$price = wc_format_price_range( $min_max[0], $min_max[1] );
} else {
$price .= wc_price( $best_price );
}
$price_html .= sprintf(
'<p>%s: %s</p>',
__( 'Admin Price' ), // Change this text as you wish
$price
);
} else {
// Get the price for a specific user role
$price = wcfad_get_product_price_by_role( $product, $price, $role );
$price = wc_price( $price );
// Add our extra information to the label
$price_html .= sprintf(
'<p>%s: %s</p>',
__( 'Admin Price' ), // Change this text as you wish
$price
);
}
}
return $price_html;
}
add_filter( 'wcfad_get_price_html', 'my_prefix_filter_price_html', 10, 3 );

Please see here for how to add this snippet.

Was this article helpful?

Related Articles