How to update WooCommerce product attributes and variations programmatically

In this tutorial, we are going to learn how to update Woocommerce product attributes and variations programmatically using PHP.

WooCommerce product attributes let you add multiple variations to your products, for example, size, capacity, and requirements-all from one page.

WooCommerce product attributes are an amazing data structure, allowing store owners to create different variable product and versions. With a few clicks, customers can select a product size and color directly from the product page rather than browsing through multiple versions of the same product with slight different variations. The same goes for other variations such as capacity, requirements and model etc.

woocommerce update product variations programmatically

In our previous article we show how to get WooCommerce product variations and attributes programmatically. Up until this point, we did no structural changes in your site. And quite often you won’t need to. But now it’s time to learn how to create and update WooCommerce product attributes and variations on the fly.

This means that you can create new attributes and variations for specific customers. For example, the price the variable product may depend on the customer role. Or maybe you have different options for VIP customers. Even further, you could have hidden options for beta testers.

Unlike working with simple product, this kind of conditional variations is complex. Thus, we give you a simple example and you can take from there, or as us for further directions.

As with anything else with WordPress you can always use a dedicated WordPress plugin. If you don’t like the idea of installing a WordPress plugin just so that you can update the product attribute and product variation in bulk, you can achieve the same results with just a few lines of code.

WooCommerce product attributes are an amazing data structure, allowing store owners to create different variable product and versions.

If you truly want to create variations and attributes dynamically, you’d need to:

  • Get the current WooCommerce Product object
  • Use the wp_insert_post function to create the new variation
  • Get an instance of the WC variation object
  • Loop through its WooCommerce product attribute values creating them (as taxonomies) or simply adding them to the product variation
  • Handle all other variation data, such as product price, SKU, stock

Now let’s see a simpler example. Here we change the attribute name depending on the user role. Thus, VIP users will get a different attribute title. For example, let’s say that regular users select a WooCommerce product variation that includes a membership card. But they don’t even know that there are VIP cards and members. Then, there’s no reason to cause confusion with this difference. We can change the label, and regular users can pick a membership card, while VIP members pick their VIP card.

Here is a simple code snippet that you can add to your WordPress website:

 

Conclusion

Product attributes and variations in WooCommerce allow store owners to add additional information to your products. Today, we looked into many ways to update variable product attributes and variations on your site programatically. We also looked at working with code examples to learn more about how to modify them for your own use. By the end of the day, you are able to gather these examples and work on your own WooCommerce site.

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