{"id":378,"date":"2021-03-25T20:22:44","date_gmt":"2021-03-26T04:22:44","guid":{"rendered":"http:\/\/www.antunkarlovac.com\/blog\/?p=378"},"modified":"2021-05-04T02:36:41","modified_gmt":"2021-05-04T10:36:41","slug":"using-custom-site-preferences-in-sfcc","status":"publish","type":"post","link":"https:\/\/www.antunkarlovac.com\/blog\/2021\/03\/25\/using-custom-site-preferences-in-sfcc\/","title":{"rendered":"Using Custom Site Preferences in SFCC"},"content":{"rendered":"<p>This is a guide for creating custom site preferences for use with Salesforce Commerce Cloud. Site preferences let users of Business Manger (BM) define settings that are used in code. For information visit\u00a0<span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;korucaredoula&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:268476,&quot;5&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:[null,2,0]},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;6&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:[null,2,0]},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;7&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:[null,2,0]},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;8&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:[null,2,0]},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;10&quot;:2,&quot;14&quot;:[null,2,3700253],&quot;15&quot;:&quot;Arial&quot;,&quot;21&quot;:1}\" data-sheets-hyperlink=\"http:\/\/korucaredoula.com\/\" data-sheets-hyperlinkruns=\"[null,0,&quot;http:\/\/korucaredoula.com\/&quot;]\"><a class=\"in-cell-link\" href=\"http:\/\/korucaredoula.com\/\" target=\"_blank\">korucaredoula<\/a><\/span> .<\/p>\n<p>You can see available site preferences in <strong>Merchant Tools \/ Site Preferences \/ Custom Preferences<\/strong>. They are organized by groups (e.g. per cartridge).<\/p>\n<p><strong>Creating New Preferences<\/strong><br \/>\nFirst create a group for your set of preferences in BM by going to <strong>Merchant Tools \/ Site Preferences \/ Custom Preferences<\/strong> and clicking <strong>New<\/strong>. If you simply need to add a new preference to an existing group, skip this step.<\/p>\n<p>The next steps are a bit roundabout. Firstly, you have to go to <strong>Administration &gt; Site Development &gt; System Object Types<\/strong>, and select <strong>SitePreferences<\/strong> from the list. To add preference to your new group, for each one you&#8217;ll do the following:<\/p>\n<ol>\n<li>First create the attribute: Select the <strong>Attribute Definitions<\/strong> tab, click <strong>New<\/strong>, and define your new site preference (attribute), then click <strong>Apply<\/strong>. Use an ID that will be unique, since that will be the identifier you use for your preference in code.<\/li>\n<li>Then add it to your group: Go back to <strong>Site Preferences &#8211; Attribute Definitions<\/strong>, select the <strong>Attribute Grouping<\/strong> tab, find the group you created (it may be at the bottom of the list), and then click <strong>Edit<\/strong>. Search for your newly-created attribute, and click <strong>Add<\/strong>.<\/li>\n<\/ol>\n<p>When you create the attribute, you can select the Value Type &#8211; boolean, string, date, enum, etc., and define options and select a default option, if applicable:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-382\" src=\"http:\/\/www.antunkarlovac.com\/blog\/wp-content\/uploads\/2021\/03\/enum_options.jpg\" alt=\"enum_options\" width=\"537\" height=\"348\" srcset=\"https:\/\/www.antunkarlovac.com\/blog\/wp-content\/uploads\/2021\/03\/enum_options.jpg 537w, https:\/\/www.antunkarlovac.com\/blog\/wp-content\/uploads\/2021\/03\/enum_options-300x194.jpg 300w\" sizes=\"(max-width: 537px) 100vw, 537px\" \/><\/p>\n<p><strong>Accessing Preferences in Code<\/strong><br \/>\nOnce you have defined your preference, you can access it in JavaScript like so:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar System = require('dw\/system');\r\nvar site = System.Site.getCurrent();\r\nvar customPreferenceValue = site.getCustomPreferenceValue('myPreferenceID').getValue();\r\n<\/pre>\n<p>NOTE: If your preference has a simple type (e.g. string, boolean) just use site.getCustomPreferenceValue(&#8216;myPreferenceID&#8217;) &#8211; you don&#8217;t need the getValue() method.<\/p>\n<p>A tidy way to organize this is to have a helper file in your cartridge:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/ FILE: int_mycartridge\/cartridge\/config\/myCartridgePreferences.js\r\nvar System = require('dw\/system');\r\n\r\nfunction getPreferences() {\r\n    var prefs = {};\r\n    var site = System.Site.getCurrent();\r\n    prefs.myPreferenceID = site.getCustomPreferenceValue('myPreferenceID').getValue();\r\n    \/\/ ... other preferences\r\n    return prefs;\r\n}\r\n\r\nmodule.exports = getPreferences();\r\n<\/pre>\n<p>&#8230; which you can then use in a JS file:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar prefs = require('~\/cartridge\/config\/myCartridgePreferences');\r\n\/\/ prefs.myPreferenceID is now available\r\n<\/pre>\n<p>&#8230; or an ISML template:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;isscript&gt;\r\n    var prefs = require('~\/cartridge\/config\/myCartridgePreferences');\r\n    \/\/ prefs.myPreferenceID is now available\r\n&lt;\/isscript&gt;\r\n<\/pre>\n<p><strong>Exporting Preferences<\/strong><br \/>\nAfter manually setting-up your preferences in BM, export the XML files and store in your cartridge so that they are in source control. First you&#8217;ll need to export the meta data.<\/p>\n<ol>\n<li>Go to <strong>Administration &gt; Site Development &gt; Import &amp; Export<\/strong><\/li>\n<li>Click <strong>Export<\/strong> under Meta Data<\/li>\n<li>Choose System object type definitions and click <strong>Export<\/strong><\/li>\n<li>Download the XML file locally<\/li>\n<\/ol>\n<p>Next create the following file in your cartridge (assuming your cartridge&#8217;s folder is int_my_cartridge):<\/p>\n<p>int_my_cartridge\/metadata\/meta\/system-objecttype-extensions.xml<\/p>\n<p>You&#8217;ll need to <em>manually<\/em> copy\/paste just the content from the export that&#8217;s relevant to your file. It&#8217;ll look something like this, where &#8220;&#8230;.&#8221; will have details about your attribute:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;metadata xmlns=&quot;http:\/\/www.demandware.com\/xml\/impex\/metadata\/2006-10-31&quot;&gt;\r\n    &lt;type-extension type-id=&quot;SitePreferences&quot;&gt;\r\n        &lt;custom-attribute-definitions&gt;\r\n            &lt;attribute-definition attribute-id=&quot;myPreferenceID&quot;&gt;\r\n                ....\r\n            &lt;\/attribute-definition&gt;\r\n            &lt;attribute-group group-id=&quot;MyGroup&quot;&gt;\r\n                &lt;display-name xml:lang=&quot;x-default&quot;&gt;My Group&lt;\/display-name&gt;\r\n                &lt;attribute attribute-id=&quot;myPreferenceID&quot;\/&gt;\r\n            &lt;\/attribute-group&gt;\r\n        &lt;\/group-definitions&gt;\r\n    &lt;\/type-extension&gt;\r\n&lt;\/metadata&gt;\r\n<\/pre>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>This is a guide for creating custom site preferences for use with Salesforce Commerce Cloud. Site preferences let users of Business Manger (BM) define settings that are used in code. For information visit\u00a0korucaredoula . You can see available site preferences in Merchant Tools \/ Site Preferences \/ Custom Preferences. They are organized by groups (e.g. &hellip; <a href=\"https:\/\/www.antunkarlovac.com\/blog\/2021\/03\/25\/using-custom-site-preferences-in-sfcc\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using Custom Site Preferences in SFCC<\/span><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/posts\/378"}],"collection":[{"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/comments?post=378"}],"version-history":[{"count":10,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/posts\/378\/revisions"}],"predecessor-version":[{"id":406,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/posts\/378\/revisions\/406"}],"wp:attachment":[{"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/media?parent=378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/categories?post=378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.antunkarlovac.com\/blog\/wp-json\/wp\/v2\/tags?post=378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}