Our new magic cartridge is (hopefully) running as expected, however, it's kinda boring right now. It is preconfigured and our site admins cannot change anything on it without code. This is where Custom Preferences comes in. Custom Preferences enable developers (i.e us) to make properties for modules we develop (i.e cartridges) configurable in the Business Manager!
The easiest way to create new Custom Preferences (we will discuss importing existing ones later) is through the Business Manager. Custom Preferences are created on a site (storefront) level and are then grouped under a Custom Preferences group. Accessing a siteβs custom preferences is done in the Merchant Tools section of the Business Manager.
We will start by creating a new Custom Preferences group called MagicConfig, which will hold all the custom preferences objects related to the Magic Cartridge we created yesterday:
MagicConfig is now part of the Custom Site Preference Groups
A custom preference group consists of different attributes that represent an object. SFCC allows an attribute to be part of two types of objects: System and Custom. A system object allows you to add attributes to a site-wide object that is later used in your storefront. Such objects can be Product, Coupon, Invoice, etc. In addition, System objects can also contain attributes for Site Preferences or a Service Profile. For our cartridge, we would like to add a Custom Preference that allows the site admin to set the background color for the cartridge template without needing to edit it directly or redeploy it. Any attribute that describes a Site Preference will be defined for the Site Preferences object.
To add a custom preference attribute to the Site Preferences object:
π If an object does not support customization, we will not be able to add new attributes to it or edit its grouping.
4. Click on the Attribute Definitions tab.
5. The list shown is of all of the attributes currently defined for the Site Preferences object. Click on the New button in the bottom right corner to add a new attribute.
6. Fill in the following and click Apply:
7. Youβll now see the Attribute Definition Details page where you can set additional properties to the new attribute.
Congratulations! You just created your first attribute π
Like T. S. Eliot said βHome is where one starts from,β each of our attributes must be associated with a home. or rather, a Custom Preference group. As such, before we can use our new background color attribute, we need to bind it to a group.
The MagicConfig Edit button is located under the Attributes column on the right side_
5. In the ID text box type MagicBackground and click Add. This will bind the attribute to the group, enabling us to actually use it!
We are done setting up our custom attribute in the Business Manager but before we can actually use it we need to add support for it in our Magic Cartridge template.
magic.isml
template, located at thetemplates/default
folder.isprint
β As you might have noticed we used a dynamic variable for the value of isprint
: dw.system.Site.current.preferences.custom.MagicBackground.
What does this big scary line mean?3. Upload the changes to SFCC by runningnpm run uploadCartridge
from the root folder of the cartridge.
We are now ready to test our new custom attribute!
Before and after shots (the default background is white)
And there you have it, a custom preference attribute that allows the storefront admin to change the background color of our custom cartridge without needing to use code or deployment π
If you are now thinking βJohnny, that is awesome and all, but I want to call outside sources from my cartridge to get or send data!β then patience, young grasshopper, come back tomorrow to meet the Services Framework.
As always, looking forward to your comments either here or on Twitter π