连接到 WooCommerce 过滤器

Hooking into a WooCommerce filter

由于最近的 WooCommerce 更新,具有 'shopmanager' 角色的用户不再能够编辑具有 'subscriber' 角色的用户。

我发现以下函数对此负责:

function wc_modify_editable_roles( $roles ) {
  if ( is_multisite() && is_super_admin() ) {
    return $roles;
  }
  if ( ! wc_current_user_has_role( 'administrator' ) ) {
    unset( $roles['administrator'] );
    if ( wc_current_user_has_role( 'shop_manager' ) ) {
      $shop_manager_editable_roles = apply_filters( 'woocommerce_shop_manager_editable_roles', array( 'customer' ) );
      return array_intersect_key( $roles, array_flip( $shop_manager_editable_roles ) );
    }
  }
  return $roles;
}
add_filter( 'editable_roles', 'wc_modify_editable_roles' );

我需要将 subscriber 添加到 apply_filters( 'woocommerce_shop_manager_editable_roles', array( 'customer' ) ); 中的数组,但这就是我卡住的地方。

如何连接到该过滤器以添加额外的角色?

这就是我到目前为止所得到的(根本不起作用,但这是一个开始:)

add_filter( 'woocommerce_shop_manager_editable_roles', 'addanotherrole' );

function addanotherrole() {
  $shop_manager_editable_roles = array( 'customer', 'subscriber' );
}

已排序!

您需要像这样 return 新角色数组:

add_filter( 'woocommerce_shop_manager_editable_roles', 'addanotherrole' );
function addanotherrole($roles) {
    // add the additional role to the woocommerce allowed roles (customer)
    $roles[] = 'subscriber'; 

    // return roles array
    return $roles; 

希望对您有所帮助!