WooCommerce:显示默认差异价格是产品列表吗?

时间:2017-10-30 作者:JPashs

有没有办法强制显示我在产品页面设置中设置为默认的产品变体的价格,而不是越来越低的价格?

我的代码只显示一个价格,但显示的价格不是默认变体的价格:

/*******************************
    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 个回复
最合适的回答,由SO网友:Vivek Athalye 整理而成

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;
}
SO网友:Alexander Z.

这是针对可变产品的默认最低价格的解决方案:

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

SO网友:SHEESHRAM

显示单一可变价格

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

SO网友:Abhinav bhardwaj

升级@vivek的代码,因为其代码只适用于单个变体

    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;
}
这将适用于多变量键

SO网友:Stijn Sillen

升级@Abhinav的代码以在没有默认设置的情况下显示最低价格:

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

结束

相关推荐