在WooCommerce产品循环上显示可变产品的自定义价格范围

时间:2020-07-23 15:21:21

标签: php wordpress woocommerce hook-woocommerce price

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

这是我的代码尝试:

sum['y'] = 2

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

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

我该如何正确组织?

标签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; } 不在同一行。

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

2 个答案:

答案 0 :(得分:0)

您是否不使用所有类型产品都可用的标准$product->get_price_html()

答案 1 :(得分:0)

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

// 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文件。经过测试,可以正常工作。

销售产品价格变量:

enter image description here