Jump to content

PHP isn't targeting guest customers in Woocommerce


martini0
 Share

Recommended Posts

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;
}

 

Link to comment
Share on other sites

  • Replies 52
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

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 by ginerjm
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}       

 

Link to comment
Share on other sites

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 by maxxd
Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

Posted (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 by martini0
Link to comment
Share on other sites

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>'; )
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.