• hi,

    so before the 2.2 update i was able to add a new variation of a product programatically. after the 2.2 update all the variation info is shown in the backend and the new variation name in under the “additional information” tab on the single produc page. the name is not shown in the dropdown box and my site crashes as soon as i add a second variation.heres the code that worked before 2.2:

    // get the number of variations of the wc_product
    		$menu_order = $wpdb -> get_var($wpdb -> prepare("SELECT MAX(menu_order) FROM $wpdb->posts WHERE post_parent = '%s'", $wc_product_id));
    
    		// create the variations in wc
    		for ($j = 0; $j < count($vars); $j++) {
    			create_variation($vars[$j], $wc_product_id, $menu_order + 1 + $j, strtolower($tg_product -> opt1));
    		}
    		// create the wpdb version of attribute lists ( eg. red,blue,black = "red | blue | black") and update it
    		$var_names = create_new_variation_names(get_product($wc_product_id), $names, strtolower($tg_product -> opt1));
    		$thedata = Array(strtolower($tg_product -> opt1) => Array('name' => strtolower($tg_product -> opt1), 'value' => $var_names, 'is_visible' => "1", 'is_variation' => "1", 'is_taxonomy' => "0"));
    		update_post_meta($wc_product_id, '_product_attributes', $thedata);
    
    		WC_Meta_Box_Product_Data::save_variations();

    and the create variation function:

    `function create_variation($var, $parent_post_id, $i, $attribute_type) {

    // create variation info in wpdb _post
    $new_post2 = array(‘post_title’ => “random”, ‘post_content’ => $var -> description, ‘post_status’ => ‘publish’, ‘post_type’ => ‘product_variation’);
    $post_id2 = wp_insert_post($new_post2);
    $product_name = “product-” . $parent_post_id . “-variation”;
    if ($i != 0) {
    $j = $i + 1;
    $product_name = $product_name . “-” . $j;
    }
    $new_post2 = array(“ID” => $post_id2, “post_author” => 2, “post_status” => “publish”, “comment_status” => “open”, ‘post_title’ => “Variation #” . $post_id2 . ” of ” . $var -> product_name, ‘post_name’ => $product_name, ‘post_parent’ => $parent_post_id, ‘menu_order’ => $i, ‘post_type’ => ‘product_variation’);
    $post_id2 = wp_insert_post($new_post2);
    $sku = $var -> sku;

    // create variation info in wpdb _post_meta
    add_post_meta($post_id2, ‘_sku’, $sku);
    add_post_meta($post_id2, ‘_virtual’, “no”);
    add_post_meta($post_id2, ‘_downloadable’, “no”);
    add_post_meta($post_id2, ‘_stock’, $var -> stock_on_hand);
    add_post_meta($post_id2, ‘_manage_stock’, “yes”);
    add_post_meta($post_id2, ‘_stock_status’, “instock”);
    add_post_meta($post_id2, ‘_price’, $var -> buy_price);
    add_post_meta($post_id2, ‘_regular_price’, $var -> buy_price);
    add_post_meta($post_id2, ‘attribute_’ . strtolower($attribute_type), $var -> name);
    add_post_meta($post_id2, ‘_backorders’, “yes”);
    add_post_meta($post_id2, ‘_thumbnail_id’, 0);
    wp_set_object_terms($post_id2, ‘variable’, ‘product_type’);
    wp_set_object_terms($post_id2, $var -> name, strtolower($attribute_type));
    update_min_max_price($parent_post_id, $var -> buy_price, $post_id2);
    }`

    Ive been trying to figure this out for a while so any help is much appreciated 🙂

    thanks,
    ted

    https://wordpress.org/plugins/woocommerce/

  • The topic ‘Adding variation programmatically afer 2.2 update not working’ is closed to new replies.