Migrating data from .CSV files using feeds and commerce feeds modules
In the last article, we programmed a new module which created CSV files from data on a different server. Now, we need to migrate this data into our live-system. As first step we need to create a commerce store to which our products and product variations belong. Otherwise, they cannot be imported.
Commerce product and product variations
Afterwards we need a commerce product type and a commerce product variation type. A product type stores a general product, while a product variation type stores different variation of said product type. I.e. "Pair of cotton pants" would be defined as product type, while "Pair of cotton pants in red/blue/yellow, size s/m/l would be defined as product variation type.
Our product type is a 'room' and as fields, we want to store an id, a title and an image carousel showcasing the room.
Our product variation type needs to display different variations of a 'room'. Here we also need the id and additional fields for a variation title and a description of the room variation.
After these types are finished, we need to define two corresponding feed types. Both feed types need almost the same settings.
The other settings can be changed to personal liking, except the default delimiter in the Parser settings which needs to be a comma(,) because that is what we chose for our csv files.
Now, the feed type mapping has to be set up. The feeds will only work, if all mandatory fields are mapped correctly. We need to select 'Feed: Authored by' and 'Feed: Authored on' as source fields. Additionally we need 'Status' and 'SKU' which need to be set for Commerce products. These are the minimum settings we need for feeds to run. Additionally, our product type room also needs 'Store'. Otherwise commerce cannot import the product. Store and status need to be imported from the csv files, authored by and authored on don't need to be. Store has to be the store for which the product and product variation types were created for.
We select all other fields from the csv file we'd like to map to our product type /product variation type and save.
If we were to import now, the variations would not appear. They are linked to their corresponding products by the sku defined in the csv file, but the feeds module needs help to understand the link. The module feeds tamper creates another tab 'Tamper' in the feeds type settings. Here we create a plugin for our feeds type room. We select explode and as field we chose the sku and as delimiter we select '|', because that is what we chose during the csv file creation.
Creating Feeds and importing
After defining the feed types, we have to create actual feeds from them. In Content > Feeds. All we need the the path to our csv file and the correct delimiter ','. After creating one feed for the products and one for the product variations, all we have to do is activate them. Feeds belong to website-content, which means they need to be imported like content. Otherwise you have to create them again and they can't be exported to config-files.
When actually importing, the order is important. We need to import the product variations before the products, otherwise they will not appear. Feeds module creates a temporary table with the newly imported product variations and compares their skus with skus from newly imported products and connects them accordingly. It does not work the other way around.