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 ( Woocommerce get variation product price ) 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
/wp-content/plugins/woocommerce/templates/single-product/add-to-cart/variable.php
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).