Hello, Raif!
Yes, its an option but needs to done manually. I am trying to automate this process and adjust Woo default behaviour
By default product goes to “out of stock” and customer cannot purchase it if stock quantity is <1 and it makes sense. Since my website is working B2B and mostly everything goes as Backorders, I would like to automate this process a bit and make Woo to change stock status automatically when stock quantity goes below 1 and allow backorders.
This is a snippet I was hoping will help me out, but it doesn’t work as expected. Zero quantity products are still loaded from 1C to Woocommerce “as out of stock” and Backorders “not Allowed”
Is there any solution to achieve “On Backorder” if stock qt goes <1?
/**
* 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;
}