Add custom CSS class to “add to cart” button on WooCommerce shop and archives pages based on certain condition

Solution:

To add/edit/remove CSS classes from the existing add to cart button on WooCommerce shop and archives pages you can use the woocommerce_loop_add_to_cart_args filter hook

So you get:

function action_woocommerce_loop_add_to_cart_args( $wp_parse_args, $product ) {
    // Initialize
    $custom_class = '';
    
    // Not allowed
    if ( $product->get_stock_quantity() <= 0 ) {
        $custom_class = 'button-not-allowed';
    }
    
    // NOT empty
    if ( ! empty ( $custom_class ) ) {  
        // Class
        $wp_parse_args['class'] = implode(
            ' ',
            array_filter(
                array(
                    'button' . ' ' . $custom_class,
                    'product_type_' . $product->get_type(),
                    $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
                    $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
                )
            )
        );
    }

    return $wp_parse_args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'action_woocommerce_loop_add_to_cart_args', 10, 2 );

Note: the $product->get_stock_quantity() function is best used in combination with $product->managing_stock(), but this depends on your needs


Then apply the following CSS

.button-not-allowed {
    cursor: not-allowed !important;
}