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:
@@ -89,6 +89,12 @@ class WBC_Admin {
|
||||
register_setting( 'wbc_sync', 'wbc_category_parent_map', array(
|
||||
'sanitize_callback' => 'sanitize_textarea_field',
|
||||
) );
|
||||
register_setting( 'wbc_sync', 'wbc_create_missing_products', array(
|
||||
'sanitize_callback' => array( $this, 'sanitize_checkbox' ),
|
||||
) );
|
||||
register_setting( 'wbc_sync', 'wbc_new_product_status', array(
|
||||
'sanitize_callback' => array( $this, 'sanitize_new_product_status' ),
|
||||
) );
|
||||
|
||||
// Order settings (own group)
|
||||
register_setting( 'wbc_orders', 'wbc_enable_order_sync', array(
|
||||
@@ -173,6 +179,16 @@ class WBC_Admin {
|
||||
return $value === 'yes' ? 'yes' : 'no';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize the new-product status setting
|
||||
*
|
||||
* @param string $value Input value.
|
||||
* @return string 'publish' or 'draft'.
|
||||
*/
|
||||
public function sanitize_new_product_status( $value ) {
|
||||
return $value === 'publish' ? 'publish' : 'draft';
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue admin scripts
|
||||
*
|
||||
|
||||
@@ -225,6 +225,40 @@ $tabs = array(
|
||||
<p class="description">
|
||||
<?php esc_html_e( 'Optional. Comma-separated BC item category codes. Only products whose BC item category is in this list will be synced. Leave empty to sync all categories.', 'woo-business-central' ); ?>
|
||||
</p>
|
||||
<p class="description">
|
||||
<?php esc_html_e( 'When set, sync fetches items from BC filtered by category directly, instead of checking every WooCommerce product.', 'woo-business-central' ); ?>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><?php esc_html_e( 'Create Missing Products', 'woo-business-central' ); ?></th>
|
||||
<td>
|
||||
<label>
|
||||
<input type="checkbox" name="wbc_create_missing_products" value="yes"
|
||||
<?php checked( get_option( 'wbc_create_missing_products', 'no' ), 'yes' ); ?> />
|
||||
<?php esc_html_e( 'Create a new WooCommerce product when a BC item in an allowed category has no matching product', 'woo-business-central' ); ?>
|
||||
</label>
|
||||
<p class="description">
|
||||
<?php esc_html_e( 'Only applies with a Category Filter set (BC-first mode). Without a filter, sync only updates existing products and never creates new ones.', 'woo-business-central' ); ?>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="wbc_new_product_status"><?php esc_html_e( 'New Product Status', 'woo-business-central' ); ?></label>
|
||||
</th>
|
||||
<td>
|
||||
<select id="wbc_new_product_status" name="wbc_new_product_status">
|
||||
<option value="draft" <?php selected( get_option( 'wbc_new_product_status', 'draft' ), 'draft' ); ?>>
|
||||
<?php esc_html_e( 'Draft (review before publishing)', 'woo-business-central' ); ?>
|
||||
</option>
|
||||
<option value="publish" <?php selected( get_option( 'wbc_new_product_status' ), 'publish' ); ?>>
|
||||
<?php esc_html_e( 'Publish immediately', 'woo-business-central' ); ?>
|
||||
</option>
|
||||
</select>
|
||||
<p class="description">
|
||||
<?php esc_html_e( 'Status assigned to products created by "Create Missing Products".', 'woo-business-central' ); ?>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
|
||||
Reference in New Issue
Block a user