There could be a number of reasons why a company might want dynamic sorting in SuiteCommerce, generally to process values often being updated, like stock, and/or to process it in terms of the currently logged-in user.
For example, sorting item data by those that are in stock, but only according to the user's currently logged-in subsidiary, by selecting "In Stock "as a sorting option.
This is undesirable/unfeasible as a solution for two main reasons:
The Profile Model's getSearchApiUrl is where the path is set up before appending params.
Even if you did overwrite it, something as simple as sorting on the PLP should be done with already processed data because it would be a performative nightmare otherwise.
Note: You CAN sort a page of a PLP simply enough, but those can only be 100 items max, and you'd be sorting whatever data was currently on the page (not the actually sorted results you should be seeing if, for example, you have multiple pages of 100)
For example, we can set up four different fields that are all the same except the end of their ID (and backend label), where we could put (for example) the subsidiary ID that we filter by.
After getting and reading the dynamic variable:
/**
* @name DynamicPLPStockSorting.Entry.js
*
* @author Anchor Group : Tony Dittus
*
* @since 2024-03-29
*
* @file Dynamically Filters the Stock Sorters to the User's Subsidiary Location's Stock
*/
define('DynamicPLPStockSorting.Entry',
[
'underscore'
],
function (
_
) {
'use strict';
return {
mountToApp: function mountToApp(container) {
const LayoutComponent = container.getComponent('Layout');
const UserProfileComponent = container.getComponent("UserProfile");
UserProfileComponent.getUserProfile().then(function (profile) {
let subsidiaryID = profile.subsidiary;
//* Reset the defaults and filter the config
SC.CONFIGURATION.sortOptions = processDynamicSortingOptionConfig(SC.CONFIGURATION.sortOptions, subsidiaryID);
SC.CONFIGURATION.sortOptionsPhone = processDynamicSortingOptionConfig(SC.CONFIGURATION.sortOptionsPhone, subsidiaryID);
SC.CONFIGURATION.sortOptionsTablet = processDynamicSortingOptionConfig(SC.CONFIGURATION.sortOptionsTablet, subsidiaryID);
//* Initial/failsafe filter and override of the sorting selector options
LayoutComponent.addToViewContextDefinition('Facets.ItemListSortSelector.View', 'options', 'array', function (context) {
context.options = _.filter(context.options, function (option) {
if (!option.className.includes('custitem_in_stock_sorting')) {
return option;
} else if (option.className.includes('custitem_in_stock_sorting' + '_sub' + subsidiaryID)) {
return option;
}
});
return context.options;
});
});
function processDynamicSortingOptionConfig(sortingConfigSet, subsidiaryID) {
return _.filter(sortingConfigSet, function (option) {
option.isDefault = false;
if (!option.id.includes('custitem_in_stock_sorting')) {
return option;
} else if (option.id.includes('custitem_in_stock_sorting' + '_sub' + subsidiaryID)) {
option.isDefault = true;
return option;
}
});
}
}
};
});
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!
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!
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.
Tagged with Troubleshooting