We are moving a client from Shopify to Woocommerce and using the WordPress WP-All-Import plugin to easily move the data to Woocommerce. In this article we are importing the orders. We’ve already imported the customers and products which should be done first so orders can be linked to customers and products. The order export csv file from Shopify puts each line item of an order on a separate line. For importing we want each order on a single line and all line items in a single field separated by a pipe, ‘|’. So our program will parse in the csv file convert each line to an object and store into an array. We create a newLine holder to hold a line of data to export and orderNum which monitors what order number we are on.
Here’s the program using node.js
const csv = require('csvtojson');
const csvWriter = require('csv-write-stream');
const fs = require('fs');
const orders = [];
const weights = [];
let getProducts =
csv()
.fromFile('ordersFromShopify.csv')
.then((jsonObj) => {
jsonObj.forEach(function (item) {
orders.push(item);
})
});
Promise.all([getProducts]).then(() => {
const newOrders = [];
const writerExport = csvWriter({
})
writerExport.pipe(fs.createWriteStream('neworders.csv'))
let orderNum = '';
let newLine = {};
for (const line of orders) {
if (line['Name'] !== orderNum) {
if (typeof (newLine['Name']) !== 'undefined') {
writerExport.write(newLine);
}
newLine = Object.assign({}, line)
orderNum = line['Name'];
} else {
newLine['Lineitem price'] = newLine['Lineitem price'] + '|' + line['Lineitem price']
newLine['Lineitem name'] = newLine['Lineitem name'] + '|' + line['Lineitem name']
newLine['Lineitem quantity'] = newLine['Lineitem quantity'] + '|' + line['Lineitem quantity']
newLine['Lineitem sku'] = newLine['Lineitem sku'] + '|' + line['Lineitem sku']
newLine['Lineitem fulfillment status'] = newLine['Lineitem fulfillment status'] + '|' + line['Lineitem fulfillment status']
}
}
writerExport.write(newLine);
});
The first line of the shopify order export csv file are the field names:
Name,Email,Financial Status,Paid at,Fulfillment Status,Fulfilled at,Accepts Marketing,Currency,Subtotal,Shipping,Taxes,Total,Discount Code,Discount Amount,Shipping Method,Created at,Lineitem quantity,Lineitem name,Lineitem price,Lineitem compare at price,Lineitem sku,Lineitem requires shipping,Lineitem taxable,Lineitem fulfillment status,Billing Name,Billing Street,Billing Address1,Billing Address2,Billing Company,Billing City,Billing Zip,Billing Province,Billing Country,Billing Phone,Shipping Name,Shipping Street,Shipping Address1,Shipping Address2,Shipping Company,Shipping City,Shipping Zip,Shipping Province,Shipping Country,Shipping Phone,Notes,Note Attributes,Cancelled at,Payment Method,Payment Reference,Refunded Amount,Vendor,Id,Tags,Risk Level,Source,Lineitem discount,Tax 1 Name,Tax 1 Value,Tax 2 Name,Tax 2 Value,Tax 3 Name,Tax 3 Value,Tax 4 Name,Tax 4 Value,Tax 5 Name,Tax 5 Value,Phone,Receipt Number,Duties