How to show WooCommerce product variations and attributes programmatically

This article describes how to display WooCommerce product attributes and variations programmatically on your WooCommerce shop page.

Selling your products online means that you need to describe them. You might want to list sizes, colours and other attributes and variations. This is something that’s a little more complicated than you think, because you can’t simply write out the attributes and variations for each product on every product page. You also need some kind of system for filtering trough the multiple variations and managing it.

woocommerce show product variations programmatically

A clear design can help users navigate through your store. Therefore, using some code snippets to change how to display WooCommerce attributes and variations is a great idea. Maybe you can use them to remind users of which product variation they picked in their cart. Or you can use these functions to clearly show which variations are available in your store. Well, let’s dive into it then.

As usual, we can use WooCommerce hooks to help us achieve this. There is a hook, in particular, that is interesting, the woocommerce_after_shop_loop_item. This is a hook that is called right after each of your shop list items.

The code for our custom function is quite simple. First we need to check if the product has any variations e.g. if the product type is variable and not simple product. Then we loop through the WooCommerce variation data for all the different variations and we store them in an array. At the end we just need to check in the product data if the product variation is still in stock before we display variations.

Conclusion

Product attributes and variations in WooCommerce allow store owners to add additional information to your products. Today, we looked into how to show the different variable product attributes and variations on your site programatically. If you are working on a project where you need to show product attributes or variations programmatically with PHP code, then you will find this useful.

We hope you enjoyed, and see you again next time!