Customize the Facebook Feed

dw.extensions.facebook.FacebookProduct defines a data structure that contains the Facebook representation of a B2C Commerce product. The product is passed to a hook you can use to customize the default transformation of a product in the B2C Commerce schema to the Facebook catalog feed export schema.

In addition, a customization hook, com.demandware.plugin.facebook.FacebookFeedHooks, is included for optional implementation in customer custom cartridges. This interface defines the hooks that can be implemented to customize the data included in Facebook feed exports.

Name Signature Description
dw.extensions.facebook.feed.transformProduct dw.system.Status transformProduct( dw.catalog.Product product, dw.extensions.facebook.FacebookProduct facebookProduct ) Called after default transformation of given Commerce Cloud product to Facebook product as part of the catalog feed export.

The feed export generates a Facebook tab-delimited file. You can implement the hooks described previously to customize the default transformation from Product system attributes.

Note: The cartridge that contains the hook must be in the site cartridge path (not the Business Manager cartridge path).

Example

Following is an example of how to define a hook to map product attributes to Facebook product feed attributes:

  1. In a custom cartridge, include a file package.json at the root (sibling to the "cartridge" folder containing "scripts", "templates", and the like, with this content:
    {
       "hooks": "./cartridge/scripts/hooks.json"
    }
    Note: If a custom cartridge registers other custom hook implementations, the JSON file already exists.
  2. In that custom cartridge, include a file hooks.json in "cartridge/scripts" with this content:
    {
      "hooks": [
          {
             "name": "dw.extensions.facebook.feed.transformProduct",
             "script": "./facebook.js"
          }
       ]
    }
    Note: If a custom cartridge already registers other custom hook implementations, instead of performing this step, add the object with "name" and "script" to the existing array of registered hooks, for example:
    {
       "hooks": [
          {
             "name": "some.other.hook.name",
             "script": "./some-file.js"
          }, {
             "name": "dw.extensions.facebook.feed.transformProduct",
             "script": "./facebook.js"
          }
       ]
    }
  3. In that custom cartridge, include a file facebook.js in the cartridges/scripts folder with this content:
    exports.transformProduct = function (product, facebookProduct) {
        // Map the Product "shortDescription" system attribute value to the Facebook product feed "title"
        // by default the Product "name" system attribute value is mapped to "title" so Commerce Cloud overrides this
        facebookProduct.setTitle(product.shortDescription.markup);
    
        // Map the Product "facebookDescription" custom attribute value to the Facebook product feed "description"
        // by default the Product "shortDescription" system attribute value is mapped to "description" so Commerce Cloud overrides this
        facebookProduct.setDescription(product.custom.facebookDescription);
    };

    You can put whatever code you want in the exports.transformProduct function. The code in the example is just to show how it works.

    Note: The parameters to the hook function are specified in the FacebookFeedHooks Script API documentation:
    • The first parameter is a standard dw.catalog.Product API object to be mapped
    • The second parameter is a dw.extensions.facebook.FacebookProduct API object
    • The FacebookProduct is an object representation of a row in the feed.
    • The Script API documentation for FacebookProduct specifies what attributes it contains, thus what we support to be included in a Facebook product feed.
    • There are constants available in FacebookProduct for the values that Facebook supports for columns with enumerated values, such as condition ("new", "refurbished", and "used").
    • The FacebookProduct passed to this function already has values set based on a default mapping B2C Commerce performs. A developer can set values in a FacebookProduct to override the B2C Commerce default mapping, or supply a value for a field that Commerce Cloud doesn't map by default, such as shipping weight or custom label 0-4. Consult Facebook documentation for what the fields mean, and the constraints placed on them by Facebook.
    • The hook function is only called for products that are enabled for Facebook in Business Manager. If the Enabled checkbox is checked in the Facebook cofiguration page in Business Manager, all products in the storefront catalog are included in the feed except products that are explicitly excluded using the site-specific system attribute named "FacebookEnabled". If unchecked, products to include in the feed must be explicitly included using the "FacebookEnabled" attribute.
    • The Facebook feed export job automatically skips rows that don't have the values required per Facebook documentation. If a row is skipped for this reason, its SKU is logged in the export job log. These logs are in the Impex logs area like other export logs.

    You don't have to use the script name "facebook.js" to contain the hook. However, if you already have hooks in your custom cartridge that are all implemented in an existing script file, you could add this Facebook code in that script file and not create a new one.