Skip to content

Instantly share code, notes, and snippets.

@jparbros
Created September 21, 2018 18:32
Show Gist options
  • Save jparbros/a740448e818457216efcd5cf62a0d0c4 to your computer and use it in GitHub Desktop.
Save jparbros/a740448e818457216efcd5cf62a0d0c4 to your computer and use it in GitHub Desktop.
importPackage( dw.system );
importPackage( dw.customer );
importPackage( dw.util );
importPackage( dw.catalog );
importPackage( dw.object );
function GetCategoriesOverrides(Category)
{
if (empty(Category)) {
return;
}
// Check if a custom Category Overrides object exists for this category
var catOverrideObject = CustomObjectMgr.getCustomObject('CategoryOverride', Category.ID);
var template = Category.template;
if (!empty(catOverrideObject)) {
var overrideJSON = catOverrideObject.custom.overrideData;
overrideJSON = overrideJSON.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
//remove non-printable and other non-valid JSON chars
overrideJSON = overrideJSON.replace(/[\u0000-\u0019]+/g,"");
var templateOverride = JSON.parse(overrideJSON).renderingTemplate.path;
if (!empty(templateOverride)) template = templateOverride;
}
// Output the new Rendering Template
return template;
}
function GetDefaultMetaDescription( Category )
{
var Resource = require('dw/web/Resource');
var cat = Category;
if (empty(cat)) {
return ;
}
var displayName = cat.displayName;
var desc = cat.pageDescription;
if (desc) return;
var ArrayList = require('dw/util/ArrayList'),
categories = new ArrayList();
if (cat) {
while (cat.parent) {
if (cat.online) {
categories.addAt(0, cat);
}
if (cat) {
cat = cat.parent;
}
}
}
var rootCategoryName = categories[0] ? categories[0].displayName.toLowerCase() : '';
var defaultMetaDesc = Resource.msgf('metadescriptions.default', 'metadescriptions', '', displayName);
var metaDesc = Resource.msgf('metadescriptions.' + rootCategoryName, 'metadescriptions', defaultMetaDesc, displayName);
return metaDesc;
}
module.export = {
GetCategoriesOverrides: GetCategoriesOverrides,
GetDefaultMetaDescription: GetDefaultMetaDescription
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment