If you're trying to import a file that has this sort of structure:
You're going to run into a few issues:
- Variations do not have title/content data.
- Variations do not have prices or images.
- Parent products have stock listed but it'd be better to manage stock on a variation level.
To import these products properly you should use the {product_id[1]} element along with the "All variations for a particular product have the same title as the parent product" option from WP All Import's official documentation (see documentation) while making these changes:
In the title section, add some elements so that the variations will have titles:
You should use the {model_quantity[1]} element for the stock since we're going to manage it on the variation level:
In the Custom Fields section import the prices into fields named "_custom_regular_price" and "_custom_sale_price":
Next, we need to save the following code inside the Fucntion Editor:
function wpai_wp_all_import_variable_product_imported( $post_parent ) {
global $wpdb;
if ( $results = $wpdb->get_results( 'SELECT `ID` FROM `' . $wpdb->prefix . 'posts` WHERE `post_parent` = "' . $post_parent . '" AND `post_type` = "product_variation"' ) ) {
$parent_product = wc_get_product( $post_parent );
$regular_price = $parent_product->get_meta( '_custom_regular_price', true );
$sale_price = $parent_product->get_meta( '_custom_sale_price', true );
$price = ( ! empty( $sale_price ) && $sale_price < $regular_price ) ? $sale_price : $regular_price;
$main_image = $parent_product->get_image_id();
$parent_product->set_manage_stock( 'no' );
$parent_product->delete_meta_data( '_custom_regular_price' );
$parent_product->delete_meta_data( '_custom_sale_price' );
foreach ( $results as $variation ) {
$var_product = wc_get_product( $variation->ID );
$var_product->set_regular_price( $regular_price );
$var_product->set_sale_price( $sale_price );
$var_product->set_price( $price );
$var_product->set_image_id( $main_image );
$var_product->save();
}
$parent_product->set_regular_price( null );
$parent_product->set_sale_price( null );
$parent_product->save();
update_post_meta( $parent_product->get_id(), '_price', $price );
}
}
add_action( 'wp_all_import_variable_product_imported', 'wpai_wp_all_import_variable_product_imported', 10, 1 );
This will turn off stock management for the parent product, then copy the prices and images from the parent to all of its variations.
Thank You, Problem solved. Great support.