Is there any way to force to show the price of the Product Variation I set as defaul in product page settings, instead of lower and higher prices?

I’ve got this code to show just one price, but the shown price is not the price of the default variation:

/*******************************
    SHOW ONLY ONE PRICE FOR VARIATIONS
*********************************/

add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
     $price="";
     $price .= woocommerce_price($product->get_price());
     return $price;
}

enter image description here

5 Answers
5

Try this code:

add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
    $available_variations = $product->get_available_variations();
    $selectedPrice="";
    $dump = '';

    foreach ( $available_variations as $variation )
    {
        // $dump = $dump . '<pre>' . var_export($variation['attributes'], true) . '</pre>';

        $isDefVariation=false;
        foreach($product->get_default_attributes() as $key=>$val){
            // $dump = $dump . '<pre>' . var_export($key, true) . '</pre>';
            // $dump = $dump . '<pre>' . var_export($val, true) . '</pre>';
            if($variation['attributes']['attribute_'.$key]==$val){
                $isDefVariation=true;
            }   
        }
        if($isDefVariation){
            $price = $variation['display_price'];         
        }
    }
    $selectedPrice = wc_price($price);

//  $dump = $dump . '<pre>' . var_export($available_variations, true) . '</pre>';

    return $selectedPrice . $dump;
}

Leave a Reply

Your email address will not be published. Required fields are marked *