Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ In `db` section contained in `config.js` file you should adjust some info about
Please execute indexers one by one with:
1. attributes: `nodejs cli.js attributes`
2. categories: `nodejs cli.js categories`
3. products: `nodejs cli.js products`
3. tags: `nodejs cli.js tags`
4. products: `nodejs cli.js products`

and that's it.

Expand Down
8 changes: 7 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const logger = require('./src/common/logger')
const productImporter = require('./src/importer/products')
const categoryImporter = require('./src/importer/categories')
const attributeImporter = require('./src/importer/attributes')
const tagsImporter = require('./src/importer/tags')
const WooCommerceAPI = require('woocommerce-api');
const elasticsearch = require('elasticsearch')

Expand All @@ -26,6 +27,9 @@ const connector = () => {
})
}

program.command('tags').option('-p, --page', 'current page').option('-P, --pages', 'pages')
.action(cmd => { tagsImporter.importer({ config: config, elasticClient: client, apiConnector: connector, logger }).importTags() })

program.command('attributes').option('-p, --page', 'current page').option('-P, --pages', 'pages')
.action(cmd => { attributeImporter.importer({ config: config, elasticClient: client, apiConnector: connector, logger }).importAttributes() })

Expand All @@ -35,7 +39,9 @@ program.command('products')
.action((cmd) => { productImporter.importer({ config: config, elasticClient: client, apiConnector: connector, logger: logger, page: cmd.page, perPage: cmd.perPage}).importProducts() })

program.command('categories')
.action(cmd => { categoryImporter.importer({ config: config, elasticClient: client, apiConnector: connector, logger }).importCategories() })
.option('--perPage <n>', 'per page', parseInt)
.option('--page <n>', 'current page', parseInt)
.action(cmd => { categoryImporter.importer({ config: config, elasticClient: client, apiConnector: connector, logger, page: cmd.page, perPage: cmd.perPage }).importCategories() })

program
.on('command:*', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/importer/categories.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const categoryTemplate = require('../templates/category')
const sendToElastic = require('../common/sendToElastic')

const importer = ({ config, elasticClient, apiConnector, logger }) => {
apiConnector(config).getAsync('products/categories?order_by=id').then(
const importer = ({ config, elasticClient, apiConnector, logger, page = 1, perPage = 20 }) => {
apiConnector(config).getAsync(`products/categories?order_by=id&page=${page}&per_page=${perPage}`).then(
(result) => {
let body = result.toJSON().body
let array = JSON.parse(body)
Expand Down
1 change: 0 additions & 1 deletion src/importer/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const importer = ({ config, elasticClient, apiConnector, logger, page = 1, perPa
let body = chunk.toJSON().body
let products = JSON.parse(body)
if (!Array.isArray(products) || products.length===0) {
console.log(products)
logger.info(`There are no products on page ${page}`)
return;
}
Expand Down
46 changes: 46 additions & 0 deletions src/importer/tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const config = require('../../config')
const WooCommerceAPI = require('woocommerce-api');
const tagsTemplate = require('../templates/tag')
const sendToElastic = require('../common/sendToElastic')

const connector = () => {
let { host, protocol } = config.woo.api;

return new WooCommerceAPI({
url: `${protocol}://${host}`,
consumerKey: config.woo.api.auth.consumer_key,
consumerSecret: config.woo.api.auth.consumer_secret,
wpAPI: true,
version: 'wc/v1'
})
}

const importer = ({ config, elasticClient, apiConnector, logger }) => {

connector().getAsync('products/tags?per_page=100').then(
(result) => {
let body = result.toJSON().body
let tags = JSON.parse(body)

const tagsAsAttributeOptions = tags.map( tag => ({
value: tag.id,
label: tag.name
}))

const attribute = tagsTemplate.fill(tagsAsAttributeOptions)
sendToElastic(attribute, 'attribute', {config, elasticClient, logger})

}).catch(error => logger.info(error))

function importTags() {
logger.info('tags are being imported...')
}

return {
importTags
}
}

module.exports = Object.freeze({
importer
})
2 changes: 1 addition & 1 deletion src/templates/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const fill = async (source) => {
"used_in_product_listing": 0,
"used_for_sort_by": 0,
"is_configurable": true,
"apply_to": "simple,grouped,configurable",
"apply_to": ['simple', 'grouped', 'configurable'],
"is_visible_in_advanced_search": false,
"position": 0,
"is_wysiwyg_enabled": false,
Expand Down
39 changes: 27 additions & 12 deletions src/templates/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,39 @@ const connector = () => {
})
}

const extractSubcategories = async (parent_id) => {
const extractSubcategories = async (parent_id, level = 3) => {

let result = await connector().getAsync(`products/categories?parent=${parent_id}`)
let parsed = JSON.parse(result.toJSON().body)
let subcats = []
let position = 0

if (parsed.length > 0) {
for (let child of parsed) {
parsed.parent_id = parsed.parent_id ? parsed.parent_id : 1;

position++

let childData = {
"entity_type_id": 3,
"attribute_set_id": 0,
"parent_id": parsed.parent_id,
"parent_id": parent_id,
"created_at": "2018-10-12",
"updated_at": "2018-10-12",
"position": 1,
"position": position,
"level": 2,
"children_count": 1,
"available_sort_by": null,
"include_in_menu": true,
"name": entities.decode(child.name),
"slug": child.slug,
"id": child.id,
"children_data": child.id !== parent_id && await extractSubcategories(child.id),
"children_data": child.id !== parent_id && await extractSubcategories(child.id, level + 1),
"is_anchor": true,
"is_active": true,
"path": `1/2/${child.id}`,
"slug": child.slug,
"path": child.slug,
"url_key": child.slug,
"url_path": child.id,
"url_path": child.slug,
"product_count": 10,
}

Expand All @@ -69,26 +74,36 @@ const fill = async ({ id,
}
) => {

let include_in_menu = true
let level = 2 // A higher level will hide it from the main menu
// Check if category is a sub-category
if ( parent > 0 ) {
// Hide sub category from main menu
include_in_menu = false
level = 3
}

let output = {
"entity_type_id": 3,
"attribute_set_id": 0,
"parent_id": 0,
"parent_id": parent,
"created_at": "2018-10-12",
"updated_at": "2018-10-12",
"is_active": true,
"position": 1,
"level": 2,
"level": level,
"children_count": 1,
"available_sort_by": null,
"include_in_menu": true,
"include_in_menu": include_in_menu,
"name": entities.decode(name),
"id": id,
"is_anchor": true,
"path": `1/${id}`,
"slug": slug,
"path": slug,
"url_key": slug,
"url_path": slug,
"product_count": 10,
"children_data": await extractSubcategories(parseInt(id)),
"children_data": await extractSubcategories(parseInt(id), level + 1),
};

output.children_count = output.children_data.length;
Expand Down
Loading