Add prices next to WooCommerce variations

Back to Blog

Add prices next to WooCommerce variations

I will show you how you can add prices next to WooCommerce variations in your online store just by adding a short snippet to your themes functions.php file.

// Add the prices next to the variations in WooCommerce
function aws_multi_display_price_in_variation_option_name( $term_name ) {

    global $wpdb, $product;

    if ( is_product() && $product instanceof WC_Product && $product->is_type( 'variable' ) ) {

            foreach ( $product->get_available_variations() as $variation ) {
                foreach ( $variation['attributes'] as $variation_attribute_name => $variation_attribute_value ) {
                    $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE slug = '$variation_attribute_value'" );
                    $taxonomy = str_replace( 'attribute_', '', $variation_attribute_name );
                    $term     = get_term_by( 'slug', $variation_attribute_value, $taxonomy );
                    $product_price = wc_price( $variation['display_price'] );
                    $product_price = strip_tags( $product_price );
                    if ( $term_name == $variation_attribute_value ) {
                    return ''.$term_name . ' - ' . $product_price . '';
                    } else if( is_object($term) && $term->name == $term_name ) {
                    return ''.$term_name . ' - ' . $product_price . '';
                    }
                }

            }

        }
	return $term_name;
}
add_filter( 'woocommerce_variation_option_name', 'aws_multi_display_price_in_variation_option_name', 10, 1 );

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Blog