Function Overview
The conditional_stock_status_update_by_sku function dynamically updates the stock status of WooCommerce products during import based on certain conditions. It checks if the „_manage_stock“ is unchecked (disabled) and only updates the „_stock_status“ if it’s not set to custom statuses like „contact“ or „catalog_only“.
function conditional_stock_status_update_by_sku($sku, $new_stock_status) {
// Fetch the product by SKU
$product_id = wc_get_product_id_by_sku($sku);
$product = wc_get_product($product_id);
// If no product is found, return an empty string or handle as needed
if (!$product) {
return '';
}
// Define your custom stock statuses
$custom_statuses = ['contact', 'catalog_only'];
// Get the existing stock status
$existing_stock_status = $product->get_stock_status();
// Check if the existing stock status is one of the custom statuses
if (in_array($existing_stock_status, $custom_statuses)) {
// Return the existing stock status, don't update
return $existing_stock_status;
}
// Check if manage stock is enabled for this product
$manage_stock = $product->get_manage_stock();
// If manage stock is enabled, don't update and return the existing stock status
if ($manage_stock) {
return $existing_stock_status;
}
// If manage stock is not enabled and the stock status is not a custom one, update with the new stock status
return $new_stock_status;
}Implementation Steps
- Add the Function to WP All Import:
- Navigate to WP All Import Pro and open the Function Editor.
- Copy and paste the provided PHP function into the editor.
- Save your changes.
- Use the Function in Your Import Template:
- In the import template, locate the field mapping for stock status.
- Ensure {ItemCode[1]} corresponds to the SKU in your import file, and
{stock_status}matches the field for new stock statuses. - Replace the direct mapping with the following shortcode
[conditional_stock_status_update_by_sku({ItemCode[1]}, "{stock_status}")]Troubleshooting
- Function Not Working: Ensure the function is correctly pasted in WP All Import’s Function Editor and the shortcode is properly set up in your import template.
- Incorrect Stock Status Update: Verify that your import file’s SKU and stock status fields are correctly mapped in the shortcode.
Conclusion
This custom function is a powerful tool for managing stock statuses in WooCommerce via WP All Import Pro. It offers flexibility and control, ensuring that specific stock statuses are maintained according to your business rules.
Hafðu samband við Kaktus fyrir nánari upplýsingar um þessa virkni.