Any help would be greatly appreciated, been stuck with this for a couple of days now!
On WooCommerce I have a few products, each product has 5 or so variations and there is a price difference with each variation. What I'm trying to achieve is from the drop-down list of variations, is to show the actual price for that variation next to it.
I've done some searching and found this:
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
return $term . ' (' . woocommerce_price( $_product->get_price() ) . ')';
}
return $term;
}
This works great, but the problem I'm having is that it only works with variations where there are no spaces in the variation name: "green", "blue" and so on, but as soon as there's any spaces in the variation name, it stops working: "Green Shirt", "Black Speakers" and so on.
Any help would be greatly appreciated!