WooCommerce / Wordpress (Search & Link Local Attributes) Custom PHP Function
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hi there,

I have a website running Wordpress and WooCommerce. On it, some products have a WooCommerce local attribute called 'Product Code' where there is a value next to it.

Attributes
Product Code | MD

I need a custom PHP function to find all products with attribute 'Product Code', and then link it to a document based on the name.

Attributes
Product Code | < a href="link.com/MD" >MD< /a >

Could be based off https://nicola.blog/2016/03/11/make-product-attributes-linkable/. Let me know if you need more information.

Thanks in advance!

Can you please elaborate on the "and then link it to a document based on the name."? What exactly do you need?
kostasx 6 months ago
If the value in the Product Code is MD, then the link should be to link.com/md. Sorry, I had added html in the original description but it seems it was filtered out.
S9x 6 months ago
awarded to Zhopon

Crowdsource coding tasks.

2 Solutions

Winning solution

I wasn't able to understand your requirement. I would request you to upload some images for our references.

I did some test with the code you suggested and was able to achieve the following without any changes in it.
(let me know if this are the solutions you are looking for)

Image 1: http://imgur.com/a/bNK5f

Image 2: http://imgur.com/a/tSJfN

Update 1:
looks like you want this.
Just add the following in code in the function.php
https://pastebin.com/WafwYxpg

Let me know if I was able to help you.

Hi Zhopon, I need a solution similar to Image 1, but instead of typing in the link in markdown (like the code I suggested), I need a function that will do it dynamically for anything called Product Code based on the value.
S9x 6 months ago
Hi S9x i updated the answer based on your comment.
Zhopon 6 months ago
Wasn't quite what I wanted, but I was able to make it work from here. Thanks!
S9x 6 months ago
I am glad, I was able to help you out, thanks for the bounty S9x
Zhopon 6 months ago

Updated for functions.php and Single Product display:

function add_subtitle_to_product( $desc ) {

    global $product;
    global $post;
    $attributes = $product->get_attributes();
    foreach ( $attributes as $attribute ) :
        if ( $attribute['name'] == "Product Code"){
            $desc .= 'Product Code | <a href="' . get_permalink( $post->ID ) . '">'. $attribute['value'] .'</a><br/><br/>';
        }
    endforeach;
    return $desc;
}
add_action( 'woocommerce_short_description', 'add_subtitle_to_product', 10, 2 );
Hi kostasx, Thanks for the quick work. I think there was a miscommunication. I'm looking for a function to be placed in functions.php that does this in place to all of the products on the website. The goal is to have all the products (on their individual product pages) have the Product Code linked. Would it be possible to change your function accordingly? Thanks so much
S9x 6 months ago
This updated the description, and not the attributes area. Thanks anyways!
S9x 6 months ago
View Timeline