【问题标题】:restrict product page with product category 'x' WooCommerce使用产品类别“x”WooCommerce 限制产品页面
【发布时间】:2016-08-23 00:27:33
【问题描述】:

我不想限制已注销的用户或具有特定角色的用户访问某些产品页面。最简单的方法可能是检查产品页面的类别 ID,如果 current_user_can('') 则重定向到主商店页面。

但是我真的不知道从哪里开始.. 我应该在init 上添加一个操作吗?以及如何检查当前页面的产品 ID?

我以为我可以通过var_dump() 获得一些数据,但结果一无所获。 我这样做了:

add_action('init', 'get_all_post_meta');

function get_all_post_meta() {
    //$meta = get_post_meta( get_the_ID() );
    global $post;
    var_dump('$post');
    $metavar = get_the_terms($post->ID);
    var_dump('$metavar');

}

但在我的控制台中没有结果。

编辑:我发现我的var_dump() 不正确,应该是var_dump($post); 现在继续我的任务。

【问题讨论】:

    标签: php wordpress woocommerce restriction


    【解决方案1】:

    这是我目前的解决方案,现在我需要弄清楚如何删除单个产品而不是全部。

    add_action('wp_head', 'get_all_post_meta', 1 );
    
    function get_all_post_meta() {
    
        global $post;
        $banned_cid = array(8);
        $current_cid = array();
        $metavar = get_the_terms($post->ID, 'product_cat');
        global $woocommerce;
        $redirect_url = 'http://www.example.nl/';
    
    
        if(current_user_can('subscriber') || current_user_can('manage_options')){}else if( is_product()){
            foreach ( $metavar as $term ) {
                            $cat_id .= $term->term_id.',';
                            array_push($current_cid, $term->term_id);
                        }
            var_dump($current_cid);
    
            $c = array_intersect($banned_cid, $current_cid);
                if (count($c) > 0) {    
                    $woocommerce->cart->empty_cart(); 
                    wp_redirect( $redirect_url, 302 );
                    exit;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 2018-07-23
      • 1970-01-01
      • 2019-06-22
      • 2014-04-06
      • 1970-01-01
      • 2014-11-11
      相关资源
      最近更新 更多