首页 > 解决方案 > 将可变产品价格范围替换为“来自:”+ WooCommerce 中使用 number_format 的最低价格

问题描述

我有 2 个代码片段,其中 ;

代码 A 具有以下功能:

此代码工作正常


代码 B 具有以下功能:

这段代码工作正常。


问题是当两个代码同时激活时(我正在使用代码片段),代码 A 不能正常工作。
之前显示“起价+最低价”的商店页面现在只显示最低价(使用代码 B 中的价格格式)。

请看下面的前后显示变化:

在使用代码 A 和代码 B 之前 >请参阅

使用代码 A >请参阅

添加代码 B >请参阅

我要 >


如何让上面的两个代码在同时激活时正常工作?

任何帮助是极大的赞赏。


代码 A:
//Hide Price Range for WooCommerce Variable Products
add_filter( 'woocommerce_variable_sale_price_html','lw_variable_product_price', 5, 2 );
add_filter( 'woocommerce_variable_price_html','lw_variable_product_price', 5, 2 );


function lw_variable_product_price( $price, $product ) {
if(is_product()) {
return $price;
}

// Product Price
$prices = array( $product->get_variation_price( 'min', true ),
$product->get_variation_price( 'max', true ) );
$price = $prices[0]!==$prices[1] ? sprintf(__('Harga terendah : %1$s', 'woocommerce'),wc_price( $prices[0] ) ) : wc_price( $prices[0] );

 // Regular Price
$regular_prices = array( $product->get_variation_regular_price( 'min', true ),
$product->get_variation_regular_price( 'max', true ) );
sort( $regular_prices );
$regular_price = $regular_prices[0]!==$regular_prices[1] ? sprintf(__('Harga terendah : %1$s','woocommerce'), wc_price( $regular_prices[0] ) ) : wc_price( $regular_prices[0] );

if ( $price !== $regular_price ) {
$price = '<del>'.$regular_price.$product->get_price_html() . '</del> <ins>' .
$price . $product->get_price_html() . '</ins>';
}
return $price;
}

 //Hide “From:$X”
add_filter('woocommerce_get_price_html', 'lw_hide_variation_price', 5, 2);
function lw_hide_variation_price( $price, $product ) {

$product_types = array( 'variable');
if ( in_array ( $product->product_type, $product_types ) && !(is_shop()) ) {
return '';
}
// return regular price
return $price;
}

代码 B:

add_filter( 'woocommerce_get_price_html','rei_woocommerce_price_html',5, 2 );

function rei_woocommerce_price_html( $price, $product ) {

    
$currency = get_woocommerce_currency_symbol( );
$price = $currency .' ' .custom_number_format( floatval( $product->get_price() ), 2 );
return $price;
}

     $n = 1000000;

    function custom_number_format12222($n) {

        $n = (0+str_replace("."," ",$n));


        if(!is_numeric($n)) return false;


        if($n>1000000000000) return round(($n/1000000000000),1).' '.'-T';
        else if($n>1000000000) return round(($n/1000000000),2).' '.'M';
        else if($n>1000000) return round(($n/1000000),3).' '.'juta';
        else if($n>1000) return round(($n/1000),0).' '.'ribu';

        return number_format($n);
        
    }

标签: wordpressvariableswoocommerceprice

解决方案


推荐阅读