• Hi!

    I am trying to implement the Snippet your provided on your website, however it doesn’t change Stock status by checking Qty. Is there something I need to add in your Snippet?

    /**
     * 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;
    }
  • The topic ‘Filter WooCommerce Backorder Status Based on Stock Quantity’ is closed to new replies.