Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Last active March 9, 2020 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plugin-republic/073557b7a67699d739098a82e6d3809c to your computer and use it in GitHub Desktop.
Save plugin-republic/073557b7a67699d739098a82e6d3809c to your computer and use it in GitHub Desktop.
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment