【问题标题】:Woocommerce how to get categories ids of a product in the admin post edit pageWoocommerce 如何在管理帖子编辑页面中获取产品的类别 ID
【发布时间】:2022-01-20 14:06:38
【问题描述】:

我正在尝试限制特定类别的产品针对特定用户角色进行编辑。知道如何在...wp-admin/post.php?post=1702&action=edit 中获取产品的类别 ID 吗?

到目前为止,这是我的代码:

function restrict_edit_if_in_manufacturing_cat($post){
    global $pagenow, $post_type;

    // Targeting admin edit single product screen
    if ($pagenow === 'post.php' && $post_type === 'product') {

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

        // Roles
        $roles = (array) $user->roles;

        // Roles to check
        $roles_to_check = array('shop_manager'); // several can be added, separated by a comma

        // Compare
        $compare = array_diff($roles, $roles_to_check);

        // Result is empty
        if (empty($compare)) {

            // $category_ids = ...Get Category IDs here...

            // If 458 is in the category id list die
            if (strpos($category_ids, '458') !== false) {

                wp_die('cheatn');
            } else {

                // do something if needed

            }
        }
    }
}
add_action('pre_get_posts', 'restrict_edit_if_in_manufacturing_cat', 10, 1);

【问题讨论】:

    标签: php wordpress woocommerce wordpress-admin


    【解决方案1】:

    您可以使用get_the_terms 函数获取当前帖子的类别,然后获取它们的 ID,如下所示:

    add_action('pre_get_posts', 'restrict_edit_if_in_manufacturing_cat');
    
    function restrict_edit_if_in_manufacturing_cat($post)
    {
    
        global $pagenow, $post_type;
    
        if ($pagenow === 'post.php' && $post_type === 'product') 
        {
    
            $current_user = wp_get_current_user();
    
            $user_roles = $current_user->roles;
    
            if (in_array("shop_manager", $user_roles)) 
            {
    
                $categories_for_the_current_post = get_the_terms($_GET['post'], 'product_cat');
    
                $categories_ids = array();
                foreach ($categories_for_the_current_post as $category_object) {
                    array_push($categories_ids, $category_object->term_id);
                }
    
                if (in_array('458', $categories_ids)) 
                {
    
                    wp_die('cheatn');
    
                } else {
    
                    // do something if needed
                }
            }
        }
    }
    

    此 sn-p 已在标准 woo 安装 5.7.1 上进行了测试,并且工作正常。

    【讨论】:

    • 这不是我要求的 - 也许我不够清楚 - 如果当前产品属于类别 458...不是如果产品 ID 是 458。并且 get_the_category 没有返回任何内容...谢谢
    • 刚刚更新了我的答案,请再试一次!
    • 不起作用 - 以任何方式回显类别 ID,以便我可以检查它们
    • 嗯,我刚刚在我的 woo 安装上进行了测试,它在我的终端上运行良好。是的,您可以在此行上方使用 die(print_r($categories_ids)); 来检查 ID:if (in_array('458', $categories_ids))
    • 如果您使用管理员帐户进行检查,则需要将 if (in_array("shop_manager", $user_roles)) 更改为 if (in_array("administrator", $user_roles))
    猜你喜欢
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 2021-02-07
    • 2013-02-24
    • 2020-02-15
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    相关资源
    最近更新 更多