Importing Product Stocks With Dataflow Profiles

August 9, 2013

Working with Magento means working with a lot of data, and if you’ve done Magento for a while, that will probably seem like an understatement!

Your typical store could have thousands of SKUs and tens of thousands of customers and associated addresses. With every project you eventually need to move this data around and, naturally, you ask, what’s the best way to do that?

And like most things Magento, the answer will likely be: it depends.

Magento And Dataflow

If you need to move a small amount of data quickly, you might go through Magento’s API. If you have a ton of data, you might look into writing scripts to interact directly with the store database.

Likely you’re going to end up somewhere in between, so you might find yourself looking for a module to handle it. Magento comes built in with two different “basic” modules that can help you to import and export store data. One is the ImportExport module and the other is Dataflow. You can find both of them in the top navigation of the admin area under System > Import/Export. They both have their upsides and downsides.

I found myself working most recently with Dataflow to manually manage stock levels, so in this post, I’ll be covering how to use the default ‘Import Product Stocks’ profile to import a CSV file matching SKU to inventory quantities. In my case, I am running Magento Enterprise 1.12, but the process is the same across all of the modern Magento versions and editions.

To set us up, here’s a shot of my product catalog. I’ll be updating the inventory for 5 of the products in my store:

screen shot of product catalog

 

I’m going to set the inventory to 10000 for the following SKUs: ID1, ID2, ID3, ID4, and ID5.

CSV File And Formatting Prerequisites



Before we start, we should discuss the file that we are going to import. Dataflow has a few options that allow you to configure the CSV file format or even work with certain formats of Excel files. Magento seems very flexible but is actually very picky about formatting.

Here’s the CSV file that we will be importing in this walk through (import_product_stocks.csv):

import_product_stocks.csv

"store","sku","qty","is_in_stock"

"default","ID1","10000.0000","1"

"default","ID2","10000.0000","1"

"default","ID3","10000.0000","1"

"default","ID4","10000.0000","1"

"default","ID5","10000.0000","1"

I recommend running a few test imports with a file matching this format so you can become familiar with the process. All the values in your document should be enclosed in double-quotes (“) and be separated by a comma (,). Each row should be separated by a new line. The first line in the document should contain the name of the columns that will be imported. If possible, I also recommend that your CSV file is encoded in UTF8 and uses LF line endings.

You will want 4 columns in your CSV file: “store”, “sku”, “qty”, and “is_in_stock”.

The “store” column will contain the store code that the product you want to import inventory to belongs. My demo store is fairly new, so in this walk-through my store code is “default”. You can find the store code for a particular store by going to‘System > Manage Stores’ in the top menu. You’ll see a grid listing the stores in your instance of Magento.

store code listings

 

Click on the link from the far right column to edit the ‘Store View’ for an entry.

screen shot of step edit the store view for an entry

 

The store code is listed in the ‘Store View Information’ section, and this is the value that you’ll use to identify the stores in your import CSV file.

The “sku” and “qty” columns are self explanatory: put the SKU you’d like to update and the inventory quantity that you’d like to set in these columns.

The last column “is_in_stock” allows you to set whether or not the product is in stock. Put a “1” in here to set the item as in stock, or a “0” if you’d like it to be out of stock.

After you have a handle on how the system works, feel free to experiment with different file formats and encodings and see what Magento will accept.

Dataflow Profiles

First off, you’ll need to navigate to the ‘Dataflow – Profiles’ page. From the dashboard, you will find the link in the top menu under ‘System > Import/Export > Dataflow – Profiles’:

screen grab of dataflow profiles page

 

Once you’re at the ‘Dataflow – Profiles’ page you will see a grid showing off a list of all the profiles defined in your Magento store.

 screen shot of grid showing off a list of all the profiles defined in your Magento store

 

So what is a profile? It’s a configured list of instructions that tells Magento how to import a particular file or create an export. Magento comes with a few default profiles for you to configure and use. Our focus will be on ‘Import Product Stocks’. Click on it to head over to the edit view.

Configuring a Profile

screen shot of a profile being configured

When you’re at the profile edit screen you’ll see a ton of information. There’s some basic configuration options on the right and some tabs on the left. We’ll walk through all of the sections you’ll need to adjust in order to import a CSV of SKU and inventory quantities.

Profile Information



The first section is called ‘Profile Information’ and you shouldn’t need to adjust anything in this section. All of the default values should work. I know that the ‘Number of records’ value is particularly confusing but you don’t need to adjust that for exporting data. Note: if you only plan on importing product stocks for a particular store, you can set the default store here in the ‘Store’ select box. Then you will be able to skip the “store” column in your import file. If you plan on importing stocks for multiple stores at once, you should probably leave this option alone.

In the ‘File Information’ section you’ll see a select box for configuring the file source/destination of the profile. You have options that allow you to set a particular directory on the server, or configure an FTP site to use; these would be helpful if you had an automated export that dumped your inventory data to a particular place. For now, we’ll leave it set to‘interactive’. This will allow us to upload a CSV file for Magento to read.

The last section in the first tab is ‘Data Format’. We won’t need to adjust any options here if you match the file format as I described above, but if you have a CSV file that doesn’t have standard delimiters or enclosures, you can adjust the options to handle that here.

Upload File

Now that we have done some basic configuration for our profile we are ready to upload our ‘import_product_stocks.csv’file. Click on the ‘Upload File’ tab on the left.

screen shot of how to upload file

First you’ll likely notice some semi-helpful information messages. In my case I see one telling me about the max file upload size and another that notes what file encodings are supported.

Next is the area where you can select files to upload. The upload process is a little weird, but don’t worry, we’ll get through it!

Go ahead and click ‘Browse’ or ‘Choose File’ and select our CSV file to upload. You’ll notice that there’s no upload button. In order to upload the file you actually have to click the ‘Save and Continue Edit’ button! This will tell Magento to save the profile and post the files to your Magento server. Go ahead and do that now.

When the upload is finished the page will refresh and you will be looking at the ‘Profile Wizard’ tab again. If everything went well you’ll see a green message at the top of your screen that says the profile has been saved. If you run into any errors you’ll see a nasty red box that bluntly encourages you to fix your file and try again.

You might be asking, well, where did my file upload to? By default Magento places all your Dataflow uploads in the ‘../var/import/’ directory under the Magento install location. The list of files in that directory shows up in the next tab, so you will be able to confirm that the file uploaded successfully before running the profile. Click on the ‘Run Profile’ tab.

Run Profile

screen shot of how to run profile

Almost there! You’ll see there’s not many things to do on this tab. There’s just one last notice about how you really should save your changes before you attempt to run the profile. It’s telling you this because when you run the profile it won’t consider any unsaved changes you may have made when it starts working. So don’t forget to save your changes if you see any disk icons in the left side tab area.

We’re now ready to run the import! There is a select box here for you to pick out the file you’d like to import. If your upload finished successfully, you’ll see it listed inside of this select box. Magento automatically prepends the file name with the date, so you might not initially recognize your file. In my case the file name turned out to be ‘import-201308071846121_import_product_stocks.csv’.

Make sure your pop-up blocker is disabled, pick out your file and then click ‘Run Profile in Popup’ to run the import!

run profile in popup screen shot of step

You’ll see a window pop up that has the progress of your import. Dataflow is notorious for being slow. Depending on the size of the import it could take a while for Magento to process everything. If you are importing stock data for a lot of products you might try splitting them up into different imports to handle products for one store at a time. If Magento runs into any problems importing the stock data it will notify you in this window. When the import is finished you’ll see a message that says ‘Finished profile execution’.

If you head on over to your product catalog you can now see that the inventory for your products has been updated. Here’s a screenshot from my store showing that I now have a lot more wine to sell:

screen shot of the now updated product catalog

Wrap up

I think that it’s important for a Magento developer to know all of the tools that the framework provides. The example in this post provides a good illustration of how Dataflow can be used. It’s a very convenient way to run a one-time or on-demand data transfer. For some of our clients that just need to run an import occasionally, it works pretty well; we can set them up with an import profile and show them how to run it when they need to.

How are some ways you use Dataflow in your own projects?