martini0
-
Posts
26 -
Joined
-
Last visited
Posts posted by martini0
-
-
I am working in WordPress. Please be more specific as I don't follow.
-
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.
-
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?
-
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.
-
Yes, uhm how do I find that. Sorry 🙂
-
Tried it, but not working. It isn't removing any shipment options for both guest as for logged in users.
-
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
-
$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?
-
$is_wholesale = get_user_meta( 'user_id=153', 'wholesale_customer', true );
is this correct?
-
Not the actual ID. Good point. Let me try with an Actual ID
-
$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.
-
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.
-
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!
-
Could you help me re write it to target logged in users?
-
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...
-
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; }
-
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.
-
Oh wait, thanks! Let me try that first thing in the morning 🙂 thanks a lot.
-
But with PHP I am very carefull actually.
-
I know what to do when the code breaks.. and I always test the code for issues on a PHP code tester.
-
Because I try not to be a quitter.. 🙂 I am a small business owner and design babyitems.
-
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.
-
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.
-
$is_wholesale is always returning true, but why? wholesale_user is a valid user type.
PHP isn't targeting guest customers in Woocommerce
in PHP Coding Help
Posted
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; }