您会看到,如果价格为空,除了不显示产品库存状态外,此价格(HTML 代码)也不会显示。因为如果价格为空,该产品将被视为不可购买。
所以为了显示产品库存状态,如果价格为空,您应该使产品可购买。但是,我不相信这是你的意图?
因此,与其专注于显示产品库存状态,不如在通常显示价格的地方显示其他内容(HTML/文本)会简单得多,
这可以通过使用woocommerce_empty_price_html 过滤钩来完成
- 因此,您要么放弃产品库存状态的整个想法,而是简单地使用:
function filter_woocommerce_empty_price_html( $html, $product ) {
// NOT true on a single product page, RETURN
if ( ! is_product() ) return $html;
// Add HTML
$html = '<p>My text</p>';
return $html;
}
add_filter( 'woocommerce_empty_price_html', 'filter_woocommerce_empty_price_html', 10, 2 );
- 或者您在产品库存状态的基础上进一步构建,并在此基础上获得:
function filter_woocommerce_empty_price_html( $html, $product ) {
// NOT true on a single product page, RETURN
if ( ! is_product() ) return $html;
// Get stock status
$product_stock_status = $product->get_stock_status();
// Compare
if ( $product_stock_status == 'MY CUSTOM STATUS' ) {
// Add HTML
$html = '<p>My text based on product stock status</p>';
}
return $html;
}
add_filter( 'woocommerce_empty_price_html', 'filter_woocommerce_empty_price_html', 10, 2 );
此解决方案的唯一缺点是,您的新 HTML/文本不会显示在您通常看到产品库存状态的位置,而是显示在通常显示价格的位置。