martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 (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 August 5, 2022 by martini0 Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599002 Share on other sites More sharing options...
martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 (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 August 5, 2022 by martini0 Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599003 Share on other sites More sharing options...
Barand Posted August 5, 2022 Share Posted August 5, 2022 It looks like get_user_meta() alway returns "false". You need to have a close look at that function's code to determine why. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599004 Share on other sites More sharing options...
martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 $is_wholesale = get_user_meta( 'A_USER_ID', 'wholesale_customer', true ); -> I tried, no effect. Could you rewrite the code to target ALL logged in user? So not only wholesale_customers. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599005 Share on other sites More sharing options...
dodgeitorelse3 Posted August 5, 2022 Share Posted August 5, 2022 (edited) 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 August 5, 2022 by dodgeitorelse3 Typo Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599006 Share on other sites More sharing options...
martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 Not the actual ID. Good point. Let me try with an Actual ID Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599007 Share on other sites More sharing options...
martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 $is_wholesale = get_user_meta( 'user_id=153', 'wholesale_customer', true ); is this correct? Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599008 Share on other sites More sharing options...
Barand Posted August 5, 2022 Share Posted August 5, 2022 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? Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599009 Share on other sites More sharing options...
Barand Posted August 5, 2022 Share Posted August 5, 2022 1 minute ago, martini0 said: $is_wholesale = get_user_meta( 'user_id=153', 'wholesale_customer', true ); is this correct? No. $is_wholesale = get_user_meta( 153, 'wholesale_customer', true ); Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599010 Share on other sites More sharing options...
maxxd Posted August 5, 2022 Share Posted August 5, 2022 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? Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599012 Share on other sites More sharing options...
martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 (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 August 5, 2022 by martini0 Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599013 Share on other sites More sharing options...
martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599014 Share on other sites More sharing options...
Barand Posted August 5, 2022 Share Posted August 5, 2022 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; } Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599015 Share on other sites More sharing options...
martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 (edited) Tried it, but not working. It isn't removing any shipment options for both guest as for logged in users. Edited August 5, 2022 by martini0 Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599016 Share on other sites More sharing options...
Barand Posted August 5, 2022 Share Posted August 5, 2022 As requested some time ago... 4 hours ago, Barand said: Can you show us the contents of your $rates array ( echo '<pre>' . print_r($rates, 1) . '</pre>'; ) ...and still waiting. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599017 Share on other sites More sharing options...
martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 Yes, uhm how do I find that. Sorry 🙂 Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599018 Share on other sites More sharing options...
Barand Posted August 5, 2022 Share Posted August 5, 2022 With the code I've given you to output the $rates array 8 minutes ago, Barand said: 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/page/2/#findComment-1599019 Share on other sites More sharing options...
martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 I have placed the code in functions.php. And saved. All works. So now where can I find that contents of your $rates array? Is that within Directadmin. Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599020 Share on other sites More sharing options...
Barand Posted August 5, 2022 Share Posted August 5, 2022 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; } Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599021 Share on other sites More sharing options...
martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 (edited) I must be super annoying, I am so sorry. The code is in my functions.php. It is saved and works. Now where do I find that output? Edited August 5, 2022 by martini0 Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599022 Share on other sites More sharing options...
Barand Posted August 5, 2022 Share Posted August 5, 2022 I was hoping it would be sent to your screen Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599023 Share on other sites More sharing options...
Barand Posted August 5, 2022 Share Posted August 5, 2022 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; } Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599024 Share on other sites More sharing options...
Barand Posted August 5, 2022 Share Posted August 5, 2022 Did placing that new line in the function create the file? Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599025 Share on other sites More sharing options...
martini0 Posted August 5, 2022 Author Share Posted August 5, 2022 (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 August 5, 2022 by martini0 Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599026 Share on other sites More sharing options...
Barand Posted August 5, 2022 Share Posted August 5, 2022 1 hour ago, Barand said: (The file "rates_array_content.txt" should be created in the same folder as your script) Quote Link to comment https://forums.phpfreaks.com/topic/315135-php-isnt-targeting-guest-customers-in-woocommerce/page/2/#findComment-1599029 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.