↳ Notes

Changing a product category link in WooCommerce

If you have an e-shop running on WooCommerce, you may also use some product categories. You can have the categories listed somewhere, including thumbnail images, and thus make the buying journey easier for a potential customer. On the www.kytkyodpotoka.cz e-shop there’s this list of categories on the homepage.

WooCommerce product categories

Over time a separate page svatby.kytkyodpotoka.cz was created for Weddings, so it was no longer desirable for the link to lead to the Weddings product category.

WooCommerce hooks for the product category link

Rendering the HTML tag for the product category link is handled by this function.

function woocommerce_template_loop_category_link_open($category) {
    $category_term = get_term($category, 'product_cat');
    $category_name = (!$category_term || is_wp_error($category_term)) ? '' : $category_term->name;
    /* translators: %s: Category name */
    echo '<a aria-label="' . sprintf(esc_attr__('Visit product category %1$s', 'woocommerce'), esc_attr($category_name)) . '" href="' . esc_url(get_term_link($category, 'product_cat')) . '">';
}

It’s hooked to this action.

/**
 * The woocommerce_before_subcategory hook.
 *
 * @hooked woocommerce_template_loop_category_link_open - 10
 */
do_action('woocommerce_before_subcategory', $category);

Unfortunately, the woocommerce_template_loop_category_link_open function doesn’t contain any filter for manipulating the URL. So the solution was to remove this function, modify it, and add it back. You can see the full code that handles changing the URL in the link below.

remove_action('woocommerce_before_subcategory', 'woocommerce_template_loop_category_link_open', 10);
add_action('woocommerce_before_subcategory', 'lh_woocommerce_template_loop_category_link_open', 10, 1);

function lh_woocommerce_template_loop_category_link_open($category) {
    $category_term = get_term($category, 'product_cat');
    $category_name = (!$category_term || is_wp_error($category_term)) ? '' : $category_term->name;
    $link = get_term_link($category, 'product_cat');
    $target = '';
    if ($category->slug == 'svatby') {
        $link = 'https://svatby.kytkyodpotoka.cz/';
        $target = '_blank';
    }

    echo '<a target="' . $target . '" aria-label="' . sprintf(esc_attr__('Visit product category %1$s', 'woocommerce'), esc_attr($category_name)) . '" href="' . esc_url($link) . '">';
}

First I remove the original function using remove_action, and then with add_action I add a new function in its place (or rather a modified original). It differs by the condition that if the category is svatby, the URL changes to https://svatby.kytkyodpotoka.cz/. As a bonus, using the $target variable I make the link open in a new tab. Add the code to the functions.php file of your active child theme, or via a plugin for adding snippets to WordPress.

↳ Note written by

Need to tweak your website?

I'm not a fan of dozens of plugins that slow a website down. Wherever I can, I solve it with clean code - without unnecessary extra weight.

Discuss a website edit