我需要在woocommerce中创建一些客户特定的产品,这些产品只应向特定用户展示

时间:2016-01-28 15:50:04

标签: wordpress woocommerce

我正在使用WordPress和WooCommerce开发电子商务商店。

我有一个产品。

我只允许特定客户购买该产品。

因此,如果特定客户已登录,我只想展示此产品。

由于

2 个答案:

答案 0 :(得分:0)

这对限制用户很有用,

// Woocommerce - Redirect unauthorised users from accessing a specified product category when clicked or visited via direct url

function woocommerce_hide_non_registered() {
 if( ( is_product_category('specials') ) && ! ( current_user_can( 'customer' ) || current_user_can( 'administrator' ) ) ) {

 wp_redirect( site_url( '/' ) );

 exit();
 }
}
add_action( 'template_redirect','woocommerce_hide_non_registered' );

// End - Woocommerce - redirect unauthorised users from accessing a specified product category

// Woocommerce - Removes category link from woocommerce product category widgets so they are not seen

add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );

function get_subcategory_terms( $terms, $taxonomies, $args ) {

  $new_terms = array();

  // if a product category and on the shop page
  if ( in_array( 'product_cat', $taxonomies ) && ! ( current_user_can( 'customer' ) || current_user_can( 'administrator' ) ) && is_shop() ) {

    foreach ( $terms as $key => $term ) {

      if ( ! in_array( $term->slug, array( 'specials' ) ) ) {
        $new_terms[] = $term;
      }

    }

    $terms = $new_terms;
  }

  return $terms;
}

// End - Woocommerce - Removes category link from woocommerce product category widgets so they are not seen

// Woocommerce - Remove products from being displayed that belong to a category user is not authorised to visit. Products seem to still be accessible via direct url unfortunately.

add_action( 'pre_get_posts', 'custom_pre_get_posts' );

function custom_pre_get_posts( $q ) {

        if ( ! $q->is_main_query() ) return;

        if ( ! $q->is_post_type_archive() ) return;

        if ( ! ( current_user_can( 'customer' ) || current_user_can( 'administrator' ) ) && is_shop() ) {

               $q->set( 'tax_query', array(array(

                   'taxonomy' => 'product_cat',

                   'field' => 'slug',

                   'terms' => array( 'specials'), // Don't display products in the private-clients category on the shop page

                   'operator' => 'NOT IN'

               )));

        }

remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' ); 

}

// End - Woocommerce - Remove products from being displayed that belong to a category user is not authorised to visit. Products seem to still be accessible via direct url unfortunately.

答案 1 :(得分:0)

这将为你解决问题。

     add_action( 'pre_get_posts', 'dm_restrict_user_to_show_own_posts_only' );

        function dm_restrict_user_to_show_own_posts_only( $dm_wp_query_obj ) 
        {
            // Front end, do nothing
            if( !is_admin() )
                return;

            global $current_user, $pagenow;
            wp_get_current_user();

            // http://php.net/manual/en/function.is-a.php
            if( !is_a( $current_user, 'WP_User') )
                return;

            // Not the correct screen, bail out
            if( 'edit.php' != $pagenow )
                return;

            // Not the correct post type, bail out
            if( 'product' != $dm_wp_query_obj->query['post_type'] )
                return;

            // If the user is not administrator, filter the post listing
            if( !current_user_can( 'delete_plugins' ) )
                $dm_wp_query_obj->set('author', $current_user->ID );
        }