Tuesday 17 May 2016

AEM eCommerce / Products

It is possible to import product data into AEM using the example geometrixx solution.  The example given imports a csv file using the ProductImporter interface.  However, it is actually much simpler to just create the product nodes manually.

Import Code

The following code can be used to create a product in the correct area in the JCR.  It could be called multiple times as a loop through a CSV, XML or WebService feed

    // Create the product node
    final String testPath = "/etc/commerce/products/test";
    final Resource testPathResource = resourceResolver.getResource(testPath);

    // Get the test path as a Node object
    final Node testJcrNode = testPathResource.adaptTo(Node.class);

    // Add a child node on the test node
    final Node productNode = testJcrNode.addNode("prodABC", "nt:unstructured");

    // Add the properties to the product node created
    productNode.setProperty("cq:commerceType", "product");
    productNode.setProperty("sling:resourceType", "commerce/components/product");
    productNode.setProperty("cq:tags", new String[] {"my:mortgage"});
    productNode.addMixin("cq:Taggable");

    productNode.setProperty("jcr:title", "Product ABC");
    productNode.setProperty("identifier", "ABC");

    ... other properties can be added

Note: If you don't include the addMixin("cq:Taggable") then the rollout of a catalog that includes match criteria based on the tags will not work even if a tab is present.

Subfolders

Subfolders can be created as part of the process above is that is needed.  The easiest way to create a subfolder is to use the ResourceResolver.

In the code below the parentResource has a new subfolder created with the name 'title'.

    final Resource parentResource = resourceResolver.getResource(path);
    resourceResolver.create(parentResource, title, new HashMap<>());

This will create a folder of

    primaryType = sling:Folder 

No comments:

Post a Comment