Jump to content

martini0

Members
  • Posts

    26
  • Joined

  • Last visited

Posts posted by martini0

  1. I have got the fix!

    add_filter( 'woocommerce_package_rates', 'shipping_methods_based_on_wholesale_customer', 10, 2 );
    function shipping_methods_based_on_wholesale_customer( $rates, $package ) {
       $user  = wp_get_current_user();
       $roles = (array) $user->roles;
       // Set the shipping methods rate ids in the arrays.
       if ( ! in_array( 'wholesale_customer', $roles, true ) ) {
          $shipping_rates_ids = array( 'flat_rate:10', 'flat_rate:7' ); // To be removed for NON Wholesale users.
       } else {
          $shipping_rates_ids = array( 'flat_rate:13', 'flat_rate:15' ); // To be removed for Wholesale users.
       }
       // Loop through shipping rates from the current shipping package.
       foreach ( $rates as $rate_key => $rate ) {
          if ( in_array( $rate_key, $shipping_rates_ids, true ) ) {
             unset( $rates[ $rate_key ] );
          }
       }
       return $rates;
    }
    Quote
    Quote

     

     

     

  2. 1. I have paste the code in functions.php

    2. Where should I see the output? I am working in WordPress.

    3. Not sure if I am allowed to ask this here, if not, I will erase point 3. Would someone be willing to debug and make the code work? As a gesture, I can make a donation. 🙂 Please tell me if I am not allowed to post this here.

  3. 58 minutes ago, maxxd said:

    Apparently I was incorrect; I thought the third parameter of get_user_meta was a default value. It's actually telling the function what type of value to return - an array if false, a value if true So it sounds like all your registered users are set to have a meta value of wholesale_customer = true.

    How is the system creating the distinction between wholesale and non-wholesale user accounts?

    Via WooCommerce Wholesale Prices -> it makes a new user ID which is wholesale_customer

  4. $is_wholesale = get_user_meta( 153, 'wholesale_customer', true );  -> tried but no effect.

    Does someone know a work around? Maybe when the code checks for logged in user, it works. But how? It doesn't have to target wholesale_customers only. Because only wholesale_customer needs to log in. Nobody else can log in. Could someone change the code to check for logged in, or not logged in?

  5. So I tried a second piece of code. Same issue. It isn't targeting wholesale_users. IT just removed the flat_rate:13 for ALL users guest, subscribers, and wholesale_users.

    /**
     * Removes shipping methods for non-wholesale customers.
     * Please be sure to clear your WooCommerce store's cache.
     * Adjust 'flat_rate:2' to match that of your wholesale shipping method.
     */
     
    function my_wcs_remove_shipping_non_wholesale( $rates, $package ){
    	global $current_user;
    
    	$is_wholesale = get_user_meta( $current_user->ID, 'wholesale_customer', true );
    
    	if ( ! $is_wholesale ) {
    		foreach( $rates as $method ) {
    			if ( $method->id == 'flat_rate:13') {
    				unset( $rates[$method->id] );			
    			}
    		}
    	}
    	
    	return $rates;
    }
    add_filter( 'woocommerce_package_rates', 'my_wcs_remove_shipping_non_wholesale', 10, 2 );

    This is how I setup my shipment zones. Is that correct? So within 1 zone, 2 options for paid shipment and 2 options for free shipment. 

  6. Does it help if I post my website? Can I do it securely? OK

    1. All wholesale users have the wp_capabilities: 

    array ( 0 => array ( 'wholesale_customer' => true, 'bbp_participant' => true, ), )

    2. "no effect"???  Let me clarify as detailed as possible. I have changed 'wholesale_customer', true ); -> 'translator', true ); BUT then still GUEST (NON LOGGED IN) are NOT seeing: flat_rate:13 and flat_rate:15. The code is thus always removing both flat_rates13 and 15.

     

    *I will actually 100% donate you if we are able to fix. Or I can send a gift card for our shop. We design babyitems.

     

    Hold on, I have more info!

  7. How can I make my code work for ALL logged in users? Maybe that works.

    add_filter( 'woocommerce_package_rates', 'shipping_methods_based_on_wholesale_customer', 10, 2 );
    function shipping_methods_based_on_wholesale_customer( $rates, $package ){
        $is_wholesale = get_user_meta( get_current_user_id(), 'wholesale_customer', true );
        
        // Set the shipping methods rate ids in the arrays:
        if( $is_wholesale ) {
            $shipping_rates_ids = array('flat_rate:10', 'flat_rate:7'); // To be removed for NON Wholesale users
        } else {
            $shipping_rates_ids = array('flat_rate:13', 'flat_rate:15'); // To be removed for Wholesale users
        }
    
        // Loop through shipping rates from the current shipping package
        foreach( $rates as $rate_key => $rate ) {
            if ( in_array( $rate_key, $shipping_rates_ids) ) {
                unset( $rates[$rate_key] ); 
            }
        }
        
        return $rates;
    }

     

  8. 9 hours ago, Barand said:

    I don't think that is the case, but the method seems convoluted. If I got it right, it could be rewritten as

    function shipping_methods_based_on_wholesale_customer( $rates, $package ){
        $is_wholesale = get_user_meta( get_current_user_id(), 'wholesale_customer', true );
        
        if( $is_wholesale ) {
            unset($rates['flat_rate:10'], $rates['flat_rate:7']);  // To be removed for NON Wholesale users
        } else {
            unset($rates['flat_rate:13'], $rates['flat_rate:15']); // To be removed for Wholesale users
        }
        return $rates;
    }       

    Unfortunately, the code is not working. Now not for wholesale customers as for guest.

     

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.