Adding a Custom Popup for Cart Attributes in Salesforce Commerce Cloud (SFRA)
In Salesforce Commerce Cloud (SFCC) with the SFRA architecture, there are scenarios where business teams want to capture extra input from customers during the shopping experience.
For example, let’s say we want to capture a custom attribute weekAttributeAk when a shopper clicks the Add to Cart button. Instead of directly adding the product to the cart, we’ll display a pop-up, let the customer choose a value (like 2 Weeks, 3 Weeks, etc.), and then store this data with the order once checkout completes.
In this article, I’ll walk you through the full implementation
Step 1: Extending the Add to Cart Templates
We need to inject a popup component into both the product add to cart button and the global add to cart button.
Get SelfCodedMindset’s stories in your inbox
Join Medium for free to get updates from this writer.
First, extend the addToCartProduct.isml template. This includes loading a custom JS file and adding a reference to the popup template.
#!templates\default\product\components\addToCartProduct.isml
<isscript>
var assets = require('*/cartridge/scripts/assets');
assets.addJs('js/cartClick.js');
</isscript>
<isinclude template="product/components/customPopup" />
<div class="row cart-and-ipay">
<div class="col-sm-12">
<input type="hidden" class="add-to-cart-url" value="${pdict.addToCartUrl}">
<button class="add-to-cart btn btn-primary"
data-toggle="modal" data-target="#chooseBonusProductModal"
data-pid="${product.id}"
id="addToCartButton"
${!product.readyToOrder || !product.available ? "disabled" : ""}>
<i class="fa fa-shopping-bag"></i>
${Resource.msg('button.addtocart', 'common', null)}
</button>
<isinclude template="product/components/addToCartButtonExtension"/>
</div>
</div>Then, extend the addToCartGlobal.isml template in a similar way.
#!templates\default\product\components\addToCartGlobal.isml
<isscript>
var assets = require('*/cartridge/scripts/assets');
assets.addJs('js/cartClick.js');
</isscript>
<isinclude template="product/components/customPopup" />
<div class="row cart-and-ipay">
<div class="col-sm-12">
<input type="hidden" class="add-to-cart-url" value="${pdict.addToCartUrl}">
<button class="add-to-cart-global btn btn-primary" data-pid="${product.id}"
id="addToCartButton"
${!product.readyToOrder || !product.available ? "disabled" : ""}>
<i class="fa fa-shopping-bag"></i>
<isif condition="${product.productType === 'set' || product.productType === 'bundle'}">
${Resource.msg('button.addalltocart', 'common', null)}
<iselse>
${Resource.msg('button.addtocart', 'common', null)}
</isif>
</button>
</div>
</div>Both templates include the custom popup template.
Step 2: Creating the Custom Popup Template
Next, create the popup UI where the shopper can select the number of weeks. This template also exposes a route (Cart-StoreWeek) that will be used to store the chosen value in the session.
#!templates\default\product\components\customPopup.isml
<script>
var routes = {
'StoreWeek': '${URLUtils.url(https://www.medium.com/"Cart-StoreWeek")}'
};
</script>
<isscript>
var assets = require('*/cartridge/scripts/assets');
assets.addCss('css/customPopup.css');
</isscript>
<div class="weekPopup">
<select id="weekInput">
<option value="2 Weeks">2 Weeks</option>
<option value="3 Weeks">3 Weeks</option>
<option value="4 Weeks">4 Weeks</option>
</select>
<button class="submitWeekDetail">Submit</button>
</div>Step 3: JavaScript Handler for Cart Click
Now we need client-side logic to:
- Intercept the add-to-cart click.
- Show the popup.
- Capture the value and store it via AJAX.
- Re-trigger the original add-to-cart event after storing the value.
#!client\default\js\cartClick.js
'use strict';
var originalHandlers = {};
function turnOffAndStore(selector, eventType) {
// Get all event handlers for the element
var element = $(selector);
var events = $._data(element[0], "events");
if (events && events[eventType]) {
// Store the original handlers
originalHandlers[selector + "_" + eventType] =
events[eventType].slice();
// Turn off the event
element.off(eventType);
}
}
function turnBackOn(selector, eventType) {
var element = $(selector);
var key = selector + "_" + eventType;
if (originalHandlers[key]) {
// Re-attach all original handlers
originalHandlers[key].forEach(function (handler) {
element.on(
eventType,
handler.selector,
handler.data,
handler.handler,
);
});
}
}
$("#addToCartButton").on("click", function (event) {
event.stopPropagation();
event.preventDefault();
$(".weekPopup").addClass("show");
});
$('.submitWeekDetail').on('click', function(event) {
// $('body').off('click', '#addToCartButton');
turnOffAndStore("#addToCartButton", "click");
$('.weekPopup').removeClass('show');
$.ajax({
url: routes.StoreWeek,
type: "POST",
data: { week: $("#weekInput").val() },
success: function (response) {
// Handle success response if needed
console.log("Week stored successfully:", response);
},
error: function (error) {
// Handle error response if needed
console.error("Error storing week:", error);
},
});
$("button.add-to-cart, button.add-to-cart-global").trigger("click");
turnBackOn("#addToCartButton", "click");
});
The logic here ensures the default add-to-cart action is delayed until after the shopper’s selection is captured.
Step 4: Extending the Cart Controller
On the server side, extend the Cart.js controller with a new route (Cart-StoreWeek). This route receives the selected week value via AJAX and stores it in the session custom object.
#!controllers\Cart.js
'use strict';
var server = require('server');
var base = module.superModule;
server.extend(base);
server.post('StoreWeek', function (req, res, next) {
var customSession = request.getSession().custom;
var week = req.form.week;
customSession.week = week;
res.json({
success: true,
week: customSession.week
});
next();
});
module.exports = server.exports();
Step 5: Extending the Checkout Process
Finally, extend the CheckoutServices.js controller to carry the session value into the final order. When the order is placed, the custom attribute weekAttributeAk is written to the order record.
#!controllers\CheckoutServices.js
'use strict';
var server = require('server');
var base = module.superModule;
server.extend(base);
server.replace('PlaceOrder', server.middleware.https, function (req, res, next) {
//... existing code
// Creates a new order.
var order = COHelpers.createOrder(currentBasket);
if (!order) {
res.json({
error: true,
errorMessage: Resource.msg('error.technical', 'checkout', null)
});
return next();
}
var currentSession = request.getSession().custom;
Transaction.wrap(function () {
order.custom.weekAttributeAk = currentSession.week;
});
// rest of code...
return next();
});
module.exports = server.exports();How It Works
- Shopper clicks “Add to Cart.”
- Instead of immediately adding, a popup appears asking for the week selection.
- The selection is saved in the shopper’s session (
customSession.week). - The add-to-cart action proceeds as usual.
- At checkout, the order is created, and the session value is written into the order’s custom attribute
weekAttributeAk.
Why This Approach Works
- Decoupled logic: We don’t modify the core add-to-cart behavior, but instead extend it.
- Session storage: Capturing values in the session makes it easy to persist across the flow.
- Order customization: The final order carries the custom value without major changes to checkout logic.
This pattern is reusable whenever you need to prompt for extra attributes at add-to-cart time (like engraving text, delivery preferences, or subscription options).
Final Thoughts
With a little frontend and backend extension, SFCC SFRA gives you the flexibility to capture custom attributes during shopping flows. This popup-based approach is lightweight, user-friendly, and integrates seamlessly into the checkout lifecycle.





