0

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

1
+50

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;
}
  • Thanks for your code, but I think your code only working for a single variation. I have updated code and added in below post – Abhinav bhardwaj Jul 31 at 18:41
0

This is solution for the minimal price as default for the variable products:

add_filter('woocommerce_variable_price_html','shop_variable_product_price', 10, 2 );

function shop_variable_product_price( $price, $product ){
    $variation_min_reg_price = $product->get_variation_regular_price('min', true);

    if(!empty($variation_min_reg_price)) {
        $price = woocommerce_price( $variation_min_reg_price );
    }
    else {
        $price = woocommerce_price( $product->regular_price );
    }

    return $price;
}
0

Show single variable price

add_filter( 'woocommerce_show_variation_price', '__return_true' );
0

upgrading @vivek's code as its code is only working for a single variation

    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 = '';
    $defaultArray = array();
    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>';
        $defaultArray['attribute_' . $key] = $val;
    }
    **$result = array_diff($defaultArray, $variation['attributes']);**
    **if (empty($result)) {
        $isDefVariation = true;
        $price = $variation['display_price'];
    }**
    }

    $selectedPrice = wc_price($price);
//  $dump = $dump . '<pre>' . var_export($available_variations, true) . '</pre>';
    return $selectedPrice . $dump;
}

This will work for multiple variation key

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.