martini0 Posted August 4, 2022 Share Posted August 4, 2022 The following code is removing 'flat_rate:13' and 'flat_rate:15' but for BOTH wholesale customers as for guests. But it should only remove for wholesale customers. Now, guests are seeing the wrong shipment costs: 'flat_rate:10' and 'flat_rate:7' What is wrong? As I am loosing my mind because so.. 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; } Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/ Share on other sites More sharing options...
martini0 Posted August 4, 2022 Author Share Posted August 4, 2022 $is_wholesale is always returning true, but why? wholesale_user is a valid user type. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598954 Share on other sites More sharing options...
ginerjm Posted August 4, 2022 Share Posted August 4, 2022 (edited) Since you seem to be questioning the determining of the is_wholesale value, perhaps you should show us that function? PS Since there are only a couple of rates to be dropped why bother with a loop to unset them. Just do the unset instead of setting up an array to loop thru. Can you unset a variable defined outside of the function from inside of that function? ****************** My Bad. Didn't notice the return of the rates array back to the caller. So definitely you s/b examining that other function. Edited August 4, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598956 Share on other sites More sharing options...
martini0 Posted August 4, 2022 Author Share Posted August 4, 2022 Show you that function? Do you mean another code? I got the code from here. Do I also need to add other code then the one mentioned above? I am no programmer, so not that experienced. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598961 Share on other sites More sharing options...
maxxd Posted August 4, 2022 Share Posted August 4, 2022 wholesale_user might be a valid user type, but your code is checking for wholesale_customer and returning true if it's not found. In addition, your conditional is backwards - the comment says flat_rate:7 and flat_rate:10 are to be removed for non wholesale customers, but shipping_rates_ids is being populated with those values when $is_wholesale is true. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598962 Share on other sites More sharing options...
ginerjm Posted August 4, 2022 Share Posted August 4, 2022 Good guess on that function's actions. I didn't even try to work out what that was doing. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598963 Share on other sites More sharing options...
martini0 Posted August 4, 2022 Author Share Posted August 4, 2022 Could you please update the code so that it is working? I have lost 2 days trying things, as I am not a programmer. You would honestly, not joking, be my hero. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598964 Share on other sites More sharing options...
ginerjm Posted August 4, 2022 Share Posted August 4, 2022 If you are not a programmer why oh why are you doing this? Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598965 Share on other sites More sharing options...
martini0 Posted August 4, 2022 Author Share Posted August 4, 2022 Because I try not to be a quitter.. 🙂 I am a small business owner and design babyitems. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598966 Share on other sites More sharing options...
ginerjm Posted August 4, 2022 Share Posted August 4, 2022 We could help you (and probably someone will) but that could end up giving you encouragement to make a mess out of your next 'programming' problem and then where would you be? Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598967 Share on other sites More sharing options...
martini0 Posted August 4, 2022 Author Share Posted August 4, 2022 I know what to do when the code breaks.. and I always test the code for issues on a PHP code tester. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598968 Share on other sites More sharing options...
martini0 Posted August 4, 2022 Author Share Posted August 4, 2022 But with PHP I am very carefull actually. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598969 Share on other sites More sharing options...
ginerjm Posted August 4, 2022 Share Posted August 4, 2022 Well as maxxd told you you have your if test backwards, assuming that he is interpreting what that other function is doing correctly. Try changing that php code and see if you're happier. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598970 Share on other sites More sharing options...
Barand Posted August 4, 2022 Share Posted August 4, 2022 27 minutes ago, maxxd said: In addition, your conditional is backwards 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; } Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598971 Share on other sites More sharing options...
martini0 Posted August 4, 2022 Author Share Posted August 4, 2022 (edited) Oh wait, thanks! Let me try that first thing in the morning 🙂 thanks a lot. Edited August 4, 2022 by martini0 Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598972 Share on other sites More sharing options...
maxxd Posted August 4, 2022 Share Posted August 4, 2022 (edited) 1 hour ago, Barand said: 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 } Unless $is_wholesale is true for customers that aren't wholesale customers, the logic still seems backwards to me. Though admittedly, it's been a long week and I may be mis-reading it. Edited August 4, 2022 by maxxd Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598975 Share on other sites More sharing options...
maxxd Posted August 4, 2022 Share Posted August 4, 2022 I don't recall WooCommerce having a wholesale/retail distinction out of the box; is this a plugin or am I remembering incorrectly? Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598977 Share on other sites More sharing options...
martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598990 Share on other sites More sharing options...
martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 (edited) 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; } Edited August 5, 2022 by martini0 Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598991 Share on other sites More sharing options...
Barand Posted August 5, 2022 Share Posted August 5, 2022 Some questions for you Why is $package being passed to the function when it isn't used? Have you checked that get_current_user_id() is doing what its name suggests? Have you tested get_user_meta() with different existing ids to see if returns the correct is_wholesale value for them? Can you show us the contents of your $rates array ( echo '<pre>' . print_r($rates, 1) . '</pre>'; ) Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598994 Share on other sites More sharing options...
martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 1. I just copied the code from stackoverflow and edited with my own flat rate information 2. Im not a programmer, I dont know how to do... 3. I have just now tested it with the ID: 'translator' and no effect. Also I have removed all other PHP and tested. No effect. 4. I don't know how... Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598997 Share on other sites More sharing options...
martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 Could you help me re write it to target logged in users? Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598998 Share on other sites More sharing options...
Barand Posted August 5, 2022 Share Posted August 5, 2022 6 minutes ago, martini0 said: 2. Im not a programmer, I dont know how to do... Log in as different users and echo get_current_user_id() to see if outputs the correct ids for the logged in users * * * 8 minutes ago, martini0 said: I have just now tested it with the ID: 'translator' and no effect. "no effect"??? Are you saying that no matter what id you specify, it always returns the same value? If that is the case then it would appear your problem is with the get_user_meta() function. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1598999 Share on other sites More sharing options...
Barand Posted August 5, 2022 Share Posted August 5, 2022 2 minutes ago, martini0 said: Could you help me re write it to target logged in users? Instead of $is_wholesale = get_user_meta( get_current_user_id(), 'wholesale_customer', true ); hard code the id values instead of "get_current_user_id()", so you use $is_wholesale = get_user_meta( 'A_USER_ID', 'wholesale_customer', true ); Test it substituting different ids. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1599000 Share on other sites More sharing options...
Barand Posted August 5, 2022 Share Posted August 5, 2022 16 hours ago, martini0 said: $is_wholesale is always returning true, but why? Of course, it could be that the code is correct but the data says all your user are "wholesale" Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/#findComment-1599001 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.