• Resolved makc57

    (@makc57)


    Hi!

    I have the following problem:
    By default, when products are bulk-imported to WooCommerce with stock quantity=0, it show stock status “Out of stock” and it cannot be added to cart.

    I would like to keep possibility to purchase “out of stock” products once they are imported.

    Is there any code snippet to allow it, or lets say auto-change stock status by default from out of stock to Backorders?

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Raif D. a11n

    (@rdeari)

    Hi @makc57 !

    I understand that you are importing your products to the site. Are you using a CSV file for that?

    If so, you can change the backorder availability by changing the Backorders allowed? column.

    Here is a screenshot for reference:

    https://d.pr/i/TKtVQF

    Thread Starter makc57

    (@makc57)

    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;
    }

    Hello,

    Please note that our support for custom code is really limited, however, I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the following pages for resources and also you will be able to find someone to help you with the code:
    WooCommerce Developer Resources Portal.
    WooCommerce Facebook group.
    WooCommerce Community Slack.

    As a reference you can use this plugin to help you manage the backorders:
    https://www.ads-software.com/plugins/woo-backorder-manager/

    I hope this points you in the right direction.

    We haven’t heard back from you in a while (I think that at this point you have been able to find a solution), so I’m going to mark this as resolved – we’ll be here if and/or when you are ready to continue.

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add to cart out of stock products’ is closed to new replies.