Skip to content
Advertisement

Set variations default attributes values for a variable product Programmatically

I’m building a site where WooCommerce Variable products (with Products Variations) are inserted programmatically. I have followed this tutorial successfully:
Insert WooCommerce Products & Variations Programmatically

I need just something that is missing:
How to set variations default attributes values for a variable product? Is it possible?

Advertisement

Answer

1). You will need to insert in your json data for each variable product this piece of array like:

JavaScript

Or

JavaScript

This array is made with the attribute short slug (without ‘pa_‘) and the default term name values.

2). Then the dedicated function:

JavaScript

3). You will need to trigger this function adding one line at the end:

JavaScript

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement