We are importing products in our WooCommerce shop with an API. All is working but we have a problem with the variations we are importing for a product. Everything is importing correctly but the last variation is set to Every size...
while it should be 11/XXL
. If I click on the variation which is set to Every size...
all the data is filled in but it is not automatically setting the variation to 11/XXL
while all other variations are set automatically the correct way.
Here is the function we are using to add the variations.
/**
* Create a product variation
*/
function create_variation($product_id, $variation_data) {
// Get variable product parent
$product = wc_get_product($product_id);
$variation_post = array(
'post_title' => $product->get_name(),
'post_name' => 'product-'.$product_id.'-variation',
'post_status' => 'publish',
'post_parent' => $product_id,
'post_type' => 'product_variation',
'guid' => $product->get_permalink()
);
// Create product variation
$variation_id = wp_insert_post($variation_post);
// Get WC_Product instance of variation
$variation = new WC_Product_Variation($variation_id);
// Iterate through variation attributes
foreach($variation_data['attributes'] as $attribute => $term_name)
{
$taxonomy = 'pa_'.$attribute;
// Create taxonomy if it doesnt exist
if(!taxonomy_exists( $taxonomy )) {
register_taxonomy(
$taxonomy,
'product_variation',
array(
'hierarchical' => false,
'label' => ucfirst($attribute),
'query_var' => true,
'rewrite' => array('slug' => sanitize_title($attribute)),
)
);
}
// Create term if it doesnt exist
if(!term_exists($term_name, $taxonomy))
wp_insert_term($term_name, $taxonomy);
$term_slug = get_term_by('name', $term_name, $taxonomy)->slug;
// Set/save the attribute data in the product variation
update_post_meta($variation_id, 'attribute_'.$taxonomy, $term_slug);
// Get the post Term names from the parent variable product.
$post_term_names = wp_get_post_terms($product_id, $taxonomy, array('fields' => 'names'));
// Check if the post term exists and if not, set it in the parent product
if(!in_array($term_name, $post_term_names))
wp_set_post_terms($product_id, $term_name, $taxonomy, true);
}
## Set/save other data
//SKU
if(!empty($variation_data['sku']))
$variation->set_sku($variation_data['sku']);
//Prices
if(empty($variation_data['sale_price'])) {
$variation->set_price($variation_data['regular_price']);
}else {
$variation->set_price($variation_data['sale_price']);
$variation->set_sale_price($variation_data['sale_price']);
}
$variation->set_regular_price($variation_data['regular_price']);
// Stock
if(!empty($variation_data['stock_qty'])) {
$variation->set_stock_quantity($variation_data['stock_qty']);
$variation->set_manage_stock(true);
$variation->set_stock_status('');
}else {
$variation->set_manage_stock(false);
}
$variation->set_weight('');
$variation->save();
}
Here is the $variation_data
Array
(
[attributes] => Array
(
[maat] => 7/S
)
[sku] => 14120007
[regular_price] => 1.53
)
Array
(
[attributes] => Array
(
[maat] => 6/XS
)
[sku] => 14120006
[regular_price] => 1.98
)
Array
(
[attributes] => Array
(
[maat] => 8/M
)
[sku] => 14120008
[regular_price] => 1.53
)
Array
(
[attributes] => Array
(
[maat] => 9/L
)
[sku] => 14120009
[regular_price] => 1.53
)
Array
(
[attributes] => Array
(
[maat] => 10/XL
)
[sku] => 14120010
[regular_price] => 1.53
)
Array
(
[attributes] => Array
(
[maat] => 11/XXL
)
[sku] => 14120011
[regular_price] => 1.53
)
Here is the output in the back-end of WooCommerce
As you can see the variation 11/XXL
is set as Every size...
but it should be 11/XXL
.
Does anyone know how to fix this so the variation is set as 11/XXL
instead of Every size...
Thanks for your time!