【问题标题】:Display a custom price range on WooCommerce Product loops for variable products在 WooCommerce 产品循环中显示可变产品的自定义价格范围
【发布时间】:2020-11-13 09:08:19
【问题描述】:

我正在尝试为我的可变产品显示自定义价格范围。 我设法插入了一个包含常规(最低和最高)价格和销售(最低和最高)价格的价格范围。

这是我的代码尝试:

add_filter( 'woocommerce_get_price_html', 'custom_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'custom_price_format', 10, 2 );
function custom_price_format( $price, $product ) {

    // Main Price
    $regular_priceMin = $product->is_type('variable') ? $product->get_variation_regular_price( 'min', true ) : $product->get_regular_price();
    $regular_priceMax = $product->is_type('variable') ? $product->get_variation_regular_price( 'max', true ) : $product->get_regular_price();
    
    $sale_priceMin = $product->is_type('variable') ? $product->get_variation_sale_price( 'min', true ) : $product->get_sale_price();
    $sale_priceMax = $product->is_type('variable') ? $product->get_variation_sale_price( 'max', true ) : $product->get_sale_price();

    if ( $regular_priceMin !== $sale_priceMin && $product->is_on_sale()) {
        
        $price = '<p class="teste"><del>' . wc_price($regular_priceMin). 'a' . wc_price($regular_priceMax) . '</del></p> <ins>' . wc_price($sale_priceMin) . '</ins>';
    }
    return $price;
}

但是,一些销售价格具有相同的值并且格式不正确。
它创建了 3 行:

  • 一个代表最低价格,
  • 另一个字母“a”
  • 另一个是最高价格值。

我怎样才能正确地组织这个?

标签&lt;del&gt;标签不在同一行。

如何解决这个问题?我做错了什么?

【问题讨论】:

  • 尝试改写您的问题,因为它不清楚且无法理解。
  • 我更新问题谢谢!

标签: php wordpress woocommerce hook-woocommerce price


【解决方案1】:

尝试以下重新访问的代码,该代码将适用于产品循环中的任何地方,除了用于可变产品自定义价格范围的单个产品:

// For variable product
add_filter( 'woocommerce_variable_price_html', 'variable_prices_range_formatted', 100, 2 );
function variable_prices_range_formatted( $price, $product ){
    global $woocommerce_loop;
    // Not on single products
    if ( ( is_product() && isset($woocommerce_loop['name']) && ! empty($woocommerce_loop['name']) ) || ! is_product() )
    {
        // For on sale variable product
        if ( $product->is_on_sale()  )
        {
            $regular_price_min = $product->get_variation_regular_price( 'min', true );
            $regular_price_max = $product->get_variation_regular_price( 'max', true );

            $active_price_min = $product->get_variation_price( 'min', true );
            $active_price_max = $product->get_variation_price( 'max', true );

            if ( $regular_price_min !== $active_price_min || $regular_price_max !== $active_price_max )
            {
                // Regular price range (for <del> html tag)
                if( $regular_price_min !== $regular_price_max ) {
                    $regular_price_del_html = sprintf( '<del>%s a %s</del>', wc_price($regular_price_min), wc_price($regular_price_max) );
                } else {
                    $regular_price_del_html = sprintf( '<del>%s</del>', wc_price($regular_price_min) );
                }

                // Active price range (for <ins> html tag)
                if( $active_price_min !== $active_price_max ) {
                    $active_price_ins_html = sprintf( '<ins>%s a %s</ins>', wc_price($active_price_min), wc_price($active_price_max) );
                } else {
                    $active_price_ins_html = sprintf( '<ins>%s</ins>', wc_price($active_price_min) );
                }

                $price = sprintf( '<p class="teste">%s %s</p>', $regular_price_del_html, $active_price_ins_html );
            }
        }
    }
    return $price;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

销售产品价格的变量:

【讨论】:

  • 感谢您的帮助!我的代码和你的类似。也工作,但我有同样的问题。查看屏幕:i.imgur.com/XVKqnUL.png 未正确格式化,他使用 3 行。 habe
    用词。有可能解决这个问题吗?
  • @BarbaraEster 这是因为没有足够的空间(宽度),所以字符串在多行中中断......请参阅我的答案,this screen shot......我的代码很干净,没有格式错误,基于你问了什么。一些 CSS 可能会使它变得更好。
【解决方案2】:

您不会使用适用于所有类型产品的标准$product-&gt;get_price_html() 吗?

【讨论】:

  • 不!我只需要这个用于类型变量产品。其他类型没问题!我用于显示最高和最低价格以显示在页面商店和产品拇指框中的类别页面上。
猜你喜欢
  • 1970-01-01
  • 2017-11-24
  • 2019-02-16
  • 2019-05-09
  • 1970-01-01
  • 2019-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多