From 052005cd55c6da9e5c7e0da71d40a4e67837dbbf Mon Sep 17 00:00:00 2001 From: David Grimason Date: Fri, 7 Jun 2019 16:32:39 +0900 Subject: [PATCH] Example extension for implementing a custom result processor (PR #269) --- config/default.json | 8 ++++++- src/api/extensions/example-processor/index.js | 8 +++++++ .../processors/my-product-processor.js | 21 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/api/extensions/example-processor/index.js create mode 100644 src/api/extensions/example-processor/processors/my-product-processor.js diff --git a/config/default.json b/config/default.json index 2a1a97c4..01d9d205 100644 --- a/config/default.json +++ b/config/default.json @@ -137,7 +137,8 @@ "mailchimp-subscribe", "example-magento-api", "cms-data", - "mail-service" + "mail-service", + "example-processor" ], "extensions": { "mailchimp": { @@ -155,6 +156,11 @@ }, "targetAddressWhitelist": ["contributors@vuestorefront.io"], "secretString": "__THIS_IS_SO_SECRET__" + }, + "example-processor": { + "resultProcessors": { + "product": "my-product-processor" + } } }, "magento2": { diff --git a/src/api/extensions/example-processor/index.js b/src/api/extensions/example-processor/index.js new file mode 100644 index 00000000..47390068 --- /dev/null +++ b/src/api/extensions/example-processor/index.js @@ -0,0 +1,8 @@ +import { Router } from 'express' + +module.exports = () => { + + let exampleApi = Router() + + return exampleApi +} diff --git a/src/api/extensions/example-processor/processors/my-product-processor.js b/src/api/extensions/example-processor/processors/my-product-processor.js new file mode 100644 index 00000000..17b484ce --- /dev/null +++ b/src/api/extensions/example-processor/processors/my-product-processor.js @@ -0,0 +1,21 @@ +class MyProductProcessor { + constructor (config, request) { + this._request = request + this._config = config + } + + process (productList) { + // Product search results can be modified here. + // For example, the following would add a paragraph to the short description of every product + // + // for (const prod of productList) { + // prod._source.short_description = prod._source.short_description + '

Free Shipping Today Only!

' + // } + // + // For a real-life example processor, see src/platform/magento2/tax.js + // For more details and another example, see https://docs.vuestorefront.io/guide/extensions/extensions-to-modify-results.html + return productList + } +} + +module.exports = MyProductProcessor