admin管理员组

文章数量:1295694

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;
}

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;
}

Share Improve this question asked Oct 30, 2017 at 9:41 JPashsJPashs 1773 gold badges3 silver badges12 bronze badges 2
  • Maybe duplicate with stackoverflow/questions/43279746/… – Drupalizeme Commented Nov 2, 2017 at 9:40
  • It's not, because in that function instead of the default variation, the lowest/discount price is shown. Although it is a duplicate of this: stackoverflow/questions/36453795/… I've tested this, and it works. I suspect that the op used this as a starting point (same function name and variables passed), but I'm not sure why it wasn't sufficient. – galingong Commented Nov 2, 2017 at 11:55
Add a comment  | 

5 Answers 5

Reset to default 6 +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;
}

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;
}

Upgrading @ Abhinav's code to show minimum price in case there is no default set:

/**
 * Show default variation price
 * If no default variation, shows min price
 */

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 ($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;
    }
    // $dump = $dump . '<pre>' . var_export($defaultArray, true) . '</pre>';
    if (empty($defaultArray)) {
        $price = $product->get_variation_price( 'min', true ); // no default variation, show min price
    } else {
        foreach ($available_variations as $variation) {
            // $dump = $dump . '<pre>' . var_export($variation['attributes'], true) . '</pre>';
            $isDefVariation = false;
    
            $result = array_diff($defaultArray, $variation['attributes']);
            if (empty($result)) {
                $isDefVariation = true;
                $price = $variation['display_price'];
                break;
            }
        }
    }

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

Show single variable price

add_filter( 'woocommerce_show_variation_price', '__return_true' );

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

本文标签: Woocommerce show default variation price is products list