Hello Everyone
During the development of a prototype e-commerce website, I encountered an unexpected issue today. Specifically, the challenge lies in a method designed to take user input and apply discounts based on two criteria:
1). Validation of a correct code (represented by object keys).
2). Meeting specific total conditions for the user's order (based on $scope.total comparisons).
Below is the current code implementation:
$scope.processDiscount = function(code) {
var discountInfo = {
'FIVE': {
condition: true,
discount: 5
},
'TEN': {
condition: $scope.total > 50,
discount: 10
},
'FIFTEEN': {
condition: $scope.total > 75 && $scope.reviewCartCategories('Men\'s Footwear', 'Women\'s Footwear'),
discount: 15
}
};
for (var key in discountInfo) {
if (code === key && discountInfo[key].condition) {
$scope.applyDiscount(discountInfo, key);
};
};
};
The main concern is the large object defined within the method. Initially, I tried assigning it to a variable on the $scope, but faced the issue of it being set once and not updating with changes in the user's cart. This is because $scope.total is initially set to 0 when the controller loads, making it static.
I am seeking advice on a more efficient way to handle this situation. Any suggestions or assistance would be greatly appreciated!