Automatically Redirect From WooCommerce Products to Shopify Collections

Sometimes you need to redirect from Product pages to Collection pages.

We recently did an ecommerce website migration from WooCommerce to Shopify.

The old WooCommerce website had thousands of products, with many product categories.

On the new Shopify website, they had a few hundred products, with the same product Collections (product categories are called ‘Collections’ in Shopify).

So we decided to create “301 Redirects” from WooCommerce product pages to Shopify Collection pages.

We created a redirect script with this logic:

  1. On product page, get product’s category
  2. Find product category ‘slug’ (which is the short url of the category)
  3. Redirect to new website using category slug

This assumes that the url of Product Categories on old WooCommerce website is the same as the ‘slug’ of the Collection on the new Shopify website.

We added this code to the ‘header.php’ file on the WordPress WooCommerce site:


//setup redirect for product pages - to relevant category page on new website
if ( is_product() ){

global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
    $product_cat_id = $term->term_id;

function woocommerceCategorySlug( $id ){
    $term = get_term_by('id', $id, 'product_cat', 'ARRAY_A');
    return $term['slug'];       

$categoryurl = woocommerceCategorySlug($product_cat_id);

wp_redirect( '' . $categoryurl, 301 ); exit;



Replace “” with your new website domain name.

Related Posts