Variation price in dropdown for specific attributes
-
Hi there!
I typically have 3 attributes on my products. 2 of them affect the price and one does not. For the ones that add up to the product price I would like to show only for one of them the variation price in the dropdown selector because for the other one the price added is shown in attribute name.
I have tried this code (size affects the price and this is the attribute I want to show the price in variation dropdown):
function display_price_in_variation_option_name( $term ) { $product = wc_get_product(); $id = $product->get_id(); if ( empty( $term ) || empty( $id ) ) { return $term; } if ( $product->is_type( 'variable' ) ) { $product_variations = $product->get_available_variations(); } else { return $term; } foreach($product_variations as $variation){ /*if(count($variation['attributes']) > 1){ return $term; }*/ foreach($variation['attributes'] as $key => $slug){ if("attribute_" == mb_substr( $key, 0, 10 )){ $taxonomy = mb_substr( $key, 10 ) ; if( $taxonomy =="pa_size"){ $attribute = get_term_by('slug', $slug, $taxonomy); if($attribute->name == $term){ $term .= " (" . wp_kses( wc_price($variation['display_price']), array()) . ")"; } } } } } return $term; } add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
It should not work I suppose but it does. Nevertheless it makes the product page slow (this is fixed with caching) and the add to cart very slow also (which is not solved with caching).
Is there another way to show price in dropdown only for a selected attribute even if another attribute also impact the price?
Thank you for your help!
The page I need help with: [log in to see the link]
- The topic ‘Variation price in dropdown for specific attributes’ is closed to new replies.