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

问题描述 投票:0回答:2

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

这是我的代码尝试:

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”
  • 另一个代表最高价格。

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

标签

<del>
标签不在同一行。

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

php wordpress woocommerce hook-woocommerce
2个回答
0
投票

您不会使用适用于所有类型产品的标准

$product->get_price_html()
吗?


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

© www.soinside.com 2019 - 2024. All rights reserved.