From 5072a967ca4b0c10f68e268c644cb0d31864341c Mon Sep 17 00:00:00 2001 From: Malin Date: Thu, 9 Jul 2026 15:02:54 +0200 Subject: [PATCH] fix: zero-stock products left unmanaged with default "in stock" status get_stock_quantity() on a never-managed product returns 0, which equaled BC's inventory for out-of-stock items, so the "did the value change?" guard treated it as no-op and never called set_manage_stock(true). The product was left at WooCommerce's default stock_status of "instock" with no quantity tracked - looked fine for in-stock items (real quantity forced the block to run) but silently wrong for anything actually out of stock. Now also runs whenever stock isn't managed yet, regardless of whether the value looks unchanged. Also enables manage_stock at product creation time so newly created products never sit in that ambiguous state to begin with. Co-Authored-By: Claude Sonnet 5 --- .../includes/class-wbc-product-sync.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/woo-business-central/includes/class-wbc-product-sync.php b/woo-business-central/includes/class-wbc-product-sync.php index 43f9983..eb9af90 100644 --- a/woo-business-central/includes/class-wbc-product-sync.php +++ b/woo-business-central/includes/class-wbc-product-sync.php @@ -349,6 +349,7 @@ class WBC_Product_Sync { try { $product->set_sku( $sku ); $product->set_status( get_option( 'wbc_new_product_status', 'draft' ) === 'publish' ? 'publish' : 'draft' ); + $product->set_manage_stock( true ); $product->save(); } catch ( Exception $e ) { WBC_Logger::error( 'ProductSync', 'Failed to create WooCommerce product from BC item', array( @@ -561,9 +562,16 @@ class WBC_Product_Sync { if ( get_option( 'wbc_enable_stock_sync', 'yes' ) === 'yes' ) { $stock = isset( $item['inventory'] ) ? (float) $item['inventory'] : 0; $current_stock = (float) $product->get_stock_quantity(); + $needs_manage_stock = ! $product->get_manage_stock(); - if ( $stock !== $current_stock ) { - if ( ! $product->get_manage_stock() ) { + // Also run this when stock management isn't on yet, even if the + // (defaulted, unmanaged) current value happens to already equal + // the BC value - otherwise a brand-new product with 0 BC stock + // never gets manage_stock enabled (0 !== 0 looks like "no + // change") and is left at WooCommerce's default stock_status + // of "instock" with no quantity tracked at all. + if ( $stock !== $current_stock || $needs_manage_stock ) { + if ( $needs_manage_stock ) { $product->set_manage_stock( true ); }