Woocommerce 仅显示折扣可变产品的一个价格
Woocommerce Show only one price for variable product on discount
这是我的 WooCommerce 网站:sweetworldcandy.com
问题是,可变产品价格最低和最高
value is showing what I want is show what I want is if the product is not on sale show the least value if it is on sale show the least value and the least value of offer price 通过添加斜杠作为删除标签
我在下面分享了这三张图片以供参考:
2020 更新 (对于 Woocommerce 3+)
从 Woocommerce 3+ 开始,已将弃用函数 woocommerce_price()
替换为 wc_price()
:
add_filter('woocommerce_variable_sale_price_html', 'shop_variable_product_price', 10, 2);
add_filter('woocommerce_variable_price_html','shop_variable_product_price', 10, 2 );
function shop_variable_product_price( $price, $product ){
$variation_min_reg_price = $product->get_variation_regular_price('min', true);
$variation_min_sale_price = $product->get_variation_sale_price('min', true);
if ( $product->is_on_sale() && !empty($variation_min_sale_price)){
if ( !empty($variation_min_sale_price) )
$price = '<del class="strike">' . wc_price($variation_min_reg_price) . '</del>
<ins class="highlight">' . wc_price($variation_min_sale_price) . '</ins>';
} else {
if(!empty($variation_min_reg_price))
$price = '<ins class="highlight">'.wc_price( $variation_min_reg_price ).'</ins>';
else
$price = '<ins class="highlight">'.wc_price( $product->regular_price ).'</ins>';
}
return $price;
}
原始线程(WooCommerce 3 之前):下面的代码应该符合您的预期:
add_filter('woocommerce_variable_sale_price_html', 'shop_variable_product_price', 10, 2);
add_filter('woocommerce_variable_price_html','shop_variable_product_price', 10, 2 );
function shop_variable_product_price( $price, $product ){
$variation_min_reg_price = $product->get_variation_regular_price('min', true);
$variation_min_sale_price = $product->get_variation_sale_price('min', true);
if ( $product->is_on_sale() && !empty($variation_min_sale_price)){
if ( !empty($variation_min_sale_price) )
$price = '<del class="strike">' . woocommerce_price($variation_min_reg_price) . '</del>
<ins class="highlight">' . woocommerce_price($variation_min_sale_price) . '</ins>';
} else {
if(!empty($variation_min_reg_price))
$price = '<ins class="highlight">'.woocommerce_price( $variation_min_reg_price ).'</ins>';
else
$price = '<ins class="highlight">'.woocommerce_price( $product->regular_price ).'</ins>';
}
return $price;
}
代码在您的活动子主题(或主题)的 function.php 文件或任何插件文件中。
此代码已经过测试并且有效。
这是我的 WooCommerce 网站:sweetworldcandy.com
问题是,可变产品价格最低和最高 value is showing what I want is show what I want is if the product is not on sale show the least value if it is on sale show the least value and the least value of offer price 通过添加斜杠作为删除标签
我在下面分享了这三张图片以供参考:
2020 更新 (对于 Woocommerce 3+)
从 Woocommerce 3+ 开始,已将弃用函数 woocommerce_price()
替换为 wc_price()
:
add_filter('woocommerce_variable_sale_price_html', 'shop_variable_product_price', 10, 2);
add_filter('woocommerce_variable_price_html','shop_variable_product_price', 10, 2 );
function shop_variable_product_price( $price, $product ){
$variation_min_reg_price = $product->get_variation_regular_price('min', true);
$variation_min_sale_price = $product->get_variation_sale_price('min', true);
if ( $product->is_on_sale() && !empty($variation_min_sale_price)){
if ( !empty($variation_min_sale_price) )
$price = '<del class="strike">' . wc_price($variation_min_reg_price) . '</del>
<ins class="highlight">' . wc_price($variation_min_sale_price) . '</ins>';
} else {
if(!empty($variation_min_reg_price))
$price = '<ins class="highlight">'.wc_price( $variation_min_reg_price ).'</ins>';
else
$price = '<ins class="highlight">'.wc_price( $product->regular_price ).'</ins>';
}
return $price;
}
原始线程(WooCommerce 3 之前):下面的代码应该符合您的预期:
add_filter('woocommerce_variable_sale_price_html', 'shop_variable_product_price', 10, 2);
add_filter('woocommerce_variable_price_html','shop_variable_product_price', 10, 2 );
function shop_variable_product_price( $price, $product ){
$variation_min_reg_price = $product->get_variation_regular_price('min', true);
$variation_min_sale_price = $product->get_variation_sale_price('min', true);
if ( $product->is_on_sale() && !empty($variation_min_sale_price)){
if ( !empty($variation_min_sale_price) )
$price = '<del class="strike">' . woocommerce_price($variation_min_reg_price) . '</del>
<ins class="highlight">' . woocommerce_price($variation_min_sale_price) . '</ins>';
} else {
if(!empty($variation_min_reg_price))
$price = '<ins class="highlight">'.woocommerce_price( $variation_min_reg_price ).'</ins>';
else
$price = '<ins class="highlight">'.woocommerce_price( $product->regular_price ).'</ins>';
}
return $price;
}
代码在您的活动子主题(或主题)的 function.php 文件或任何插件文件中。
此代码已经过测试并且有效。