Skip to content

Instantly share code, notes, and snippets.

@trey8611
Last active September 21, 2024 21:02
Show Gist options
  • Save trey8611/b1597e00a29faa5bb17c72ef10e3af69 to your computer and use it in GitHub Desktop.
Save trey8611/b1597e00a29faa5bb17c72ef10e3af69 to your computer and use it in GitHub Desktop.
WP All Import & WooCommerce Add-On - How to import PRODUCT / MODEL product files.

Importing PRODUCT / MODEL files

If you're trying to import a file that has this sort of structure:

data in csv

You're going to run into a few issues:

  1. Variations do not have title/content data.
  2. Variations do not have prices or images.
  3. 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:

Title

In the title section, add some elements so that the variations will have titles:

title

Stock

You should use the {model_quantity[1]} element for the stock since we're going to manage it on the variation level:

model quantity

Prices in Custom Fields

In the Custom Fields section import the prices into fields named "_custom_regular_price" and "_custom_sale_price":

custom field prices

Code in the Function Editor

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.

@importfusion
Copy link

importfusion commented Dec 10, 2019

Thank You, Problem solved. Great support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment