WooCommerce 3产品库存通知异常:缺货时通知不显示
Fix: WooCommerce Out-of-Stock Notification Not Showing on Shop/Archive Pages
Hey Juraj, let's break down why your out-of-stock notifications aren't appearing and fix it up. The core issues are incorrect stock management checks and messed-up logic priority in your conditional statements—here's how to resolve it:
Key Problems in Your Original Code
- Incorrect stock management check: Using
if ( $product->stock )isn't the right way to verify if a product has stock management enabled. WooCommerce provides a dedicated method for this, and direct property access can lead to compatibility issues. - Logical operator priority issues: Your
&&and||combinations lack parentheses, so the code evaluates conditions in an order you didn't intend. For example,$product->get_stock_quantity() > 0 && $regular >= 500 || $sale >= 500gets parsed as($product->stock >0 && $regular >=500) OR $sale >=500, which skips the out-of-stock cases entirely when a sale price meets the threshold.
Fixed Code
add_action( 'woocommerce_after_shop_loop_item','show_stock_shop', 90 ); function show_stock_shop() { global $product; // Get current active price (handles regular and sale prices automatically) $current_price = (float) $product->get_price(); $is_in_stock = $product->is_in_stock(); // Only run logic if stock management is enabled for the product if ( $product->managing_stock() ) { // Simplify free shipping qualification check $qualifies_for_free_shipping = $current_price >= 500; if ( $is_in_stock && $qualifies_for_free_shipping ) { echo '<div class="remaining text-center pt-2 pb-2">Skladom. <span class="doprava-zadarmo">Doprava zadarmo</span></div>'; } elseif ( !$is_in_stock && $qualifies_for_free_shipping ) { echo '<div class="remaining text-center pt-2 pb-2"><s>(nie je skladom)</s>. <span class="doprava-zadarmo">Doprava zadarmo</span></div>'; } elseif ( $is_in_stock && !$qualifies_for_free_shipping ) { echo '<div class="remaining text-center pt-2 pb-2">Skladom. <span class="doprava-zadarmo">Doprava zadarmo od 500 eur</span></div>'; } elseif ( !$is_in_stock && !$qualifies_for_free_shipping ) { echo '<div class="remaining text-center pt-2 pb-2"><s>(nie je skladom)</s>. <span class="doprava-zadarmo">Doprava zadarmo od 500 eur</span></div>'; } else { // Fallback for special stock statuses (e.g., backorders) echo '<div class="remaining text-center pt-2 pb-2">Do ' . /*$stock .*/ ' dní</div>'; } } }
What Changed?
$product->managing_stock()instead of$product->stock: This is WooCommerce's official, future-proof method to check if stock management is enabled for a product.- Simplified price handling: Using
$product->get_price()automatically grabs the correct active price (regular or sale) instead of checking two separate variables. - Clearer condition variables: Extracting
$is_in_stockand$qualifies_for_free_shippingmakes the code easier to read and eliminates logical priority errors. - Explicit out-of-stock checks: Using
!$is_in_stockensures we correctly target products with 0 stock.
Test this with in-stock, out-of-stock, sale, and regular-priced products to confirm all notification cases work as expected.
内容的提问来源于stack exchange,提问作者Juraj
相关产品推荐
相关产品推荐

