2
votes

I need to add a few shipping methods to our WooCommerce Edit Order page.

This will be for admin-only internal use so customers should not be able to see these options.

Is there a hook or custom code that we could add to populate the shipping method dropdown.

There is no additional functionality needed (no tax, rate, etc.)

Screenshot

1
How did you get that dropdown in the order page ? I'm trying to have the shipping methods in that page and I cannot find anything about it...Sebastien

1 Answers

0
votes

I think there are more ways to achieve what you are looking for. This would be one solution:

Shipping methods for user roles

You can solve this problem with user permissions. Only users of the role "administrator" should have the ability to use the shipping method "usps".

Easy way would be to use a free plugin like:

https://wordpress.org/plugins/user-role-editor/

In this plugin, you can select your User Role and click "Add capability" and hopefully find your shipping method.

There are also solutions exactly for your issue:

https://wordpress.org/plugins/user-role-based-shipping-method/ There are also a lot of plugins which are not for free, but may offer some more comfort and support.


The harder way is to write a function and make use of woocommerce and wordpress hooks and filters. This is a little bit complex, but I will give you something to start with.

In wordpress, user roles are a set of capabilities.

You can get the role object in wordpress like this:

$administrator_role = get_role( 'administrator' );

You can add capabilties and remove them:

$administrator_role->add_cap( 'custom_capability' );
$administrator_role->remove_cap( 'custom_capability' );

Read something about this here: https://developer.wordpress.org/plugins/users/roles-and-capabilities/


And fortunately there is a woocommerce filter to make use of. Maybe you should take a look at this first, this might totally solve your issue:

https://docs.woocommerce.com/document/hide-other-shipping-methods-when-free-shipping-is-available/

As you see, the filter you want is the:

add_filter('woocommerce_package_rates', 'your_function', 30, 2);

http://hookr.io/filters/woocommerce_package_rates/

Here is a tutorial, how to make use of it:

https://trackitweb.com/woocommerce-hide-a-shipping-method-based-on-user-role/

The shipping method in the example is called "local pickup":

add_filter('woocommerce_package_rates', 'hide_local_pickup_based_on_user_role', 30, 2);

function hide_local_pickup_based_on_user_role($rates, $package) {
  foreach($rates as $rate_key => $rate){
    if($rate->method_id === 'local_pickup'){
      if(!current_user_can('local_pickup_allowed') || !is_user_logged_in()){
        unset($rates[$rate_key]);
        break;
      }
    }
  }
  return $rates;

And this is the part, where you check the role capability (and the login), to unset the shipping method for users without this capability:

if(!current_user_can('local_pickup_allowed') || !is_user_logged_in()){ ... }

I hope this can help you!