简体   繁体   中英

Show WooCommerce product pages for only specific category

I have a client with a website where customers can order product, and sales people can order sales samples. I want to be able to show individual product pages for a single category, but hide individual product pages for all other products.

I have used this code in the past to hide all product pages, but I need to filter by category. Any hints?

//Remove all single product pages
function hide_product_page($args){
    $args["publicly_queryable"]=false;
    $args["public"]=false;
    return $args;
}
add_filter( 'woocommerce_register_post_type_product','hide_product_page',12,1); 

EDIT: Here's the background: the sales sample products are free, and we have a single order page for those that is accessible by password (not individual product pages.) Even though category pages were prevented from showing, the individual product pages still existed. Some random people found those pages and placed orders for "free" product. I need to prevent that from happening, so it's not enough to just "hide" the individual product pages, I must ensure they do not exist. However, we still need product pages for the regular products that are for sale to the public.

EDIT: I ended up using this in my functions.php:

function custom_shop_page_redirect(){
    if (class_exists('WooCommerce')){
        if(is_product()){
            global $post;
            $price = get_post_meta( $post->ID, '_regular_price', true);

            if($price == 0) {
                wp_redirect(home_url());
                exit();
            }
        }
    } 
    return;
} 
add_action('template_redirect','custom_shop_page_redirect');

It does not check the category, but rather disables product pages for items that have a price of zero. This accomplishes what I need.

Let's assume you have a category with a slug like pizza . You can hide products in this category and prevent the category from showing up in the shop page (if you enabled showing categories before products) in WooCommerce like this. (Include this somewhere in your child theme or your theme's funtion.php .)

$CATEGORY_SLUGS = array('pizza'); // Can contain multiple slugs.

// Prevents category from showing up.
function get_terms_exclude_by_category_slug($terms, $taxonomies, $args) {
  global $CATEGORY_SLUGS;
  if (in_array('product_cat', $taxonomies)
      && !is_admin()
      && is_shop()) {
    $new_terms = array();
    foreach ($terms as $key => $term) {
      if (!in_array($term->slug, $CATEGORY_SLUGS)) {
        $new_terms[] = $term;
      }
    }
    $terms = $new_terms;
  }
  return $terms;
}

// Prevents products in certain categories from showing up.
function exclude_products_by_category_slug($q) {
  global $CATEGORY_SLUGS;
  $tax_query = (array) $q->get('tax_query');
  $tax_query[] = array(
    'taxonomy' => 'product_cat',
    'field' => 'slug',
    'terms' => $CATEGORY_SLUGS,
    'operator' => 'NOT IN'
  );
  $q->set('tax_query', $tax_query);
}

// The last two parameters are needed only because the callback
// receives 3 arguments instead of the default 1.
add_filter('get_terms', 'get_terms_exclude_by_category_slug', 10, 3);
add_action('woocommerce_product_query', 'exclude_products_by_category_slug');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM