When a customer login to the website and select the category branded products needed to be displayed, for that we will initially check whether the customer has any brand or not.
If no brand selected, then we will display a message on the page(Sorry, you don’t have any specific brand items. Please contact us for any further information). If a brand is selected, then we will check the selected brand. Then we will filter the items in the category based on the customers brand. So only those items will be displayed.
So by default, we will add all the branded items to the branded category. Only on the website, the products will be displayed based on the brand of the logged-in customer.
- create a field in the item record for the brand, it containing a list of brands
- In the customer record create brand field for selecting these brands
- Create a commerce category named brand and then add all the items having the brand to that category.
- To add all the items to this category, we can perform item importing to the category
- Create an extension to manage the brand functionality in SCA
- For the logged-out customers we need to display an informative message “Please logged in to see the branded products“
- In the case of the customer having the brand , we will filter the items in the brand category based on the brand on the customer record and show the items having a matching brand in to the webstore
- For the customers who have no brand is selected on the customer record will display an informative message “Sorry, you don’t have any specific brand items. Please contact us for any further information” and add the contact us page link along with the message.
define(
'JJ.BrandCategory.BrandCategory'
, [
'JJ.BrandCategory.BrandCategory.View',
'Facets.Browse.View',
'Profile.Model',
'jj_brandcategory_brandcategory.tpl',
'Facets.FacetedNavigationItemCategory.View'
]
, function (
BrandCategoryView,
FacetsBrowseView,
ProfileModel,
jj_brandcategory_brandcategory_tpl,
FacetsFacetedNavigationItemCategoryView
) {
'use strict';
return {
mountToApp: function mountToApp(container) {
var layout = container.getComponent('Layout');
if (layout) {
_.extend(FacetsFacetedNavigationItemCategoryView.prototype, {
getContext: _.wrap(FacetsFacetedNavigationItemCategoryView.prototype.getContext, function (fn) {
var originalRet = fn.apply(this, _.toArray(arguments).slice(1));
if (originalRet.parentName === "Brand") {
originalRet.showFacet = true;
}
return originalRet;
})
});
_.extend(FacetsBrowseView.prototype, {
template: jj_brandcategory_brandcategory_tpl,
initialize: _.wrap(FacetsBrowseView.prototype.initialize, function (fn) {
try {
var context = fn.apply(this, _.toArray(arguments).slice(1));
// to show items based on brand
var brand, allBrand;
var brandArray = [];
var currentBrand = [];
var urlOfCategory = this.translator.categoryUrl;
if (urlOfCategory === "/brand") {
var profile = ProfileModel.ProfileModel ? ProfileModel.ProfileModel.getInstance() : ProfileModel.getInstance();
var profilelogged = profile.attributes.isLoggedIn;
if (profilelogged === "T") {
var customFieldLength = profile.get('customfields').length;
for (var i = 0; i < customFieldLength; i++) {
var customfields = profile.get('customfields');
if (customfields[i].name === "custentity_jj_brand") {
brand = customfields[i].value;
allBrand = customfields[i].html;
}
}
brandArray = brand.split("");
if (!!brand) {
var tempDiv = document.createElement('div');
tempDiv.innerHTML = allBrand;
// Find the element with the specified data-name attribute
var element = tempDiv.querySelector('[data-name="custentity_jj_brand"]');
if (element) {
var dataOptions = element.getAttribute('data-options');
var options;
try {
options = JSON.parse(dataOptions.replace(/"/g, '"'));
} catch (error) {
console.error('Error parsing JSON:', error);
}
// Find the option with value brand
for (var i = 0; i < brandArray.length; i++) {
var optionWithText = options.find(option => option.value === brandArray[i]);
// Check if the option is found
if (optionWithText) {
currentBrand.push(optionWithText.text);
}
}
}
var brandString = currentBrand.join(',');
// Replace single quotes with %27 and hyphens with ~
var brandString = brandString.replace(/'/g, '%27').replace(/-/g, '~');
// Adding the custitem1 property in the data object to show the brand wise item
this.model.options.data.custitem1 = brandString;
}
}
}
} catch (ex) {
console.log('Brand section error', ex);
}
}),
getContext: _.wrap(FacetsBrowseView.prototype.getContext, function (fn) {
var originalRet = fn.apply(this, _.toArray(arguments).slice(1));
try {
var idOfCategory = this.model.get('category').get('internalid');
var brand;
if (idOfCategory === "1144") {
originalRet.isLoggedOut = false;
var profile = ProfileModel.ProfileModel ? ProfileModel.ProfileModel.getInstance() : ProfileModel.getInstance();
var profilelogged = profile.attributes.isLoggedIn;
if (profilelogged != "T") {
originalRet.showItems = false;
originalRet.isLoggedOut = true;
}
else {
var customFieldLength = profile.get('customfields').length;
for (var i = 0; i < customFieldLength; i++) {
var customfields = profile.get('customfields');
if (customfields[i].name === "custentity_jj_brand") {
brand = customfields[i].value;
}
}
if (!brand) {
originalRet.showInfoMessage = true;
originalRet.showItems = false;
}
}
}
} catch (ex) {
console.log('Brand section error', ex);
}
return originalRet;
}),
});
}
}
};
});