I have a front-end form I am creating to allow users to publish a variable product to my shop from the front end with predefined attributes and variations.
I have found this very helpful question: here Which shows me how to set the product type as variable and assign my predefined attributes in the attributes section of the product data.
However when I am on the backend of Wordpress/Woocommerce and editing the product I click on variations and none are set, I look at the attributes and my "resolution" attribute is set with my 3 items.
How do I make this to where it will actually set those attributes to variations of my form? Do I need to use wp_insert_post? Looking in phpmyadmin it just looks like product variations are assigned to a parent_id (product id) and post type is product_varition and so on.
$new_post = array( 'post_title' => esc_attr(strip_tags($_POST['postTitle'])), 'post_content' => esc_attr(strip_tags($_POST['postContent'])), 'post_status' => 'publish', 'post_type' => 'product', 'tags_input' => array($tags) ); $skuu = rand(); $post_id = wp_insert_post($new_post); update_post_meta($post_id, '_sku', $skuu ); //my array for setting the attributes $avail_attributes = array( 'high-resolution', 'medium-resolution', 'low-resolution' ); //Sets the attributes up to be used as variations but doesnt actually set them up as variations wp_set_object_terms ($post_id, 'variable', 'product_type'); wp_set_object_terms( $post_id, $avail_attributes, 'pa_resolution' ); $thedata = array( 'pa_resolution'=> array( 'name'=>'pa_resolution', 'value'=>'', 'is_visible' => '1', 'is_variation' => '1', 'is_taxonomy' => '1' ) ); update_post_meta( $post_id,'_product_attributes',$thedata); update_post_meta( $post_id, '_visibility', 'search' ); update_post_meta( $post_id, '_stock_status', 'instock');
So just to be clear (I tend to be confusing) the above does create my variable product from the front end, and when I look at the product in the backend it is a variable product, it has the resolution attribute set and has my 3 terms (high-res, medium-res, low-res) as attributes. I just need to take this a step further where they are actually set as variations so people can place an order.