Woocommerce Show default variation price Woocommerce Show default variation price wordpress wordpress

Woocommerce Show default variation price


I did not find answer for this question (how to display price from default product variation instead of price range?) so i created the code:

add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);    function custom_variation_price( $price, $product ) {        foreach($product->get_available_variations() as $pav){            $def=true;            foreach($product->get_variation_default_attributes() as $defkey=>$defval){                if($pav['attributes']['attribute_'.$defkey]!=$defval){                    $def=false;                             }               }            if($def){                $price = $pav['display_price'];                     }        }           return woocommerce_price($price);    }

This plugin show price from default variation, of course if you set default values before. Tested on woocommerce version 2.6.2.


This is my own solution, think it could be better, but I'm not a php developer, so feel free to add improvements.

function wc_wc20_variation_price_format( $price, $product ) {// Main Price$available_variations = $product->get_available_variations();$selectedPrice = '';foreach ( $available_variations as $variation ){    $tmp = implode($variation['attributes']);    if (strpos($tmp,'standard') !== false) {        $selectedproduct = wc_get_product($variation['variation_id']);        $price =  $selectedproduct->get_price();        $selectedPrice = wc_price($price);    }       }return $selectedPrice;}