0

我正在使用 Woocommerce 和 Dokan 插件建立一个多供应商电子商务网站。我和其他供应商只会在网站上出售农产品。

但是,我在调整事物以满足我的需求时遇到了困难。

  • 每当我创建新产品时,我想添加一个仅对我可用(其他供应商不可用/不可见)的私有产品类别。例如,当我(作为网站管理员)创建新产品时,我可以看到/将产品分配给私有类别。但是当其他供应商正在创建新产品时,他们将无法看到/选择私有类别。

但是,在所有情况下,管理员和供应商添加的产品都应该在网站上可见。

有人可以帮我做上述吗?

请参阅下面的图片以获得清晰的解释。

提前致谢

我正在使用woocommerce 版本 4.2.0Dokan 版本 3.0.5

图 1:创建新产品时的管理仪表板 创建新产品时的管理仪表板

图 2:创建新产品时的供应商仪表板 创建新产品时的供应商仪表板

4

1 回答 1

0

对的,这是可能的:

使用 pre_get_posts WordPress 钩子,它在创建查询变量对象之后但在实际查询运行之前调用。所以它非常适合这种情况。我们在这里假设“管理员”类别的 ID 是“123”。

这是自定义代码:

function administrator_role_cat( $query ) {

    // Get the current user
    $current_user = wp_get_current_user();

    if ( $query->is_main_query() ) {
        // Displaying only "administrator" category products to "administrator" user role
        if ( in_array( 'administrator', $current_user->roles ) ) {
            // Set here the ID for administrator category 
            $query->set( 'cat', '123' ); 

        // Displaying All products (except "administrator" category products) 
        // to all other users roles (except "administrator" user role)
        // and to non logged user.
        } else {
            // Set here the ID for administrator category (with minus sign before)
            $query->set( 'cat', '-123' ); // negative number
        }
    }
}
add_action( 'pre_get_posts', 'administrator_role_cat' );
于 2020-06-27T17:48:48.590 回答