• Resolved eggrace

    (@eggrace)


    Hi,
    I am running a script that displays a message to enforce a minimum order volume – and it works.
    However, now I have wholesale and retail customers.
    So I only want to run the script for ‘customer’ roles.
    I have added at the beginning:

    // Get the current user
    $current_user = wp_get_current_user();

    if ( in_array( ‘customer’, $current_user->roles ) ) {

    This just seems to stop it working for everybody regardless of user type.

    Can anybody help?
    Thank you

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hey,

    This is a bit hard to say without seeing the rest of the code. You should use something like

    // Check current user role
    $user = wp_get_current_user();
    $roles = ( array ) $user->roles;
    		
    if ( in_array( 'user1', $roles ) ) {
        //bla bla...
    }

    You may need to do a print_r or var_dump from $current_user to see what information it contains before you proceed.

    Regards

    Thread Starter eggrace

    (@eggrace)

    Thank you so much for your assistance.

    Below is the full code:

    // Check current user role
    $user = wp_get_current_user();
    $roles = ( array ) $user->roles;

    if ( in_array( ‘customer’, $roles ) ) {

    // Set a minimum number of products requirement before checking out
    add_action( ‘woocommerce_check_cart_items’, ‘spyr_set_min_num_products’ );
    function spyr_set_min_num_products() {

    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {
    global $woocommerce;

    // Set the minimum number of products before checking out
    $minimum_num_products = 24;
    // Get the Cart’s total number of products
    $cart_num_products = WC()->cart->cart_contents_count;

    // Compare values and add an error is Cart’s total number of products
    // happens to be less than the minimum required before checking out.
    // Will display a message along the lines of
    // A Minimum of 20 products is required before checking out. (Cont. below)
    // Current number of items in the cart: 6
    if( $cart_num_products < $minimum_num_products ) {
    wc_add_notice( sprintf( ‘We sell bottles in packs of %s.
    . ‘<br />Current number of items in the cart: %s.’,
    $minimum_num_products,
    $cart_num_products ),
    ‘error’ );
    }
    }
    }
    }

    Unfortunately this does not run at all now regardless of the user role.

    I am a bit of a notice.
    I have set default site visitor and accounts as ‘Customer’ role type.
    I also manually set up 2 wholesale account types.

    I only want the above script to run on the default ‘customers’; i don’t want it to run for my wholesale accounts.

    Am I going about this the right way?

    Many thanks for your help

    // Set a minimum number of products requirement before checking out
    add_action( 'woocommerce_check_cart_items', 'spyr_set_min_num_products' );
    function spyr_set_min_num_products() {
        // Check current user role
        $user = wp_get_current_user();
        $roles = ( array ) $user->roles;
    
        // Only run in the Cart or Checkout pages & userrole matches
        if( is_cart() || is_checkout() && in_array( ‘customer’, $roles ) ) {
            global $woocommerce;
            
            // Set the minimum number of products before checking out
            $minimum_num_products = 24;
            
            // Get the Cart’s total number of products
            $cart_num_products = WC()->cart->cart_contents_count;
            
            // Compare values and add an error is Cart’s total number of products
            // happens to be less than the minimum required before checking out.
            // Will display a message along the lines of
            // A Minimum of 20 products is required before checking out. (Cont. below)
            // Current number of items in the cart: 6
            if( $cart_num_products < $minimum_num_products ) {
                wc_add_notice( sprintf( 'We sell bottles in packs of %s.'
                . 'Current number of items in the cart: %s.',
                $minimum_num_products,
                $cart_num_products ),
                'error' );
            }
        }
    }

    Give it a try this way, the code where you are controlling the user is outside of the function.

    Thread Starter eggrace

    (@eggrace)

    I feel so bad for taking up your time and thank you for your assistance.
    Unfortunately it still works for all roles.
    Am I right in thinking that ‘customer’ is a standard WordPress role and I am not doing anything silly?

    Run script for ‘customer’
    or
    Not run script for ‘wholesale_buyer’ and ‘wholesale_tax_free’

    Maybe it needs an exception rather than an inclusion?

    Sorry for the problems and thank you

    Can you give this a try? On the cart page you should see an array with the users role(s). Also the else statement if not satisfied = ‘NOT’.

    If this works, you can delete the ‘echo…’ and ‘else…’ code

    // Set a minimum number of products requirement before checking out
    add_action( 'woocommerce_check_cart_items', 'spyr_set_min_num_products' );
    function spyr_set_min_num_products() {
        // Only run in the Cart or Checkout pages & userrole matches
        if( is_cart() || is_checkout() ) {
    		
    		// Check current user role
    		$user = wp_get_current_user();
    		$roles = ( array ) $user->roles;
    	
    		echo '<pre>', print_r($roles, 1), '</pre>';
    		
    		if ( in_array( 'customer', $roles ) ) {
    			global $woocommerce;
    			
    			// Set the minimum number of products before checking out
    			$minimum_num_products = 24;
    			
    			// Get the Cart’s total number of products
    			$cart_num_products = WC()->cart->cart_contents_count;
    			
    			// Compare values and add an error is Cart’s total number of products
    			// happens to be less than the minimum required before checking out.
    			// Will display a message along the lines of
    			// A Minimum of 20 products is required before checking out. (Cont. below)
    			// Current number of items in the cart: 6
    			if( $cart_num_products < $minimum_num_products ) {
    				wc_add_notice( sprintf( 'We sell bottles in packs of %s.'
    				. 'Current number of items in the cart: %s.',
    				$minimum_num_products,
    				$cart_num_products ),
    				'error' );
    			}
    		} else {
    			echo 'NOT';
    		}
        }
    }
    Thread Starter eggrace

    (@eggrace)

    Thank you so much, this has worked and I can see what is now the problem.
    A non logged in visitor is not a ‘customer’ but they can still order.
    But i cannot find anywhere what role is assigned to a site visitor.
    Is there default that you know of?
    Thank you

    Hi,

    There is no user role for guests but $roles will be empty if nothing is found, so we can assign a ‘guest’ role ourselves

    add_action( 'woocommerce_check_cart_items', 'spyr_set_min_num_products' );
    function spyr_set_min_num_products() {
        // Only run in the Cart or Checkout pages & userrole matches
        if( is_cart() || is_checkout() ) {
    		
            // Check current user role
            $user = wp_get_current_user();
    		
            // Get userrole
            $roles = $user->roles;
    		
            // No user role found, user role = guest
            if ( empty($roles) ) {
                $roles[] = 'guest';
            }
    	
            echo '<pre>', print_r($roles, 1), '</pre>';
    		
            if ( in_array( 'customer', $roles ) ) {
                echo 'customer';
            } elseif ( in_array( 'guest', $roles ) ) {
                echo 'guest';
            } else {
                echo 'something else';
            }
        }
    }
    Thread Starter eggrace

    (@eggrace)

    This worked!
    Thank you so much for your time and experience.
    Cheers

    Hi there,

    I am glad to hear there is a solution! I will go ahead and close this thread ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Only running a script for a particular role’ is closed to new replies.