Created
February 2, 2019 13:34
-
-
Save raulfmiranda/714254f5e53584b37f22d23bc4c697bd to your computer and use it in GitHub Desktop.
JOB QUE BUSCA POR PRODUTOS DE UMA MARCA ESPECÍFICA E GERA XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const File = require('dw/io/File'); | |
const FileWriter = require('dw/io/FileWriter'); | |
const XMLStreamWriter = require('dw/io/XMLStreamWriter'); | |
const ProductMgr = require('dw/catalog/ProductMgr'); | |
exports.execute = function(args) { | |
try { | |
// Brand from job parameters | |
var brand = args.brand; | |
var products = ProductMgr.queryAllSiteProducts(); | |
var productsWithBrand = []; | |
while (products.hasNext()) { | |
var product = products.next(); | |
// Collect all products with a specific brand | |
if(product.brand == brand) { | |
productsWithBrand.push(product); | |
} | |
} | |
// Save XML on: /on/demandware.servlet/webdav/Sites/Impex/src/exports/categoryAssign.xml | |
var impexPath = File.IMPEX + File.SEPARATOR + 'src' + File.SEPARATOR + 'exports' + File.SEPARATOR + 'categoryAssign.xml'; | |
var file = new File(impexPath); | |
var fileWriter = new FileWriter(file, 'UTF-8'); | |
var xsw = new XMLStreamWriter(fileWriter); | |
/* The code below should write a XML similar to this onde: | |
<?xml version="1.0" encoding="UTF-8"?> | |
<catalog xmlns="http://www.demandware.com/xml/impex/catalog/2006-10-31" catalog-id="apparel-catalog"> | |
<category-assignment category-id="womens-clothing-tops" product-id="008884303989"> | |
<primary-flag>true</primary-flag> | |
</category-assignment> | |
</catalog> | |
*/ | |
xsw.writeStartDocument("UTF-8", "1.0"); | |
xsw.writeStartElement('catalog'); | |
xsw.writeAttribute('xmlns', 'http://www.demandware.com/xml/impex/catalog/2006-10-31'); | |
xsw.writeAttribute('catalog-id', 'apparel-catalog'); | |
for(var i = 0; i < productsWithBrand.length; i++) { | |
xsw.writeStartElement('category-assignment'); | |
xsw.writeAttribute('category-id', 'womens-clothing-tops'); | |
xsw.writeAttribute('product-id', productsWithBrand[i].ID); | |
xsw.writeStartElement('primary-flag'); | |
xsw.writeCharacters('true'); | |
xsw.writeEndElement(); | |
xsw.writeEndElement(); | |
} | |
xsw.writeEndElement(); | |
xsw.writeEndDocument(); | |
xsw.flush(); | |
} catch(e) { | |
// let errorXML = e; | |
} finally { | |
xsw.close(); | |
fileWriter.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment