Search code examples
wordpresswoocommercehook-woocommerce

Prevent admin from making orders for out of stock products in Woocommerce


I don't want any out of stock products to be added in admin order page!

Refer to this topic Prevent woocommerce to create manual order from out of stock products

Any tricks or custom code provide would appreciate much.

Thanks.

enter image description here


Solution

  • You can try it like this:

    add_filter( 'woocommerce_ajax_add_order_item_validation', 'rmg_woocommerce_ajax_add_order_item_validation', 10, 4 );
    function rmg_woocommerce_ajax_add_order_item_validation( $validation_error, $product, $order, $qty ) {
        if ( $validation_error && !$product->is_in_stock() ) {
            $validation_error->add( 'product-out-of-stock', __('Product Out of Stock', 'woocommerce') );
        }
        if ( $validation_error && ( $product->get_stock_quantity() < $qty ) ) {
            $validation_error->add( 'product-low-stock', __('Product low of Stock', 'woocommerce') );
        }
        return $validation_error;
    }
    

    You may need to add more logic to this as this is the simplest.