note: works with WooCommerce Wholesale Prices plugin!

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;

if( isset( $product ) ) {

$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );

$term_slug = ( !empty( $result ) ) ? $result[0] : $term;

$query = sprintf( "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta postmeta
LEFT JOIN {$wpdb->prefix}posts products ON ( products.id = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%%'
AND postmeta.meta_value = '%s'
AND products.post_parent = %d", $term_slug, $product->get_id() );

$variation_id = $wpdb->get_col( $query );
if(isset($variation_id[0])){
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
if( is_user_logged_in() ) {
$wholesale = 'wholesale_customer';
$user = wp_get_current_user();
if ( $wholesale == $user->roles[0] ) {
$varID = $variation_id[0];
$itemPrice = get_post_meta($varID, 'wholesale_customer_wholesale_price');

$beforeitemPrice = number_format($itemPrice[0],2);
if (substr($beforeitemPrice, -2) === '00') {
$beforeitemPrice = substr($beforeitemPrice, 0, -3);
}

return $term . ' ($'. $beforeitemPrice .')';
} else {
$_product = new WC_Product_Variation( $variation_id[0] );

$beforeitemPrice = number_format($_product->get_price(),2);
if (substr($beforeitemPrice, -2) === '00') {
$beforeitemPrice = substr($beforeitemPrice, 0, -3);
}
$itemPrice = strip_tags ( $beforeitemPrice );
return $term . ' ($'. $itemPrice .')';
}

} else {
$_product = new WC_Product_Variation( $variation_id[0] );

$beforeitemPrice = number_format($_product->get_price(),2);
$itemPrice = strip_tags ( $beforeitemPrice );
return $term . ' ($'. $itemPrice .')';
}

}
}
}
return $term;
}

ex. www.millsflorist.com

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.