dungey_140 dungey_140 - 1 month ago 4
PHP Question

Forwarding WooCommerce 'Shop' page to another page on a site

As part of our sitemap (using WooCommerce), we don't want to have a page at /shop/. Instead, our menu just links directly through to the shop categories. Because of this, I want to use PHP to forward /shop/ onto our primary category, which is /product-category/coffee/.

On previous Wordpress sites I've used something similar to the below, in a dedicated page template file to forward the user on. However, using the below code inside of a template name 'page-shop.php' doesn't work, and I am left with a generic 'shop overview page' showing categories and products.

Is the code wrong or am I putting it inside the wrong template?

Thanks

<?php
/* Redirect /shop/ directly to coffee category */
wp_redirect( get_bloginfo('url').'/product-category/coffee/' ); exit;
?>

Answer

Try putting the following code into your theme's functions.php file.

function custom_shop_page_redirect() {
    if( is_shop() ){
        wp_redirect( home_url( '/product-category/coffee/' ) );
        exit();
    }
}
add_action( 'template_redirect', 'custom_shop_page_redirect' );