【问题标题】:How do I display price of selected bundle products instead of "From $"?如何显示所选捆绑产品的价格而不是“起价”?
【发布时间】:2022-07-07 18:13:25
【问题描述】:

我有一个由 5 个产品组成的产品包。所有这些产品都是可选的,但默认选中。因此,主产品显示为“From 900 kr”。而不是显示所选商品的价格。我正在使用 WooCommerces 自己的名为“WooCommerce 产品包”的插件。

picture of the product page

总价已显示在捆绑选项下方,但我希望将其显示为主要价格区域。

我已经在可变产品类型上取得了成功,并认为我可以使用相同的代码并进行一些更改。可惜我没能成功。

我用于变量产品的代码:

// Show selected variation price in main price area

add_action('woocommerce_before_add_to_cart_form', 'selected_variation_price_replace_variable_price_range');
function selected_variation_price_replace_variable_price_range(){
    global $product;

    if( $product->is_type('variable') ):
    ?><style> .woocommerce-variation-price {display:none;} </style>
    <script>
    jQuery(function($) {
        var p = 'p.price'
            q = $(p).html();

        $('form.cart').on('show_variation', function( event, data ) {
            if ( data.price_html ) {
                $(p).html(data.price_html);
            }
        }).on('hide_variation', function( event ) {
            $(p).html(q);
        });
    });
    </script>
    <?php
    endif;
}

有谁知道我如何做到这一点?

编辑:

我在产品页面上隐藏了主要价格,并将捆绑价格设置为看起来像主要价格。问题现在出在类别页面上,其中价格不显示捆绑包的默认选择变体的价格。它显示产品的最高可能价格,而不是默认捆绑选择价格。

这是来自捆绑插件的代码,它会导致最高价格显示:

/**
                 * 'woocommerce_bundle_force_old_style_price_html' filter.
                 *
                 * Used to suppress the range-style display of bundle price html strings.
                 *
                 * @param  boolean            $force_suppress_range_format
                 * @param  WC_Product_Bundle  $this
                 */
                if ( $suppress_range_price_html || apply_filters( 'woocommerce_bundle_force_old_style_price_html', false, $this ) ) {

                    $price = wc_price( $price_max );

                    $regular_price_min = $this->get_bundle_regular_price( 'max', true );

                    if ( $regular_price_min !== $price_max ) {

                        $regular_price = wc_price( $regular_price_max );

                        if ( $price_min !== $price_max ) {
                            $price = sprintf( _x( '%1$s%2$s', 'Price range: from', 'woocommerce-product-bundles' ), wc_get_price_html_from_text(), wc_format_sale_price( $regular_price, $price ) . $this->get_price_suffix() );
                        } else {
                            $price = wc_format_sale_price( $regular_price, $price ) . $this->get_price_suffix();
                        }

由于价格显示并有效地随着产品页面内的变化而变化,因此代码也必须可用于商店循环,我似乎找不到它。

【问题讨论】:

  • 我刚刚测试了这段代码,对我来说效果很好。可以分享一下网址吗?
  • @Bhautik 该代码适用于可变产品类型,但不适用于捆绑产品类型。这是呃网址:staging.coolenergi.dk/produkt/…

标签: php wordpress woocommerce


【解决方案1】:

现在您只检查产品类型variable。您还必须检查bundle

您只需使用or $product-&gt;is_type('bundle') 添加更多条件

检查下面的代码。

add_action('woocommerce_before_add_to_cart_form', 'selected_variation_price_replace_variable_price_range');
function selected_variation_price_replace_variable_price_range(){
    global $product;

    if( $product->is_type('variable') || $product->is_type('bundle') ):
    ?><style> .woocommerce-variation-price {display:none;} </style>
    <script>
    jQuery(function($) {
        var p = 'p.price'
            q = $(p).html();

        $('form.cart').on('show_variation', function( event, data ) {
            if ( data.price_html ) {
                $(p).html(data.price_html);
            }
        }).on('hide_variation', function( event ) {
            $(p).html(q);
        });
    });
    </script>
    <?php
    endif;

更新:试试下面的代码。代码将进入您的活动主题 function.php 文件。

function override_woocommerce_bundle_default_variation_price_html( $price, $product ) {

    if( $product->is_type('bundle') ){
        
        $bundled_items = $product->get_bundled_items();

        if ( ! empty( $bundled_items ) ) {

            foreach ( $bundled_items as $bundled_item ) {

                foreach( $bundled_item->product->get_available_variations() as $pav ){
                    $def = true;
                    foreach( $bundled_item->product->get_variation_default_attributes() as $defkey => $defval ){
                        if( $pav['attributes']['attribute_'.$defkey] != $defval){
                            $def = false;             
                        }   
                    }
                    if( $def ){
                        $price = $pav['display_price'];         
                    }
                }   
                
            }
            
        }

        return wc_price($price);
    }

    return $price;
}
add_filter( 'woocommerce_get_price_html', 'override_woocommerce_bundle_default_variation_price_html', 10, 2 );

【讨论】:

  • 感谢您的调查。可悲的是,我已经尝试过了,但它不起作用。价格会发生变化,但它不准确,就好像它只计算了其中一个变量一样。价格仅为 3.000、3.200 或 3.500,即使包含近 50.000 的产品。
  • 我已经检查了你的网站,看起来它正在工作。
  • 您如何检查代码是否有效?它对我不起作用...我通过隐藏主要价格对网站进行了其他调整,也许这就是您所指的并认为它有效?
  • 啊,是的,也许……
  • 产品页面上的当前修复很好,但在商店循环中,我让它显示了“最高”捆绑价格,但价格现在显示最高可能价格,而不是默认选择的价格可变价格。如您所见,商店循环中的产品显示为 51.498,但在产品页面上显示正确的价格:50.498...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-05
  • 1970-01-01
相关资源
最近更新 更多