php - 在 Woocommerce 商店页面中显示可变产品的默认变化价格

标签 php wordpress woocommerce price product-variations

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

但是这些都是使用默认变量参数设置的产品: Default Parameters

当您点击产品时,它会显示如下: Single Product

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

最佳答案

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

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 文件中。经过测试并有效。

相关答案:Display the default variation price and savings amount on Woocommerce 3

关于php - 在 Woocommerce 商店页面中显示可变产品的默认变化价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50681539/

相关文章:

php/mysql/ajax : google style search with suggestions

php - 使用电子邮件搜索 linkedin 个人资料

wordpress - 如何在 WordPress 中为 fatal error 处理程序 (WSoD) 设置通知电子邮件

javascript - 在颜色和尺寸之前更改“添加到购物车”按钮的文本

php - 在 WooCommerce 订单状态更改时添加用户元数据作为订单元

javascript - 在 Codeigniter 中自动刷新 div

css - 将侧边栏添加到页面

wordpress - 在不使用 .htaccess 的情况下重定向 Wordpress 网站上的非 www

wordpress - Woocommerce 2.6 API v1 -> 参数无效

php - mysql查询获取2个日期之间的第一个特定日期