WooCommerce 测量价格计算 - 用最低价格替换价格范围

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

希望标题是不言自明的!我在这里使用 WooCommerce 中的测量价格计算器 (MPC)。我在我的functions.php中添加了调整,以更改可变产品上的价格显示,以便隐藏从 - 到价格范围,只显示“从{最低价格}”。我相信这是用户在 WooCommerce 中进行的相当常见的调整,但为了更好的衡量,我将包含下面使用的代码,因为我希望它也适用于 MPC。

我的问题是,当使用 MPC 的定价表功能时,它会返回显示表的价格范围,例如“0.86 欧元 - 0.98 欧元/平方米”。我的调整没有捕捉到这一点,我已经联系了开发人员 Skyverge,他们无法给我解决方案,但确实向我指出了 “wc_measurement_price_calculator_get_price_html” 过滤器。

这里有没有人在使用 MPC 时成功做到这一点或类似的,或者有人可以建议使用 Skyverge 建议的过滤器吗?

我用于可变产品的代码如下:

// Hide Price Range on Variable Products

add_filter( 'woocommerce_variable_sale_price_html', 'variation_price_format', 10, 2 );

add_filter( 'woocommerce_variable_price_html', 'variation_price_format', 10, 2 );

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

    // Sale Price
    $prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
    sort( $prices );
    $saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'from %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    if ( $price !== $saleprice ) {
        $price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
    }

    return $price;
}
php wordpress woocommerce product-price
1个回答
0
投票

在没有任何保证的情况下,试试这个:

add_filter('wc_measurement_price_calculator_get_price_html', 'm_p_caculator_variable_product_prices', 10, 1 );
function m_p_caculator_variable_product_prices( $price_html ) {
    if( ! $product->is_type('variable') ) return $price_html; // Only variable products
    // Main Price
    $prices = array(  $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
    $price_html = $prices[0] !== $prices[1] ? sprintf( __( 'from %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    // Sale Price
    $prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
    sort( $prices );
    $sale_price = $prices[0] !== $prices[1] ? sprintf( __( 'from %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    if ( $price_html !== $saleprice ) {
        $price_html = '<del>' . $saleprice . '</del> <ins>' . $price_html . '</ins>';
    }

    return $price_html;
}

代码位于活动子主题(或活动主题)的 function.php 文件中。未经测试。

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