Woocommerce shows price on both variations


I'am building a shop for a client. Now on the product page, there is a dropdown where you can choose Delivery "standard" or "express.

When this is chosen by the customer you can choose the amount you want from this product.

now i found a piece of code on stackoverflow ( <a href="https://stackoverflow.com/questions/12272733/woocommerce-get-variation-product-price" rel="nofollow">Woocommerce get variation product price</a> ) to display the correct price directly after the amount in the dropdown. this works perfect.

But now , the price of the first amount variation (100 (€22) ) is also displayed on the delivery dropdown ( 2 - 3 days (€22) . And i would like to remove the price from the delivery variable / dropdown.

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; }

I tried to change the $variation_id[0]; to 1 , 2 , 3 ,4 and 5 , but no succes, so i assume there must be another way to fix it.

Thnx in advance :)


What you need to do is to put some logic in your display_price_in_variation_option_name() function to check whether the filter should apply to a particular attribute or not. One big problem in this case is that the only thing you have access to in your function is the name of the term, which is not guaranteed to be unique. For example, you could have a variation called "Blue" in both a "Material Color" or "Packaging Color" attribute. We need to find out exactly which attribute this term belongs to, and the only reasonable way to do that, as far as I can tell, is to modify the templates that apply this filter, and pass in an extra "taxonomy" parameter that you can check within the function.

For example, on line 48 of the template


you will find two instances of the 'woocommerce_variation_option_name' filter, one on line 48 and one on line 54 (as of version 2.3.0). You will see that these currently pass in a single parameter. We need to pass in a second parameter, which is the name of the attribute the term belongs to. Luckily, that variable already exists in scope, and it's called $name. So, we need to make two modifications to the template. First, we should add the second $name variable, as such:

apply_filters( 'woocommerce_variation_option_name', $term->name )

Should become

apply_filters( 'woocommerce_variation_option_name', $term->name, $name )

You will also need to update your filter to accept a second argument. So, in your original code, this:

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' )

Should become:

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name', 10, 2 )

And you will also need to update your filter function to accept a second argument. So this:

function display_price_in_variation_option_name( $term ) {

Should become:

function display_price_in_variation_option_name( $term, $name = null ) {

Now you are ready to add the logical check to see if the attribute is one that should display the price. Since you only indicated you want to hide it for the shipping attribute, that's what we'll use. First you will need to find the full name of the attribute you're looking to suppress. The quickest way to do this is to go into the WP Admin and go to the Products -> Attributes section and select the shipping attribute that you want to suppress. Then in the URL you will see ?taxonomy= followed by something beginning with "pa_" - you want the thing that starts with "pa_" (it stands for product attribute). For this example, let's say that it's "pa_shipping". Now, in your filter function, you'll do the following:

function display_price_in_variation_option_name( $term, $name = null ) { if($name == 'pa_shipping') { return $term; } global $wpdb, $product; // the rest of your original code

I've tested this out and it seems to work pretty well. There is also a JavaScript option that I could describe, but that would not be as robust (though it would not require you to alter template files).


  • Make a “exclusive” Post using a custom field
  • Wordpress get posts between date range using a date meta field
  • How to display sub child pages only when on direct parent page?
  • How to query SQL for coords nearest a point?
  • Wordpress - Pagination for pages
  • Return multiple values in an array from an array of objects
  • Wordpress - ACF - get post multiple times with repeater date field
  • order posts by custom field
  • Query rows for which meta data key does not exist
  • Wordpress meta box values display to custom fields
  • Visual Form Builder Pro (VFB Pro): Which table are the form entries stored in?
  • GDataXML nodesForXPath on Node contains items of entire GDataXMLDocument
  • post__not_in is not excluding IDs from Wordpress Search Query
  • Wordpress - get 5 popular posts by views without plugin
  • WordPress child nav a:current styling
  • Eloquent Left Join, getting unexpected result
  • MySql update fields based on other fields
  • Woocommerce - List product by using specific attributes
  • SQL query to include specific WooCommerce tag
  • Woocommerce meta_query not working for featured products
  • Dispose not working as expected in DirectorySearcher
  • Fluid video height
  • Weird LEFT OUTER JOIN on Includes eager loading of rails 3
  • Shopify API CARTS - Changing line_item line_price for price Override
  • How can I make a 100% stacked chart with nvd3?
  • Returning the auto incrementing value after an insert using slick
  • Sending HTML Form Data to Spring REST Web Service
  • Webgrid not refreshing after delete MVC
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Disable Enter in editText android
  • Jetty Server not starting: Unable to establish loopback connection
  • Django: Count of Group Elements
  • How to check if every primary key value is being referenced as foreign key in another table
  • How to handle AllServersUnavailable Exception
  • How to get next/previous record number?
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • python regex in pyparsing
  • How to stop GridView from loading again when I press back button?