Home > OS >  Check user for logged in in Woocommerce
Check user for logged in in Woocommerce

Time:08-07

I am trying to rewrite the following in order to check for logged in user (instead of wholesale customers). I have the following, who can help me edit it so that it works? The original code can be found here.

add_filter( 'woocommerce_package_rates', 'shipping_methods_based_on_logged_in', 10, 2 );
function shipping_methods_based_on_logged_in( $rates, $package ){
    $usermeta = get_user_meta( get_current_user_id() ,'wp_capabilities' ,true );
  
    
    // Set the shipping methods rate ids in the arrays:
    if ($login->isUserLoggedIn() == 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) ) {
            unset( $rates[$rate_key] ); 
        }
    }
    
    return $rates;
}

CodePudding user response:

You need to check if is_user_logged_in()

add_filter( 'woocommerce_package_rates', 'shipping_methods_based_on_wholesale_customer', 10, 2 );
function shipping_methods_based_on_wholesale_customer( $rates, $package ) {
    // Set the shipping methods rate ids in the arrays.
    if ( ! is_user_logged_in() ) { // If user is NOT logged in.
        $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;
}
  • Related