在 Woocommerce 商店页面中显示可变产品的默认变化价格 [英] Display default variation price for variable products in Woocommerce shop pages

查看:31
本文介绍了在 Woocommerce 商店页面中显示可变产品的默认变化价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在商店页面上显示产品价格.现在我的可变产品显示了它们的价格范围.

I'm wondering how I can display the product price on the shop page. Right now my variable products are shown with their price range.

但这两款产品都设置了默认可变参数:

But these are both products that have been setup with default variable parameters:

当你点击一个产品时,它会显示如下:

When you click on a product, it's being shown as followed:

如您所见,该特定商品的价格为 €300,我想知道如何在商店页面上显示 €300 而不是 €150-€2.003

As you can see the price of that specific selection is €300, I'm wondering how I can display that €300 on the shop page instead of €150-€2.003

推荐答案

可以获取在可变产品中设置的默认变化价格并将其显示在商店和档案页面中:

It's possible to get the default variation price that is set in a variable product and to display it in shop and archives pages:

add_filter( 'woocommerce_variable_price_html', 'custom_variable_displayed_price', 10, 2 );
function custom_variable_displayed_price( $price_html, $product ) {
    // Only for archives pages
    if ( ! ( is_shop() || is_product_category() || is_product_tag() ) )
        return $price_html;

    // Searching for the default variation
    $default_attributes = $product->get_default_attributes();
    // Loop through available variations
    foreach($product->get_available_variations() as $variation){
        $found = true; // Initializing
        // Loop through variation attributes
        foreach( $variation['attributes'] as $key => $value ){
            $taxonomy = str_replace( 'attribute_', '', $key );
            // Searching for a matching variation as default
            if( isset($default_attributes[$taxonomy]) && $default_attributes[$taxonomy] != $value ){
                $found = false;
                break;
            }
        }
        // When it's found we set it and we stop the main loop
        if( $found ) {
            $default_variaton = $variation;
            break;
        } // If not we continue
        else {
            continue;
        }
    }

    // If no default variation is found we exit.
    if( ! isset($default_variaton) )
        $price_html;

    // Formatting the price
    if ( $default_variaton['display_price'] !== $default_variaton['display_regular_price'] && $product->is_on_sale()) {
        $price_html = '<del>' . wc_price($default_variaton['display_regular_price']) . '</del> <ins>' . wc_price($default_variaton['display_price']) . '</ins>';
    } else {
        $price_html = wc_price($default_variaton['display_price']);
    }
    return $price_html;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

相关答案:在 Woocommerce 3 上显示默认的变化价格和节省金额

这篇关于在 Woocommerce 商店页面中显示可变产品的默认变化价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆