Show WooCommerce product variation price in the variation dropdown

If you would like to show the price for each variation in the variation dropdown, then you can use the code below. The output would look like this:

function wc_display_price_in_variation_option_name( $term ) {
    global $product;
 
    if ( empty( $term ) ) {
        return $term;
    }
    if (isset ($product) && $product->get_id()) {
 
    $variation_id = $product->get_children();
 
    foreach ( $variation_id as $id ) {
        $_product       = new WC_Product_Variation( $id );
        $variation_data = $_product->get_variation_attributes();
 
        foreach ( $variation_data as $key => $data ) {
            if ( $data == $term ) {
                $html = $term;
                $html .= ( $_product->get_stock_quantity() ) ? ' - ' . $_product->get_stock_quantity() : '';
                $html .= ' - ' . wp_kses( wc_price( $_product->get_price() ), array() );
                return $html;
            }
        }
    }
}
 
    return $term;
}
add_filter( 'woocommerce_variation_option_name','wc_display_price_in_variation_option_name', 10 , 1 );

This code can be added following the instructions here:

How to add custom code to your WooCommerce/WordPress site the right way

Have any feedback? Be sure to let me know here: Contact me

One thought on “Show WooCommerce product variation price in the variation dropdown

Share your thoughts

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