Added config to sort products

master
Mark Moffat 2020-03-25 15:43:45 +10:30
parent e91b44074a
commit ce2d6c5bd0
4 changed files with 33 additions and 3 deletions

View File

@ -31,6 +31,8 @@
"maxQuantity": 25,
"twitterHandle": "",
"facebookAppId": "",
"productOrderBy": "date",
"productOrder": "descending",
"modules": {
"enabled": {
"shipping": "shipping-basic",

View File

@ -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": {

View File

@ -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();
@ -708,6 +724,7 @@ module.exports = {
getId,
newId,
paginateData,
getSort,
hooker,
getCountryList,
cleanAmount

View File

@ -17,6 +17,7 @@ const {
emptyCart,
updateSubscriptionCheck,
paginateData,
getSort,
addSitemapProducts,
getCountryList
} = require('../lib/common');
@ -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]) => {
@ -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]) => {
@ -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]) => {