首页 > 解决方案 > 在 WooCommerce 产品循环上显示可变产品的自定义价格范围

问题描述

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

这是我的代码尝试:

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 行:

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

tag<del>标签不在同一行。

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

标签: phpwordpresswoocommercehook-woocommerceprice

解决方案


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


推荐阅读