7
votes

Im trying to disable "shop" page in Woocommerce. Basically im creating a shop theme to sell prints and image downloads for a photographer.

Because i need to create private galleries i created a custom post type where i use the woocommerce category shortcode to show products and then i password protect the post type.

This is a workaround for password protecting the woocommerce categories (if someone knows a better one please explain).

The problem is that is someone goes to /shop they will all products, including the "protected ones". So i need to disable the shop page and i need to do it programmatically on my theme functions. Any thoughts?

9

9 Answers

25
votes

To disable the shop page, copy over the archive-product.php file from the /wp-content/plugins/woocommerce/templates/archive-product.php and put in /wp-content/themes/{Your Theme}/woocommerce/archive-product.php

Open up the file and overwrite everything in file with the following code below:

<?php
global $wp_query;

$wp_query->set_404();
status_header(404);

get_template_part('404');

Save the file, and now your Shop page is gone and replaced with a 404 Page!

21
votes

Add this to functions:

function woocommerce_disable_shop_page() {
    global $post;
    if (is_shop()):
    global $wp_query;
    $wp_query->set_404();
    status_header(404);
    endif;
}
add_action( 'wp', 'woocommerce_disable_shop_page' );

Docs: WooCommerce Conditional Functions Documentation

5
votes

WooCommerce has a filter for the array that it uses to create the Product post type: woocommerce_register_post_type_product.

Rather changing the archive template to force it to redirect, you can completely remove the post type’s archive, but changing the has_archive attribute on the post type on creation.

add_filter('woocommerce_register_post_type_product', function($post_type) {
    $post_type['has_archive'] = false;
    return $post_type;
});

You should then remove the shop page in the CMS by going to WooCommerce » Settings » Product » Display, and clicking the “x” on the “Shop Page” option.

You might need to flush the permalink cache, which you can do just by clicking the “Update” button in Settings » Permalinks.

4
votes

*Edit - Apparently the page setting I suggested below no longer works. If WooCommerce doesn't have a plugin setting to change it, I personally would use a wordpress redirect plugin like Redirection. This way you can automatically redirect them from the undesired shop page to whatever page displays your products. It avoids a 404 issue and keeps everything in tact. It also avoids editing template files which adds complications to non-developers.


Old Answer:

Have you tried Woo settings?

Admin area, left main menu, Woocommerce > Settings Click the pages tab.

Under Pages setup is "Shop Base Page", on the dropdown, there's a small "x" to right right. Click that to get rid of the page.

If there are links elsewhere that need to be fixed let me know and I'll find the hooks/filters to remedy it.

3
votes

template_redirect is the last hook before page render so in my use case I ask if the page being viewed is the "shop" page and if it is I redirect to (in my case) a pricing page.

function my__template_redirect(){
    if(is_shop()){
        wp_redirect(site_url() . '/pricing/', '302');
    }
}
add_action('template_redirect', 'my__template_redirect');
1
votes

The last suggestion didn't work for me with WP 4.6.1 and WooCommerce 2.6.4. Hiding products in the Publish tab works for me.

http://paperhedge.com/hide-products-from-displaying-in-shop-page-woocommerce/

0
votes

You need to hook a couple (or maybe more) things:

/* hide category from shop pages */
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {

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

    if ( ! is_admin() ) {
        $q->set( 'tax_query', array(array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => array( YOUR-CATEGORY-SLUG, YOUR-CATEGORY-SLUG-2 ), // Don't display products in the knives category on the shop page
            'operator' => 'NOT IN'
        )));

    }

    remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

Change, obviusly YOUR-CATEGORY-SLUG, YOUR-CATEGORY-SLUG-2 for your cat or cats to hide from shop pages.

Then, you need to hide them from menues too, right?:

/* hide category from menues */
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
    $new_terms = array();

    // if category and on the shop page (change it with is_woocommerce() if want to hide from all woo pages)
    if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {

        foreach ( $terms as $key => $term ) {
          if ( ! in_array( $term->slug, array( YOUR-CATEGORY-SLUG, YOUR-CATEGORY-SLUG-2 ) ) ) {
            $new_terms[] = $term;
          }

    }
        $terms = $new_terms;
    }
  return $terms;
}

But i don´t think this solve 100% the thing, since then, what about search results?

0
votes

To disable the default shop page and leave the /shop/ slug free for custom pages use this:

function remove_woocommerce_default_shop( $args, $post_type ) {
    if (class_exists('WooCommerce')) {
        if ( $post_type == "product" ) {
            $args['has_archive'] = true;
        }
        return $args;
    }
}
add_filter('register_post_type_args', 'remove_woocommerce_default_shop', 20, 2);
-1
votes

Try this

  1. Create new page named "Shop"
  2. Go to "woocommerce" > "Settings" > "Product" > "Display tab"
  3. Select shop page named "Shop" then click save changes
  4. Back to "Pages" then delete "Shop" page (keep the page on trash, don't delete permanently)

This method works fine for me