我正在使用我在互联网上找到的这个脚本来删除价格范围,并按以下语法显示WooCommerce / WordPress中可变产品的最低价格:来自US $ xxxx
当变量产品只有一个变体时,我希望脚本能够做同样的事情。 有一个自动cron(bash + SQL脚本),可以删除不可用的产品。有时它会留下一个只有一个变体的可变产品,并列出这个变化的价格“从US $ xxx”看起来很荒谬,因为只有一个变体。)
如何将从US $ 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);
答案 0 :(得分: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文件中。
经过测试和工作。