提问者:小点点

多个活动变体的自定义Woocommerce可变产品价格范围


我正在使用这个我在网上找到的脚本,在Woocommerce/WordPress中删除价格范围,只显示可变产品的最低价格,语法如下:From us$xxxx

当变量产品只有一个变体时,我希望脚本做同样的事情。有一个自动的cron(bash+SQL脚本)可以删除不可用的产品。有时,它只留下一个变化的产品,并列出一个价格说明“从XXX美元”的变化看起来是不恰当的,因为只有一个单一的变化)。

如何添加一个条件,将此从xxx美元适用于仅有一个以上变体的变体产品。我的主要目标是在目录/类别/商店页面上使用它,因为已经有一个片段从可变单产品页面中删除价格范围。多谢了。

add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format_310', 10, 2 );
function bbloomer_variation_price_format_310( $price, $product ) {

// 1. Find the minimum regular and sale prices

$min_var_reg_price = $product->get_variation_regular_price( 'min', true );
$min_var_sale_price = $product->get_variation_sale_price( 'min', true );

// 2. New $price

if ( $min_var_sale_price ) {
$price = sprintf( __( 'From %1$s', 'woocommerce' ), wc_price( $min_var_reg_price ) );
}

// 3. Return edited $price

return $price;
}

// Display Price For Variable Product With Same Variations Prices
add_filter('woocommerce_available_variation', function ($value, $object = null, $variation = null) {
    if ($value['price_html'] == '') {
        $value['price_html'] = '<span class="price">' . $variation->get_price_html() . '</span>';
    }
    return $value;
}, 10, 3);

共1个答案

匿名用户

在第一个函数中计算可见的子级将允许您实现这一点。此外,我还重新审视了您的第二个函数,它应该命名为:

add_filter( 'woocommerce_variable_price_html', 'custom_variation_price_html', 20, 2 );
function custom_variation_price_html( $price_html, $product ) {
    $visible_children = $product->get_visible_children();
    if( count($visible_children) <= 1 ) return $price_html; // Exit if only one variation

    $regular_price_min = $product->get_variation_regular_price( 'min', true );
    $sale_price_min = $product->get_variation_sale_price( 'min', true );

    if ( $sale_price_min ) 
        $price_html = __( 'From', 'woocommerce' ).' '.wc_price( $regular_price_min );

    return $price_html;
}

add_filter('woocommerce_available_variation', 'custom_available_variation', 20, 3 ) ;
function custom_available_variation( $args, $product, $variation ) {
    if( $args['price_html'] == '' )
        $args['price_html'] = '<span class="price">' . $variation->get_price_html() . '</span>';
    
    return $args;
}

这段代码将出现在活动子主题(或主题)的function.php文件中。

经过测试并起作用。