Skip to content

Commit

Permalink
Added config to sort products
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Moffat authored and Mark Moffat committed Mar 25, 2020
1 parent e91b440 commit ce2d6c5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
2 changes: 2 additions & 0 deletions config/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"maxQuantity": 25,
"twitterHandle": "",
"facebookAppId": "",
"productOrderBy": "date",
"productOrder": "descending",
"modules": {
"enabled": {
"shipping": "shipping-basic",
Expand Down
10 changes: 10 additions & 0 deletions config/settingsSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@
"facebookAppId": {
"type": "string"
},
"productOrderBy": {
"type": "string",
"enum": ["date", "title"],
"default": "date"
},
"productOrder": {
"type": "string",
"enum": ["descending", "ascending"],
"default": "descending"
},
"modules": {
"type": "object",
"properties": {
Expand Down
17 changes: 17 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,22 @@ const paginateData = (frontend, req, page, collection, query, sort) => {
});
};

const getSort = () => {
const config = getConfig();
let sortOrder = -1;
if(config.productOrder === 'ascending'){
sortOrder = 1;
}
let sortField = 'productAddedDate';
if(config.productOrderBy === 'title'){
sortField = 'productTitle';
}

return {
[sortField]: sortOrder
};
};

const hooker = (order) => {
const config = getConfig();

Expand Down Expand Up @@ -708,6 +724,7 @@ module.exports = {
getId,
newId,
paginateData,
getSort,
hooker,
getCountryList,
cleanAmount
Expand Down
7 changes: 4 additions & 3 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
emptyCart,
updateSubscriptionCheck,
paginateData,
getSort,
addSitemapProducts,
getCountryList
} = require('../lib/common');
Expand Down Expand Up @@ -731,7 +732,7 @@ router.get('/category/:cat/:pageNum?', (req, res) => {
}

Promise.all([
paginateData(true, req, pageNum, 'products', { _id: { $in: lunrIdArray } }),
paginateData(true, req, pageNum, 'products', { _id: { $in: lunrIdArray } }, getSort()),
getMenu(db)
])
.then(([results, menu]) => {
Expand Down Expand Up @@ -813,7 +814,7 @@ router.get('/page/:pageNum', (req, res, next) => {
const numberProducts = config.productsPerPage ? config.productsPerPage : 6;

Promise.all([
paginateData(true, req, req.params.pageNum, 'products'),
paginateData(true, req, req.params.pageNum, 'products', {}, getSort()),
getMenu(db)
])
.then(([results, menu]) => {
Expand Down Expand Up @@ -854,7 +855,7 @@ router.get('/:page?', async (req, res, next) => {
// if no page is specified, just render page 1 of the cart
if(!req.params.page){
Promise.all([
paginateData(true, req, 1, 'products', {}),
paginateData(true, req, 1, 'products', {}, getSort()),
getMenu(db)
])
.then(([results, menu]) => {
Expand Down

0 comments on commit ce2d6c5

Please sign in to comment.