我正在创建一个包含 4000 种产品的 WooCommerce 商店。产品列表由制造商每天更新,然后通过此网上商店的 FTP 进行更新。
不幸的是,制造商给出的值为“0”作为非折扣产品的销售价格。我怎样才能删除这个值?
我尝试使用以下代码:
add_filter('woocommerce_get_price_html', 'custom_price_html', 10, 2 );
function custom_price_html( $price, $product ) {
if ( $product->is_on_sale() && $product->get_sale_price() == '0' ) {
$price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ) . $product->get_price_suffix();
}
return $price;
}
但只有显示的价格发生变化(网上商店显示原价),但如果我将其添加到购物车中,则会将其考虑为价格“0”。
我不仅希望“0”促销价显示原价,还想将其删除。
顺便说一句,价格过滤器在网站上也不起作用,因为它认为它是“0”价格。
您的代码中缺少一些内容...您应该尝试使用以下内容,这将过滤零销售价格,并重置它们:
// Adjust product active price
add_filter('woocommerce_product_get_price', 'adjust_product_active_price', 20, 2);
add_filter('woocommerce_product_variation_get_price', 'adjust_product_active_price', 20, 2);
function adjust_product_active_price( $price, $product ) {
if ( $price == 0 && $product->get_regular_price() > 0 ) {
return floatval($product->get_regular_price());
}
return $price;
}
// Empty product sale price with zero value
add_filter('woocommerce_product_get_sale_price', 'adjust_product_zero_sale_price', 20, 2 );
add_filter('woocommerce_product_variation_get_sale_price', 'adjust_product_zero_sale_price', 20, 2 );
function adjust_product_zero_sale_price( $price, $product ) {
if ( $price == 0 ) {
return '';
}
return $price;
}
// Utility function (Remove zero sale prices)
function remove_zero_prices( $prices ) {
foreach( $prices as $key => $price ) {
if ( $price == 0 ) {
unset($prices[$key]);
}
}
return $prices;
}
// Adjust displayed variable price
add_filter( 'woocommerce_variable_price_html', 'adjust_variable_price_html', 20, 2 );
function adjust_variable_price_html( $price_html, $product ) {
$prices = $product->get_variation_prices( true );
if ( min($prices['sale_price']) > 0 ) {
return $price_html;
}
$active_prices = remove_zero_prices($prices['price']);
$regular_prices = $prices['regular_price'];
$min_price = min(array_merge($active_prices, $regular_prices));
$max_price = max(array_merge($active_prices, $regular_prices));
$min_reg_price = current($regular_prices);
$max_reg_price = end($regular_prices);
if ( $product->is_on_sale() && $min_reg_price === $max_reg_price ) {
$price_html = wc_format_sale_price( wc_price( $max_reg_price ), wc_price( $min_price ) );
} elseif ( $min_price !== $max_price ) {
$price_html = wc_format_price_range( $min_price, $max_price );
} else {
$price_html = wc_price( $min_price );
}
return $price_html;
}
add_filter('woocommerce_product_is_on_sale', 'filter_variable_product_is_on_sale', 20, 2);
function filter_variable_product_is_on_sale( $is_on_sale, $product ) {
if( $product->is_type('variable') ) {
$is_on_sale = false;
foreach( $product->get_available_variations('') as $variation ) {
if( $variation->is_on_sale() ) {
return true;
}
}
}
return $is_on_sale;
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。
相关:
您是否尝试在销售价格中添加空字符串?
function clear_zero_sale_price( $product ) {
if ( $product->get_sale_price() == '0' ) {
$product->set_sale_price( '' );
$product->save();
}
}
add_action( 'woocommerce_product_set_stock', 'clear_zero_sale_price' );
试试这个。
add_filter('woocommerce_get_price_html', 'custom_display_price_if_sale_zero', 100, 2);
function custom_display_price_if_sale_zero($price, $product) {
if ($product->is_on_sale() && $product->get_sale_price() == 0) {
$regular_price = wc_price($product->get_regular_price());
$price = sprintf('<span class="woocommerce-Price-amount amount">%s</span>', $regular_price);
}
return $price;
}
add_action( 'woocommerce_before_calculate_totals', 'change_sale_price_to_regular_price' );
function change_sale_price_to_regular_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) {
return;
}
foreach ( $cart->get_cart() as $cart_item ) {
$product = $cart_item['data'];
if ( $product->is_on_sale() && $product->get_sale_price() == 0 ) {
$product->set_price( $product->get_regular_price() );
}
}
}
还要在函数文件中添加此挂钩。 如果促销价为零我会将促销价替换为正常价格
挂钩 woocommerce_get_price_html:此过滤器允许我们修改产品价格的 HTML。
检查产品是否在促销且促销价格为零: is_on_sale() 方法检查产品是否在促销, get_sale_price() 方法检索促销价格。如果销售价格为零,则代码将执行。
显示常规价格:如果销售价格为零,则该函数使用 get_regular_price() 检索常规价格,使用 wc_price() 对其进行格式化,并将其设置为显示为产品价格。
通过将此代码添加到主题的functions.php 文件中,如果促销价设置为零,WooCommerce 将显示常规价格而不是促销价。 额外定制
如果您想添加一些自定义样式或消息来指示销售价格为零,您可以修改 sprintf 部分以根据需要包含其他 HTML 或文本