2

I am creating a product with variations:

// Creates a product

        $post = array(
         'post_title'   => $title,
         'post_content' => $description,
         'post_status'  => "publish",
         'post_excerpt' => $excerpt,
         'post_name'    => $slug, //name/slug
         'post_type'    => "product"
 );


$new_post_id = wp_insert_post( $post);

// Creates needed attributes

$product_attributes = array();

$product_attributes['print_size'] = array(
    'name' => 'print_size',
    'value' => implode('|', $availableSizes),
    'position' => 0,
    'is_visible' => 0,
    'is_variation' => 1,
    'is_taxonomy' => 0
);

$product_attributes['paper_type'] = array(
    'name' => 'paper_type',
    'value' => implode('|', $availableMaterial),
    'position' => 0,
    'is_visible' => 0,
    'is_variation' => 1,
    'is_taxonomy' => 0
);


update_post_meta($new_post_id, '_product_attributes', $product_attributes);

// Creates variation

$my_post1 = array(
    'post_title' => 'SizeType122',
    'post_name' => 'post_name222',
    'post_status' => 'publish',
    'post_parent' => $new_post_id,
    'post_type' => 'product_variation',
    'guid' => home_url() . '/?product_variation=1' 
);

$attID1 = wp_insert_post($my_post1);

update_post_meta($attID1, '_virtual', 'no');
update_post_meta($attID1, '_downloadable', 'no');
update_post_meta($attID1, '_manage_stock', 'no');
update_post_meta($attID1, '_stock_status', 'instock');
update_post_meta($attID1, '_sku', '300');
update_post_meta($attID1, '_price', '300');

It creates a variation with attributes as it should, but if I check product variation it brings me something like this:

Array
(
    [0] => Array
        (
            [variation_id] => 1542
            [variation_is_visible] => 1
            [variation_is_active] => 1
            [is_purchasable] => 1
            [display_price] => 300
            [display_regular_price] => 300
            [attributes] => Array
                (
                    [attribute_print_size] => 
                    [attribute_paper_type] => 
                )
  [image_src] => 
        [image_link] => 
        [image_title] => 
        [image_alt] => 
        [image_caption] => 
        [image_srcset] => 
        [image_sizes] => 
        [price_html] => 
        [availability_html] => 
        [sku] => 300
        [weight] =>  kg
        [dimensions] => 
        [min_qty] => 1
        [max_qty] => 
        [backorders_allowed] => 
        [is_in_stock] => 1
        [is_downloadable] => 
        [is_virtual] => 
        [is_sold_individually] => no
        [variation_description] => 
    )

)

So attribute is empty and if I go to admin side I can see "any print size" and "any paper type" as variations. If I select specific attributes and save in the admin I can see these values in the array which I shared up top filled :

  [attributes] => Array
                (
                    [attribute_print_size] => 8x10
                    [attribute_paper_type] => Enhanced Matte Paper
                )

So how I could do this programmatically?

1

For all of those who will be struggling with this you just simply need to use this like:

      update_post_meta($attID1, 'attribute_print_size', '8x10');
 update_post_meta($attID1, 'attribute_paper_type', 'Premium Luster Photo');

This sets the exact variations.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.