Show WooCommerce product pages for only specific category

Multi tool use
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.
php wordpress woocommerce hook-woocommerce
New contributor
Duke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
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.
php wordpress woocommerce hook-woocommerce
New contributor
Duke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
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.
php wordpress woocommerce hook-woocommerce
New contributor
Duke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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.
php wordpress woocommerce hook-woocommerce
php wordpress woocommerce hook-woocommerce
New contributor
Duke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Duke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Dec 27 at 20:16
New contributor
Duke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Dec 26 at 22:40


Duke
112
112
New contributor
Duke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Duke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Duke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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');
Thanks for the answer, but it's not enough to hide the product pages, they must not exist at all.
– Duke
Dec 27 at 20:18
Fair enough. You can post your own solution as an answer to your own question and accept it. It's a good idea to do it, because others will find that your question was answered and they can clearly see/find the answer too.
– Kohányi Róbert
2 days ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Duke is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53937906%2fshow-woocommerce-product-pages-for-only-specific-category%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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');
Thanks for the answer, but it's not enough to hide the product pages, they must not exist at all.
– Duke
Dec 27 at 20:18
Fair enough. You can post your own solution as an answer to your own question and accept it. It's a good idea to do it, because others will find that your question was answered and they can clearly see/find the answer too.
– Kohányi Róbert
2 days ago
add a comment |
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');
Thanks for the answer, but it's not enough to hide the product pages, they must not exist at all.
– Duke
Dec 27 at 20:18
Fair enough. You can post your own solution as an answer to your own question and accept it. It's a good idea to do it, because others will find that your question was answered and they can clearly see/find the answer too.
– Kohányi Róbert
2 days ago
add a comment |
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');
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');
answered Dec 27 at 13:34
Kohányi Róbert
6,60023766
6,60023766
Thanks for the answer, but it's not enough to hide the product pages, they must not exist at all.
– Duke
Dec 27 at 20:18
Fair enough. You can post your own solution as an answer to your own question and accept it. It's a good idea to do it, because others will find that your question was answered and they can clearly see/find the answer too.
– Kohányi Róbert
2 days ago
add a comment |
Thanks for the answer, but it's not enough to hide the product pages, they must not exist at all.
– Duke
Dec 27 at 20:18
Fair enough. You can post your own solution as an answer to your own question and accept it. It's a good idea to do it, because others will find that your question was answered and they can clearly see/find the answer too.
– Kohányi Róbert
2 days ago
Thanks for the answer, but it's not enough to hide the product pages, they must not exist at all.
– Duke
Dec 27 at 20:18
Thanks for the answer, but it's not enough to hide the product pages, they must not exist at all.
– Duke
Dec 27 at 20:18
Fair enough. You can post your own solution as an answer to your own question and accept it. It's a good idea to do it, because others will find that your question was answered and they can clearly see/find the answer too.
– Kohányi Róbert
2 days ago
Fair enough. You can post your own solution as an answer to your own question and accept it. It's a good idea to do it, because others will find that your question was answered and they can clearly see/find the answer too.
– Kohányi Róbert
2 days ago
add a comment |
Duke is a new contributor. Be nice, and check out our Code of Conduct.
Duke is a new contributor. Be nice, and check out our Code of Conduct.
Duke is a new contributor. Be nice, and check out our Code of Conduct.
Duke is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53937906%2fshow-woocommerce-product-pages-for-only-specific-category%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
U CyTCCrdx Wu,vJWIdMfgp29XhHwAV0q W4wYv