2

I have a php code that sets a 100 minimum order site wide, that works good. I want to set a different minimum order of 250 for a specific role 'company' using the same code or by cloning it and wrapping it in an if() statement, only I have no idea how to write it. Would appreciate any kind of help.
This it the currant code (don't mind the Hebrew text, its just the error messages, when the condition is not met):

// Set a minimum dollar amount per order
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' );
function spyr_set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
    global $woocommerce;

    // Set minimum cart total
    $minimum_cart_total = 100;

    // Total we are going to be using for the Math
    // This is before taxes and shipping charges
    $total = WC()->cart->subtotal;

    // Compare values and add an error is Cart's total
    // happens to be less than the minimum required before checking out.
    // Will display a message along the lines of
    // A Minimum of 10 USD is required before checking out. (Cont. below)
    // Current cart total: 6 USD 
    if( $total <= $minimum_cart_total  ) {
        // Display our error message
        wc_add_notice( sprintf( '<strong>לקוח יקר, יש צורך במינימום הזמנה 
של %s %s₪ על מנת לבצע רכישה באתר.</strong>'
            .'<br />סכום הביניים בעגלה הינו: %s %s₪',
            $minimum_cart_total,
            get_option( 'woocommerce_currency_symbol'),
            $total,
            get_option( 'woocommerce_currency_symbol') ),
        'error' );
    }
  }
}

Thanks!

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
fafchook
  • 819
  • 1
  • 13
  • 17

2 Answers2

3

Try the following using current_user_can(), so in your code:

// Set a minimum amount per order (and user role)
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
        $minimum_cart_total = current_user_can('company') ? 250 : 100;

        // Total (before taxes and shipping charges)
        $total = WC()->cart->subtotal;

        // Add an error notice is cart total is less than the minimum required
        if( $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 on function.php file of your active child theme (or active theme). It should works.

To format prices for display you can use the dedicated wc_price() formatting function.

Related: Apply a discount for a specific user role in Woocommerce

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks man! i get an error on line: wc_price($minimum_cart_total), – fafchook Feb 27 '19 at 05:42
  • @fafchook It's because the Hebrew characters… I have replaced with english and it works… – LoicTheAztec Feb 27 '19 at 05:56
  • OK that's weird. cause it worked fine in the previous code. do you know if there is a way to translate it? maybe via PO file? – fafchook Feb 27 '19 at 06:06
  • Got it. For some reason copying the code with the Hebrew characters produced an error, but modifying it after pasting did not. Thanks again my friend, you are a life saver! – fafchook Feb 27 '19 at 06:15
  • @fafchook Just enter the needed string in Hebrew and add `%s` where you need to have the formatted "minimum cart price" and the formatted "total". Then you can add in it the html tags ``, `` and `
    `… I can't do it myself as I don't understand Hebrew and as it is an RTL language.
    – LoicTheAztec Feb 27 '19 at 06:21
1

First you have to retrieve the roles of the current user.

$roles = is_user_logged_in() ? (array) wp_get_current_user()->roles : [];

Now you want to check if the role company is in the user's roles to determine the minimum.

$minimum_cart_total = in_array('company', $roles) ? 250 : 100;
Chin Leung
  • 14,621
  • 3
  • 34
  • 58