0

I am using Woocommerce set minimum order for a specific user role answer code and it works like a charm!

Though I would like to only have a minimum order amount if the product(s) placed in the shopping cart are not in stock (backorder). If the product(s) in the shopping cart are in stock, there should be no minimum order amount. Could someone help me out?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Sjors
  • 301
  • 4
  • 14

1 Answers1

1

To make the code work only when there is backordered items, you need to include in the code a check for backordered items as follows:

add_action( 'woocommerce_check_cart_items', 'set_min_total_per_user_role' );
function set_min_total_per_user_role() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {

        // Set minimum cart total (by user role)
        $minimum_cart_total = current_user_can('company') ? 250 : 100;

        // Total (before taxes and shipping charges)
        $total = WC()->cart->subtotal;
        
        $has_backordered_items = false;
        
        // Check for backordered cart items
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
                $has_backordered_items = true;
                break; // stop the loop
            }
        }

        // Add an error notice is cart total is less than the minimum required
        if( $has_backordered_items && $total <= $minimum_cart_total  ) {
            // Display our error message
            wc_add_notice( sprintf( '<strong>Dear customer, minimum order of %s is required to make a purchase on your site.</strong> <br>
                Your actual cart amount is: %s',
                wc_price($minimum_cart_total),
                wc_price($total)
            ), 'error' );
        }
    }
}

Code goes in functions.php file of the active child theme (or active theme). It should works.

Based on: Woocommerce set minimum order for a specific user role

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399