• Resolved Novakus

    (@novakus)


    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]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support B C. a11n

    (@battouly)

    Hi @novakus,

    While displaying variations as options with ‘+price’ that can be added to a product is not possible by default with WooCommerce Core, this is possible with the Product Add-Ons extension.

    The Product Add-Ons extension allows you to add paid or free options to your products using several field types including radio buttons, checkboxes, drop-down fields, custom text inputs, and more.

    However, with this topic involving custom snippet and fairly complex, I will leave it open just in case someone else in the community can help further with the request.

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    We’ve not seen any activity on this thread for a while, so I’m marking this thread as resolved.

    If you have further questions, please feel free to open a new topic.

    Cheers!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Variation price in dropdown for specific attributes’ is closed to new replies.