If you are syncing inventory from your POS to your WooCommerce store you understand the importance of the correct backorder status. WP Data Sync plugin it packed with action hooks and filters to make this an easy customization.
In the code example below, we are setting the backorder status based on the stock quantity provided by your data source using the wp_data_sync_{$meta_key}_value
filter.
Add the code snippet below to your themes function.php
file.
Code Example
/**
* Filter WooCommerce Backorder Status Based on Stock Qty
*
* Set the backorder staus when the stock qty is less than 1.
*
* @package WPDataSync
*/
// WooCommerce _stock value filter
add_filter( 'wp_data_sync__stock_value', 'wp_data_sync_backorder_status_by_stock_qty', 10, 3 );
/**
* WP Data Sync Set Backorder Status by Stock Qty.
*
* @param string $price
* @param int $product_id
* @param WP_DataSync\App\DataSync $data_sync
*
* @return mixed
*/
function wp_data_sync_backorder_status_by_stock_qty( $qty, $product_id, $data_sync ) {
if ( empty( $qty ) ) {
return $qty;
}
if ( ! function_exists( 'wc_get_product' ) ) {
return $qty;
}
$product = wc_get_product( $product_id );
// Default status
$status = 'instock';
if ( intval( $qty ) < 1 ) {
$status = 'onbackorder';
}
$product->set_stock_status( $status );
$product->save();
return $qty;
}
Get Started
-
Starter Plan
$39.00 / monthMore Info- 10,000 Requests/Month
- Unlimited Data Sources
- Unlimited Websites
- Unlimited Users
Basic Plan
$69.00 / monthMore Info- 40,000 Requests/Month
- Unlimited Data Sources
- Unlimited Websites
- Unlimited Users
Professional Plan
$129.00 / monthMore Info- 150,000 Requests/Month
- Unlimited Data Sources
- Unlimited Websites
- Unlimited Users
Data Source Mapping
$149.00 – $999.00More Info This product has multiple variants. The options may be chosen on the product pageLet us do the mapping for you!!We will map all your data sources for you, so you can move on to other projects that demand your time. One of our experts will map up to 50 fields for you in each data source.