dsf45346 2018-06-04 13:15
浏览 242
已采纳

显示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. Shope page

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

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

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

  • 写回答

1条回答 默认 最新

  • dongzecai0684 2018-06-04 15:10
    关注

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

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

    Related answer: Display the default variation price and savings amount on Woocommerce 3

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么