如何允许角色编辑器管理 wordpress 中的 woocommerce 分类法?

how to allow a role editor to manage woocommerce taxonomies in worpdress?

我希望编辑角色能够访问所有 woocommerce 管理,我通过向该角色添加功能来实现:

    $role = get_role( 'editor' );
    $role->add_cap( 'manage_woocommerce_products' );
    $role->add_cap( 'manage_woocommerce_taxonomies' );
    $role->add_cap( 'manage_woocommerce_orders' );
    $role->add_cap( 'manage_woocommerce' );
    $role->add_cap( 'view_woocommerce_reports' );
    $role->add_cap( 'manage_woocommerce_coupons' );

    $role->add_cap( 'edit_product' );
    $role->add_cap( 'read_product' );
    $role->add_cap( 'delete_product' );
    $role->add_cap( 'edit_products' );
    $role->add_cap( 'publish_products' );
    $role->add_cap( 'read_private_products' );
    $role->add_cap( 'delete_products' );
    $role->add_cap( 'delete_private_products' );
    $role->add_cap( 'delete_published_products' );
    $role->add_cap( 'edit_private_products' );
    $role->add_cap( 'edit_published_products' );
    $role->add_cap( 'edit_products' );

除了产品类别和标签外,一切似乎都正常,我一直在搜索但没有,我想必须有一个功能,但我不知道是哪个,希望有专家能指导我关于这个。

非常感谢。

如果你阅读 this, you can see that woocommerce recommend two plugins to work, try: this or this

或尝试添加此功能

  1. manage_product_terms
  2. edit_product_terms
  3. delete_product_terms
  4. assign_product_terms
  5. manage_categories

您也可以像您尝试的那样以编程方式执行此操作,但是您必须授予更多角色才能使其正常工作。

我通过查询 Woocommerce Shop Manager 角色并将功能与编辑角色进行比较发现了这一点。

这是添加全部的代码(在撰写本文时):

    //add caps to editor role
    $role = get_role("editor");

    //for woocommerce
    $role->add_cap("manage_woocommerce");
    $role->add_cap("view_woocommerce_reports");
    $role->add_cap("edit_product");
    $role->add_cap("read_product");
    $role->add_cap("delete_product");
    $role->add_cap("edit_products");
    $role->add_cap("edit_others_products");
    $role->add_cap("publish_products");
    $role->add_cap("read_private_products");
    $role->add_cap("delete_products");
    $role->add_cap("delete_private_products");
    $role->add_cap("delete_published_products");
    $role->add_cap("delete_others_products");
    $role->add_cap("edit_private_products");
    $role->add_cap("edit_published_products");
    $role->add_cap("manage_product_terms");
    $role->add_cap("edit_product_terms");
    $role->add_cap("delete_product_terms");
    $role->add_cap("assign_product_terms");
    $role->add_cap("edit_shop_order");
    $role->add_cap("read_shop_order");
    $role->add_cap("delete_shop_order");
    $role->add_cap("edit_shop_orders");
    $role->add_cap("edit_others_shop_orders");
    $role->add_cap("publish_shop_orders");
    $role->add_cap("read_private_shop_orders");
    $role->add_cap("delete_shop_orders");
    $role->add_cap("delete_private_shop_orders");
    $role->add_cap("delete_published_shop_orders");
    $role->add_cap("delete_others_shop_orders");
    $role->add_cap("edit_private_shop_orders");
    $role->add_cap("edit_published_shop_orders");
    $role->add_cap("manage_shop_order_terms");
    $role->add_cap("edit_shop_order_terms");
    $role->add_cap("delete_shop_order_terms");
    $role->add_cap("assign_shop_order_terms");
    $role->add_cap("edit_shop_coupon");
    $role->add_cap("read_shop_coupon");
    $role->add_cap("delete_shop_coupon");
    $role->add_cap("edit_shop_coupons");
    $role->add_cap("edit_others_shop_coupons");
    $role->add_cap("publish_shop_coupons");
    $role->add_cap("read_private_shop_coupons");
    $role->add_cap("delete_shop_coupons");
    $role->add_cap("delete_private_shop_coupons");
    $role->add_cap("delete_published_shop_coupons");
    $role->add_cap("delete_others_shop_coupons");
    $role->add_cap("edit_private_shop_coupons");
    $role->add_cap("edit_published_shop_coupons");
    $role->add_cap("manage_shop_coupon_terms");
    $role->add_cap("edit_shop_coupon_terms");
    $role->add_cap("delete_shop_coupon_terms");
    $role->add_cap("assign_shop_coupon_terms");
    $role->add_cap("edit_shop_webhook");
    $role->add_cap("read_shop_webhook");
    $role->add_cap("delete_shop_webhook");
    $role->add_cap("edit_shop_webhooks");
    $role->add_cap("edit_others_shop_webhooks");
    $role->add_cap("publish_shop_webhooks");
    $role->add_cap("read_private_shop_webhooks");
    $role->add_cap("delete_shop_webhooks");
    $role->add_cap("delete_private_shop_webhooks");
    $role->add_cap("delete_published_shop_webhooks");
    $role->add_cap("delete_others_shop_webhooks");
    $role->add_cap("edit_private_shop_webhooks");
    $role->add_cap("edit_published_shop_webhooks");
    $role->add_cap("manage_shop_webhook_terms");
    $role->add_cap("edit_shop_webhook_terms");
    $role->add_cap("delete_shop_webhook_terms");
    $role->add_cap("assign_shop_webhook_terms");

请注意,这将为所有编辑器添加 woocommerce 中的完整功能。

要检查您是否不需要其他角色,请将这些角色与授予商店经理角色的能力进行比较,如下所示:

    $role = get_role("shop_manager");
    print_r($role->capabilities);

抱歉,我来晚了。我想分享这个用于调试用户角色的片段,它对我帮助很大。

add_action( 'admin_notices', 'debug_user_roles' );
function debug_user_roles() {
  global $pagenow;
  if( $pagenow == 'index.php' ) {
    $MYrole = get_role("seo_specialist");
    echo '<pre>';
    print_r($MYrole->capabilities);
    echo '</pre>';

    $MY_other_role = get_role("shop_manager");
    echo '<pre>';
    print_r($MY_other_role->capabilities);
    echo '</pre>';

  }
}

这将在 wp-dashboard 上显示用户角色的能力,如果需要

,您还可以添加 if current_user_can('administrator') 之类的内容