Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
Added URL Dispatcher URL mapping feature vuestorefront/vue-storefront…
Browse files Browse the repository at this point in the history
  • Loading branch information
pkarw committed Feb 12, 2019
1 parent e73c40e commit 64f4b58
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/adapters/magento/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const util = require('util');
const request = require('request');
const _slugify = require('../../helpers/slugify')

const _normalizeExtendedData = function (result, generateUrlKey = true) {
const _normalizeExtendedData = function (result, generateUrlKey = true, config = null) {
if (result.custom_attributes) {
for (let customAttribute of result.custom_attributes) { // map custom attributes directly to document root scope
result[customAttribute.attribute_code] = customAttribute.value;
Expand All @@ -16,7 +16,11 @@ const _normalizeExtendedData = function (result, generateUrlKey = true) {
if (generateUrlKey) {
result.url_key = _slugify(result.name) + '-' + result.id;
}
result.slug = result.url_key;
if (config.seo.useUrlDispatcher) {
result.slug = config.seo.categorySlugMapper(result)
} else {
result.slug = result.url_key;
}
return result
}

Expand Down Expand Up @@ -51,13 +55,14 @@ class CategoryAdapter extends AbstractMagentoAdapter {
}

_addSingleCategoryData(item, result) {
item = Object.assign(item, _normalizeExtendedData(result, this.generateUniqueUrlKeys));
item = Object.assign(item, _normalizeExtendedData(result, this.generateUniqueUrlKeys, this.config));
}

_extendSingleCategory(rootId, catToExtend) {
const generateUniqueUrlKeys = this.generateUniqueUrlKeys
const config = this.config
return this.api.categories.getSingle(catToExtend.id).then(function(result) {
Object.assign(catToExtend, _normalizeExtendedData(result, generateUniqueUrlKeys))
Object.assign(catToExtend, _normalizeExtendedData(result, generateUniqueUrlKeys, config))
logger.info(`Subcategory data extended for ${rootId}, children object ${catToExtend.id}`)
}).catch(function(err) {
logger.error(err)
Expand Down Expand Up @@ -86,8 +91,11 @@ class CategoryAdapter extends AbstractMagentoAdapter {
if (!item.url_key || this.generateUniqueUrlKeys) {
item.url_key = _slugify(item.name) + '-' + item.id
}
item.slug = item.url_key;

if (this.config.seo.useUrlDispatcher) {
item.slug = this.config.seo.categorySlugMapper(item)
} else {
item.slug = item.url_key;
}

if (this.extendedCategories) {

Expand Down
11 changes: 9 additions & 2 deletions src/adapters/magento/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,15 @@ class ProductAdapter extends AbstractMagentoAdapter {
if (cat != null) {
resolve({
category_id: cat.id,
name: cat.name
name: cat.name,
slug: cat.slug,
path: cat.url_path
})
} else {
resolve({
category_id: catId
category_id: catId,
slug: cat.slug,

This comment has been minimized.

Copy link
@rain2o

rain2o Feb 18, 2019

Contributor

@pkarw If you are inside this else it means cat is null, but you are trying to use props on cat which throws an error.

This comment has been minimized.

Copy link
@pkarw

pkarw Feb 22, 2019

Author Contributor

Positive, will be fixed :)

path: cat.url_path
});
}
});
Expand All @@ -512,6 +516,9 @@ class ProductAdapter extends AbstractMagentoAdapter {
Promise.all(catPromises).then((values) => {
if(this.category_sync) // TODO: refactor the code above to not get cache categorylinks when no category_sync required
item.category = values; // here we get configurable options
if (this.config.seo.useUrlDispatcher) {
item.slug = this.config.seo.productSlugMapper(item)
}
resolve(item)
});
}
Expand Down
23 changes: 23 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
const _slugify = require('./helpers/slugify')

module.exports = {

seo: {
useUrlDispatcher: JSON.parse(process.env.SEO_USE_URL_DISPATCHER) || false,

This comment has been minimized.

Copy link
@rain2o

rain2o Feb 18, 2019

Contributor

If the environment variable SEO_USE_URL_DISPATCHER hasn't been set then JSON.parse will throw an error. It looks like later in this file there is a conditional statement inside JSON.parse that seems to resolve this issue.

This comment has been minimized.

Copy link
@pkarw

pkarw Feb 22, 2019

Author Contributor

OK, fixed

productSlugMapper: (product) => {
let destSlug = ''
if (product.category && product.category.length > 0) {
const firstCat = product.category[0]
destSlug = (firstCat.path ? (firstCat.path) : _slugify(firstCat.name)) + '/' + (product.slug ? product.slug : _slugify(product.name + '-' + product.id))
} else {
destSlug = (product.slug ? product.slug : _slugify(product.name + '-' + product.id))
}
destSlug += '.html'
console.log('Dest. product slug = ', destSlug)
return destSlug
},
categorySlugMapper: (category) => {
const destSlug = (category.url_path ? category.url_path + '/': '') + category.url_key
console.log('Dest. cat slug = ', destSlug)
return destSlug
},
},

magento: {
url: process.env.MAGENTO_URL || 'http://magento2.demo-1.divante.pl/rest/',
consumerKey: process.env.MAGENTO_CONSUMER_KEY || 'alva6h6hku9qxrpfe02c2jalopx7od1q',
Expand Down
1 change: 1 addition & 0 deletions src/test_fullreindex.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export MAGENTO_CONSUMER_KEY=byv3730rhoulpopcq64don8ukb8lf2gq
export MAGENTO_CONSUMER_SECRET=u9q4fcobv7vfx9td80oupa6uhexc27rb
export MAGENTO_ACCESS_TOKEN=040xx3qy7s0j28o3q0exrfop579cy20m
export MAGENTO_ACCESS_TOKEN_SECRET=7qunl3p505rubmr7u1ijt7odyialnih9
export SEO_USE_URL_DISPATCHER=1

echo 'Default store - in our case United States / en'
export MAGENTO_URL=http://demo-magento2.vuestorefront.io/rest
Expand Down
1 change: 1 addition & 0 deletions src/test_multistore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export MAGENTO_CONSUMER_KEY=byv3730rhoulpopcq64don8ukb8lf2gq
export MAGENTO_CONSUMER_SECRET=u9q4fcobv7vfx9td80oupa6uhexc27rb
export MAGENTO_ACCESS_TOKEN=040xx3qy7s0j28o3q0exrfop579cy20m
export MAGENTO_ACCESS_TOKEN_SECRET=7qunl3p505rubmr7u1ijt7odyialnih9
export SEO_USE_URL_DISPATCHER=1

echo 'German store - de'
export MAGENTO_URL=http://demo-magento2.vuestorefront.io/rest/de
Expand Down

0 comments on commit 64f4b58

Please sign in to comment.