简体   繁体   中英

Woocommerce set minimum order for a specific user role

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!

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM