feat: optionally create missing WooCommerce products from BC items
The plugin only ever updated existing WooCommerce products - nothing created new ones, so a category-filtered sync would just skip every BC item without a matching product, no matter how the filter was configured. Adds an opt-in "Create Missing Products" setting (default off, so existing update-only behavior is unchanged unless explicitly enabled) that creates a bare WC_Product_Simple (name, SKU from GTIN/number, configurable draft/publish status) for unmatched BC items in the category-filtered sync path, then runs it straight through the existing update_product() call for stock/price/brand - no duplicated logic. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -285,6 +285,12 @@ class WBC_Product_Sync {
|
||||
$this->results['total']++;
|
||||
|
||||
$product = $this->find_wc_product_for_item( $item );
|
||||
$is_new = false;
|
||||
|
||||
if ( ! $product && get_option( 'wbc_create_missing_products', 'no' ) === 'yes' ) {
|
||||
$product = $this->create_wc_product_from_item( $item );
|
||||
$is_new = (bool) $product;
|
||||
}
|
||||
|
||||
if ( ! $product ) {
|
||||
$this->results['skipped']++;
|
||||
@@ -308,11 +314,54 @@ class WBC_Product_Sync {
|
||||
|
||||
if ( $updated ) {
|
||||
$this->results['success']++;
|
||||
if ( $is_new ) {
|
||||
WBC_Logger::info( 'ProductSync', 'Created new WooCommerce product from BC item', array(
|
||||
'product_id' => $product->get_id(),
|
||||
'item_number' => $item['number'] ?? '',
|
||||
'sku' => $product->get_sku(),
|
||||
) );
|
||||
}
|
||||
} else {
|
||||
$this->results['failed']++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new WooCommerce product from a BC item
|
||||
*
|
||||
* Bare creation only (name, SKU, status) - stock/price/brand are filled
|
||||
* in right after by the normal update_product() call, so that logic
|
||||
* isn't duplicated here.
|
||||
*
|
||||
* @param array $item BC item data.
|
||||
* @return WC_Product|false
|
||||
*/
|
||||
private function create_wc_product_from_item( $item ) {
|
||||
$sku = ! empty( $item['gtin'] ) ? $item['gtin'] : ( $item['number'] ?? '' );
|
||||
|
||||
if ( empty( $sku ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( $item['displayName'] ?? $sku );
|
||||
|
||||
try {
|
||||
$product->set_sku( $sku );
|
||||
$product->set_status( get_option( 'wbc_new_product_status', 'draft' ) === 'publish' ? 'publish' : 'draft' );
|
||||
$product->save();
|
||||
} catch ( Exception $e ) {
|
||||
WBC_Logger::error( 'ProductSync', 'Failed to create WooCommerce product from BC item', array(
|
||||
'item_number' => $item['number'] ?? '',
|
||||
'sku' => $sku,
|
||||
'error' => $e->getMessage(),
|
||||
) );
|
||||
return false;
|
||||
}
|
||||
|
||||
return $product;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the WooCommerce product matching a BC item, via a local SKU lookup
|
||||
* (no BC round trip) - tries GTIN, then item number, then Item Reference
|
||||
|
||||
Reference in New Issue
Block a user