Jump to content

PHP isn't targeting guest customers in Woocommerce


martini0
 Share

Recommended Posts

Posted (edited)

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!

Edited by martini0
Link to comment
Share on other sites

  • Replies 52
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted (edited)

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. 

Edited by martini0
Link to comment
Share on other sites

17 minutes ago, martini0 said:

$is_wholesale = get_user_meta( 'A_USER_ID', 'wholesale_customer', true );  -> I tried, no effect

Did you try that code as you show here or did you replace A_USER_ID with an actual ID such as 1 or 2?

Edited by dodgeitorelse3
Typo
Link to comment
Share on other sites

23 minutes ago, martini0 said:

Could you rewrite the code to target ALL logged in user? So not only wholesale_customers.

As the purpose of the function is to return "true" if the specified user is a wholesale customer, how do see that working?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Posted (edited)

$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?

Edited by martini0
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Something like this, maybe?

function shipping_methods_based_on_wholesale_customer( $rates, $package ){
    
    if( ! get_current_user_id() ) {
        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;
}       

 

Link to comment
Share on other sites

It's your $rates array - the one you are using in the function that's being discussed all through this topic. Put the code I gave inside the function and post the out put to us.

function shipping_methods_based_on_wholesale_customer( $rates, $package ){

    echo '<pre>' . print_r($rates, 1) . '</pre>';                                                         // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    
    if( ! get_current_user_id() ) {
        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;
}       

 

Link to comment
Share on other sites

Try outputting to a file then. (The file "rates_array_content.txt" should be created in the same folder as your script)

function shipping_methods_based_on_wholesale_customer( $rates, $package ){
    
    file_put_contents('rates_array_content.txt', print_r($rates, 1));
    
    if( ! get_current_user_id() ) {
        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;
}  

 

Link to comment
Share on other sites

Posted (edited)

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.

Edited by martini0
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share


×
×
  • 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.