Nicola Mustone

Happiness Lead @ Automattic



Make Product Attributes Linkable

Update November 21st, 2023: I’ve tested this script with WooCommerce 8.3.0, and it’s fully functional.

In the dynamic world of e-commerce, product attributes in WooCommerce play a pivotal role. By transforming these attributes into clickable links, we open doors to a more intuitive and informative shopping experience.

Understanding Product Attributes in WooCommerce

Product attributes are the backbone of product differentiation in WooCommerce. They can be global, used across multiple products, or local, specific to individual products. Understanding this distinction is key to effective product management. You can learn more about them in the WooCommerce documentation, but here is a quick guide:

You can create Global attributes from Products > Attributes, they are taxonomies, like product categories or post tags. Any product can use an attribute defined there, this is why they are global.

You can instead define Local attributes in the product, while creating/editing it, in the tab Attributes. They are available only on the product where they have been defined and not usable by other products.

The Advantages of Linkable Product Attributes

Making attributes linkable can significantly enhance your store’s usability and SEO. It allows customers to easily navigate and find products based on specific attributes, creating a more engaging user journey.

Making Product Attributes Linkable

Tailoring your WooCommerce store to include linkable attributes involves custom coding. This approach offers unparalleled flexibility, allowing you to create a unique and user-friendly shopping environment.

Because of the difference between the two types of attributes that we outlined earlier, we need to use two different ways to add a URL to the product attribute term.

Before you make any code changes, have a backup of your site. This will help you restore things in case of unforeseen errors. I recommend using Jetpack Backup for real-time backups and one-click restores.

Open your functions.php file in wp-content/themes/your-child-theme-name/ and add this code at the end of the file:

From line 2 to line 68, the code adds a field named URL in the form for creating and editing global attributes. The functions are to register the field, add it in the Add New term form, add it in the Edit term form and save the field.

From line 70 to the end of the file, it tweaks the values in the tab Additional Information in the single product page to handle the URL of the product attribute term. This function, also handles URLs for local attributes.

How to Use The Product Attribute URL

For global attributes, go to Products > Attributes and add a new attribute, or edit an existing one. It will appear in the table on the right. Click on the cogs icon to add product attribute terms in it, you will see this form:

The Add new Color form shows the Name, Slug and Description text fields, followed by a new URL field added by the snippet from this article.

As you can see, there’s a new field at the bottom of the form Add new Color (Color is the name of the attribute I used for this example), named URL. Write there the URL you want the product attribute term to link to.

The same field is available while editing the product attribute term:

The image shows the Edit Attribute Term form for a term called Color, showing the new custom URL field.

For local attributes instead, we don’t have an interface like this, so we have to find a workaround to add URLs to product attribute terms.

Do not use this method for local attributes that are Used for Variations. The script is not meant to handle such cases. Use it only for attributes that are shown on the product page but not used for variations.

Did you know that I love Markdown? That is what we are going to use for local attributes, Markdown!

Edit a product from Products > All Products and open the Attributes tab. From here, add a new Custom product attribute. Then give it a name and add some values using Markdown to create links:

[Text shown](https://myurl.com)

This is the format to use. An example is shown in this screenshot:

The attributes form in WooCommerce showing the markdown value of an attribute

The second attribute here, Test, is the local attribute.

Now click on Save Attributes and Publish your product. This is how it will appear on the website:

The Additional Information tab on WooCommerce showing the local and global attributes as links.

Enhancing Your Store with Linkable Attributes

Linkable product attributes not only enhance the functionality of your WooCommerce store but also enrich the customer experience. They are a testament to the flexibility and adaptability of WooCommerce as an e-commerce platform.

Have you tried making your product attributes linkable? Share your experiences and tips in the comments below.


Subscribe to This Blog

Receive new articles from this blog directly in your inbox! No spam guaranteed!

Join 673 other subscribers

62 responses to “Make Product Attributes Linkable”

  1. Hello Nicola, your code is great and very helpful.

    Is it possible when clicking to the attribute swatch to change to another product page?

    (I have the same swatches for 4 products, and I made 4 listings changing only the color).

    It would be a great help!

  2. Hey there. It is a perfect article. But I’d like to use external link. Is this possible?

    Oliver

  3. i have copy pasted the code in the function.php the attribute shows the url filed but the link is directed to product page attribute

  4. i have copy pasted the code in the function.php the attribute shows the url filed but the link is directed to product page attribute – the code is not properly working

    1. delete https:// or http:// in url with markdown [text] (url)

  5. I have the same problem…

  6. code is not working for me . The attribute url redirct to product attribute page but i wnat it rediect to another product page please help.

  7. I added this code to the theme’s functions.php but when I clicked Products > Attributes it gave me this error “HTTP ERROR 500”, so I deleted the code but I’m still receiving the error. Please advise as I need to be able to access this part of the website.

    Kind Regards.

  8. Any updates on this? As it isn’t currently working. 🙂

  9. Hello, thanks for the info.

    Couid you please let us know how to make the link No-follow and open in a different tab.

    Thanks.

    1. Hello,
      You can edit line 91 to add the nofollow and target directly into all links.

      Changing it individually would require much more work since you’d have to edit the regular expression that parses the Markdown.

  10. Thank you very much for sharing this, it’s working really great!

  11. Great to see that the code is functioning after all the updates to Woocommerce.

    I’m trying to replace the entire text of one of my products Custom Product Attribute’s and the text contains a link to the Contact web page. How can this be done please?

    Many thanks for your excellent article.

  12. improveitsolutions Avatar
    improveitsolutions

    This is an absolutely incredible feature that you have provided!

    Is there any way to open the links in a new window?

  13. I get this notice for the above suggested code:
    [11-Feb-2022 14:56:41 UTC] PHP Notice: Trying to get property ‘term_id’ of non-object in /customers/3/4/9/bernerbecker.com/httpd.www/community/wp-content/themes/themify-ultra-child/functions.php on line 152

    The line holds this: $url = get_term_meta( $term->term_id, ‘attribute_url’, true );

    And is in your example line 79.

    I’m not sure, but something is causing attributes to disappear when saving the post. Maybe it could be the cause. Why does this happen and is there any easy way to solve this?

    Thanks!

  14. Link product attribute value to a URL – woocommerce

    […] I would like to add a local product attribute value that will have a link which points to a particular ‘href’ value. I followed this blog and tried to add via the markup option mentioned at the end of the blog, the value turns into an anchor tag link but the href value is not taken properly. https://nicolamustone.blog/2016/03/11/make-product-attributes-linkable/ […]

  15. Lê Trọng Nhân Avatar
    Lê Trọng Nhân

    Can you show me how to add link to product attribute label, please ?

  16. Awesome! This code still works perfectly here in 2023 on WC v7.7.0. Just added & configured our custom product attribute URLs for https://www.drivencoffee.com/ — must better for customer navigation from product to product. Cheers!

  17. Thank u, works!

  18. Hi,

    I guess the Code in this article is writing to the database.

    A better solution would be to generate the attribut links, when a product page is opened.
    AND
    Only for those attributes that make sense to be clickable.

    With the following code sample and a proper configuration of the search extention Relevanssi or any other search extention, the clickable link will take the clicked term and redirect to the search page with the products listed that share the same search term.

    Use this code in a child-theme functions.php OR the plugin Code Snipped.
    I recommend Code Snipped.

    /**
    * Make specific custom attributes clickable links and ignore non-alphabetic words in German.
    */
    function make_specific_attributes_links( $attribute_html, $attribute, $values ) {
    $specific_attributes = array(
    ‘Kultivar-Model’,
    ‘Japanisches Wort’
    );

    // Check if it’s a custom attribute (not a global attribute) and is in the specific attributes list
    if ( ! $attribute[‘is_taxonomy’] && in_array( $attribute[‘name’], $specific_attributes ) ) {
    $attribute_values = array();

    foreach ( $values as $value ) {
    // Check if the value contains only alphabetic characters (German)
    if ( preg_match( ‘/^[a-zA-ZäöüÄÖÜß]+$/’, $value ) ) {
    // Create the search URL
    $search_url = home_url( ‘/?s=’ . urlencode( $value ) . ‘&post_type=product’ );

    // Generate the clickable link
    $attribute_link = ‘‘ . esc_html( $value ) . ‘‘;

    // Add the clickable link to the array of attribute values
    $attribute_values[] = $attribute_link;
    } else {
    // Non-alphabetic value, add it as plain text
    $attribute_values[] = esc_html( $value );
    }
    }

    // Implode the attribute values with the original separators
    $attribute_html = ” . implode( ‘, ‘, $attribute_values ) . ”;
    }

    return $attribute_html;
    }
    add_filter( ‘woocommerce_attribute’, ‘make_specific_attributes_links’, 10, 3 );

Leave a Reply

You Might Also Like These Articles


Contribute to Improving This Blog

Did you enjoy this article? Was it helpful? Contribute to help me write more articles! The funds collected from this blog are reinvested directly into improving my skills or the blog so that I can provide more and better content!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

€1.00
€5.00
€10.00
€5.00
€15.00
€100.00
€5.00
€15.00
€100.00

Or enter a custom amount


Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Don't Miss a Thing!

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Join 673 other subscribers

Continue Reading

Discover more from Nicola Mustone

Subscribe now to keep reading and get access to the full archive.

Continue reading