Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for product attributes with terms #27

Merged
merged 1 commit into from
Feb 5, 2020
Merged
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
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ plugins:[
consumer_secret: <secret>,
},
// Array of strings with fields you'd like to create nodes for...
fields: ['products', 'products/categories'],
fields: ['products', 'products/categories', 'products/attributes'],
// Send the API keys as query string parameters instead of using the authorization header
// OPTIONAL: defaults to false
query_string_auth: false,
Expand Down Expand Up @@ -70,6 +70,7 @@ plugins:[

Definitive:
- Products
- Products Attributes
- Customers
- Orders
- Reports
Expand Down Expand Up @@ -105,6 +106,30 @@ For example, to get product categories: including 'products/categories' in field
}
```

### All products attributes with terms:
```graphql
{
allWcProductsAttributes {
nodes {
name
wordpress_id
order_by
position
slug
type
has_archives
attribute_options {
count
description
menu_order
name
slug
}
}
}
}
```

### All product categories (with associated image):
```graphql
{
Expand Down
2 changes: 2 additions & 0 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
mapRelatedProducts,
mapGroupedProducts,
asyncGetProductVariations,
asyncGetProductAttributes,
timeStampedLog,
} = require("./helpers");

Expand Down Expand Up @@ -116,6 +117,7 @@ exports.sourceNodes = async (
}
}
nodes = await asyncGetProductVariations(nodes, WooCommerce, verbose);
nodes = await asyncGetProductAttributes(nodes, WooCommerce, verbose);
nodes = await mapMediaToNodes({
nodes,
store,
Expand Down
76 changes: 76 additions & 0 deletions helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,81 @@ const asyncGetProductVariations = async (nodes, WooCommerce, verbose) => {
return processedNodes;
};

/**
* Get product attributes terms.
* Asynchronous function.
*
* @param {array} nodes
* @param {object} WooCommerce
*
* @return {array} Processed nodes
*/
const asyncGetProductAttributes = async (nodes, WooCommerce, verbose) => {
if (verbose) {
timeStampedLog(
`gatsby-source-woocommerce: Fetching product attributes for ${nodes.length} nodes`
);
}
const asyncGetProductAttributesStartTime = new Date().getTime();
let asyncGetProductAttributesTimer = asyncGetProductAttributesStartTime;
const processedNodes = [];
const promises = nodes.map(async node => {
if (node.__type === 'wcProductsAttributes') {
let page = 1;
let pages = 1;
node.attribute_options = [];
const attributes_path = `products/attributes/${node.wordpress_id}/terms`;
do {
const args = { page, per_page: 100 };
await WooCommerce.get(attributes_path, args)
.then(response => {
if (response.status === 200) {
node.attribute_options = [...node.attribute_options, ...response.data];
pages = parseInt(response.headers['x-wp-totalpages']);
page++;
if (verbose) {
//print progress every 1500 ms
if (new Date().getTime() - asyncGetProductAttributesTimer > 1500) {
asyncGetProductAttributesTimer = new Date().getTime();
timeStampedLog(`gatsby-source-woocommerce: Retrieved ${attributes_path}...`);
}
}
} else {
console.warn(`
Warning: error while fetching attributes terms for ${node.name}.
Error data: ${response.data}.
`);
pages = 0;
}
})
.catch(error => {
console.warn(`
Warning: error while fetching attributes terms for ${node.name}.
Error: ${error}.
`);
pages = 0;
});
} while (page <= pages);
}
return new Promise((res, rej) => {
res(node);
});
});

await Promise.all(promises).then(results => {
results.forEach(async node => {
processedNodes.push(node);
});
});

timeStampedLog(
`gatsby-source-woocommerce: ${promises.length} product attributes terms retrieved for ${
nodes.length
} nodes in ${(new Date().getTime() - asyncGetProductAttributesStartTime) / 1000}s`
);
return processedNodes;
};

/**
* Create links between products and categories (bi-directional)
* @param {array} nodes
Expand Down Expand Up @@ -474,5 +549,6 @@ module.exports = {
mapRelatedProducts,
mapGroupedProducts,
asyncGetProductVariations,
asyncGetProductAttributes,
timeStampedLog,
};