Use Environment.addTracker to Add Events Across Views in SuiteCommerce

You may want specific logic to execute often across an entire SuiteCommerce Advanced (SCA) application. A robust way to add this logic is by using the Environment.addTracker frontend extensibility API.

In combination with Environment.triggerEvent, a SuiteCommerce developer can add code that can be triggered by various sources across the application, such as within a cart listener function, as shown below.

This could allow a site to recalculate cart details whenever an address is changed or check for NetSuite promotions whenever an item is added to a cart. It could even be as simple as displaying an alert whenever a quantity is changed in the cart or on a product page.

Step 1: Add the Tracker Function

Start by adding the tracker.

//be sure to load the Environment component
Environment.addTracker({
    someTriggerString: function myFunction (args) {
        //add your logic here
    },
});
NOTE: The function you used can be a new, unique function. But, it can also be an existing function on some Backbone View or Model.

Step 2: Establish the Tracker Function Trigger

Your tracker can be triggered by referencing the key you used while declaring it. In this case, “someTriggerString"

//In the below example we trigger our tracker function to run whenever an address
//changes on the profile or cart. 
var profile = ProfileModel.getInstance();
profile.get("addresses").on("change", function (address) {
    Environment.triggerEvent("someTriggerString", address);
});
let Cart = container.getComponent("Cart");
Cart.on("afterSetShipAddress", function (address) {
    Cart.getShipAddress().then(function () {
        var selectedAddress = profile.get("addresses").get(address);
        Environment.triggerEvent("someTriggerString", selectedAddress);
    });
});

Author: Sam Gagliardi

Got stuck on a step in this article?

We like to update our blogs and articles to make sure they help resolve any troubleshooting difficulties you are having. Sometimes, there is a related feature to enable or a field to fill out that we miss during the instructions. If this article didn't resolve the issue, please use the chat and let us know so that we can update this article!


FREE SuiteCommerce Book

If you liked this article, you'll LOVE our book on SuiteCommerce! Order the free SuiteCommerce book today, and we'll even pay for shipping!

Oracle NetSuite Alliance Partner & Commerce Partner

If you have general questions about SuiteCommerce or more specific questions about how our team can support your business as you implement NetSuite or SuiteCommerce, feel free to contact us anytime. Anchor Group is a certified Oracle NetSuite Alliance Partner and Commerce Partner equipped to handle all kinds of NetSuite and SuiteCommerce projects, large or small!

We are a premium SuiteCommerce agency that creates powerful customer portals. Unlike our competitors, we have already solved your problems.

 
 

Want to keep learning?

Our team of NetSuite professionals has written articles on a wide variety of NetSuite topics, from SuiteCommerce tips, to recommended NetSuite solutions, to available support services, and more! 

Your cart