
Today’s Ask SkyVerge question is from Ted:
I’m trying to embed the WooCommerce SKU of the product being viewed into a link in short product description for that product. Basically, I’d like to pass the SKU (aka video ID) into a link over to a generic streaming video page.
This is an interesting idea, and something that we could easily implement using a shortcode. As we can’t directly insert PHP into our post content, a shortcode can be used to do this for us. If you’ve never created a shortcode, we recommend reading our introduction to creating WordPress shortcodes.
Let’s create a shortcode that will return the WooCommerce SKU for the product, which can then be displayed or used in a link. If you’re adding this code to your site, remember to add it properly via Code Snippets or a child theme as per our tutorial.
You could do this very easily with a couple lines of code:
function skyverge_echo_wc_sku() { global $product; return $product->get_sku(); } add_shortcode( 'sv_sku', 'skyverge_echo_wc_sku' );
This would give you the SKU using [sv_sku], which can be included as part of your link or anywhere on a product page, but couldn’t be used elsewhere. For example, here’s the output of the SKU below our short description:
However, I’m going to get a bit fancier and give us a more general use shortcode. Let’s instead create a shortcode that will do the same thing on a product page, but could optionally accept an “ID” to use the SKU from a different product, or to echo the SKU when used elsewhere besides the product page.
This could then be used anywhere; use [wc_sku]
if you’re on a product page to echo the SKU for that product just as we did above. Use [wc_sku id="10"]
, replacing 10 with the right product ID, to display a SKU for a product somewhere besides the product page (or to use the SKU for another product).
Here’s an example of this shortcode in use in a post:
And here’s the frontend:
You can also use it inside of HTML. For example, you can use it as part of a link:
<a href="http://youtube.com/[wc_sku]">Click here to view</a>
Taking it further
Going back to our original question, you can automate this a bit further. You could create a shortcode that would just echo the entire link for you if the structure will always be the same. Let’s create a shortcode that will just echo a YouTube link, which will replace part of the link with my SKU.
Now this shortcode will instead just output the entire link with the anchor text when [wc_sku_link]
is used. Here’s an example using this shortcode at the end of my short description, and notice that the SKU is part of the link:
If you needed this for every product, you could also get fancy and automatically add it to your template for all product short descriptions or descriptions. For example, you could use the woocommerce_short_description
filter to append this information to the outputted excerpt, or you could override your short description template (since this is unlikely to change). Here’s an example using the filter I mentioned:
We’d love to hear about it if you use this tip!
Need some help taking this code further? We recommend Codeable for small custom projects.
Verry Usefull article. I keep it my Evernote 🙂
Thanks a lot 😉
I wish this worked for me. It’s exactly what I’ve been trying to figure out the past few days. I want to automatically pass the sku or url of a contact us link on product pages to pre-populate form fields. Maybe something in wordpress or woocommerce has changed?
I know why it isn’t working for me it’s because I am putting the shortcode inside of my theme’s button shortcode which will require additional programming. I would love to have this and am only a beginner at wordpress. If you decide to add that code to your example I would be very grateful.
This is exactly what I am looking for, but which file do I insert the script into?
Thanks!
Works like a charm, but I’m breaking my head on how to use variation SKU instead of Parent SKU.
Any ideas that point me in the right direction?
Afraid it’s not as simple, you’d need some javascript to detect which variation is selected and then adjust the SKU display accordingly.
How can create woocommerce checkout shortcode with specfic product id in wordpress.