diff --git a/src/adapters/magento/magento2-rest-client/lib/stock_items.js b/src/adapters/magento/magento2-rest-client/lib/stock_items.js index 8907546..e4276d1 100644 --- a/src/adapters/magento/magento2-rest-client/lib/stock_items.js +++ b/src/adapters/magento/magento2-rest-client/lib/stock_items.js @@ -8,6 +8,25 @@ module.exports = function (restClient) { return restClient.get(endpointUrl); } + // MSI + module.getSalableQty = function (sku, stockId) { + var endpointUrl = util.format( + '/inventory/get-product-salable-quantity/%s/%d', + encodeURIComponent(sku), + encodeURIComponent(stockId) + ); + return restClient.get(endpointUrl); + } + + // MSI + module.isSalable = function (sku, stockId) { + var endpointUrl = util.format( + '/inventory/is-product-salable/%s/%d', + encodeURIComponent(sku), + encodeURIComponent(stockId) + ); + return restClient.get(endpointUrl); + } return module; } diff --git a/src/adapters/magento/product.js b/src/adapters/magento/product.js index 82cc6ea..f95530f 100644 --- a/src/adapters/magento/product.js +++ b/src/adapters/magento/product.js @@ -246,15 +246,15 @@ class ProductAdapter extends AbstractMagentoAdapter { } item[customAttribute.attribute_code] = attrValue; } - item.slug = _slugify(item.name + '-' + item.id) + item.slug = _slugify(item.name + '-' + item.id); item.custom_attributes = null; return new Promise((done, reject) => { // TODO: add denormalization of productcategories into product categories // DO NOT use "productcategories" type but rather do search categories with assigned products - let subSyncPromises = [] - const config = this.config + let subSyncPromises = []; + const config = this.config; // TODO: Refactor the following to "Chain of responsibility" // STOCK SYNC @@ -262,13 +262,30 @@ class ProductAdapter extends AbstractMagentoAdapter { logger.info(`Product sub-stage 1: Getting stock items for ${item.sku}`); subSyncPromises.push(() => { return this.api.stockItems.list(item.sku).then((result) => { - item.stock = result + item.stock = result; - const key = util.format(CacheKeys.CACHE_KEY_STOCKITEM, item.id); - logger.debug(`Storing stock data to cache under: ${key}`); - this.cache.set(key, JSON.stringify(result)); + if (this.config.magento.msi.enabled) { + return this.api.stockItems.getSalableQty(item.sku, this.config.magento.msi.stockId).then((salableQty) => { + item.stock.qty = salableQty; + return item; + }).then((item) => { + return this.api.stockItems.isSalable(item.sku, this.config.magento.msi.stockId).then((isSalable) => { + item.stock.is_in_stock = isSalable; - return item + const key = util.format(CacheKeys.CACHE_KEY_STOCKITEM, item.id); + logger.debug(`Storing stock data to cache under: ${key}`); + this.cache.set(key, JSON.stringify(item.stock)); + + return item; + }) + }) + } else { + const key = util.format(CacheKeys.CACHE_KEY_STOCKITEM, item.id); + logger.debug(`Storing stock data to cache under: ${key}`); + this.cache.set(key, JSON.stringify(result)); + + return item; + } }) }) } diff --git a/src/config.js b/src/config.js index 42b2e75..7347ad9 100644 --- a/src/config.js +++ b/src/config.js @@ -30,7 +30,8 @@ module.exports = { accessToken: process.env.MAGENTO_ACCESS_TOKEN || 'rw5w0si9imbu45h3m9hkyrfr4gjina8q', accessTokenSecret: process.env.MAGENTO_ACCESS_TOKEN_SECRET || '00y9dl4vpxgcef3gn5mntbxtylowjcc9', storeId: process.env.MAGENTO_STORE_ID || 1, - currencyCode: process.env.MAGENTO_CURRENCY_CODE || 'USD' + currencyCode: process.env.MAGENTO_CURRENCY_CODE || 'USD', + msi: { enabled: process.env.MAGENTO_MSI_ENABLED || false, stockId: process.env.MAGENTO_MSI_STOCK_ID || 1 } }, vuestorefront: { diff --git a/src/test_product_msi.sh b/src/test_product_msi.sh new file mode 100755 index 0000000..fd9d9f2 --- /dev/null +++ b/src/test_product_msi.sh @@ -0,0 +1,16 @@ + +export TIME_TO_EXIT=2000 +export VS_INVALIDATE_CACHE_URL=http://localhost:3000/invalidate?key=aeSu7aip&tag= +export VS_INVALIDATE_CACHE=1 +export PRODUCTS_SPECIAL_PRICES=true +export MAGENTO_MSI_ENABLED=true +export MAGENTO_MSI_STOCK_ID=1 +export MAGENTO_CONSUMER_KEY=byv3730rhoulpopcq64don8ukb8lf2gq +export MAGENTO_CONSUMER_SECRET=u9q4fcobv7vfx9td80oupa6uhexc27rb +export MAGENTO_ACCESS_TOKEN=040xx3qy7s0j28o3q0exrfop579cy20m +export MAGENTO_ACCESS_TOKEN_SECRET=7qunl3p505rubmr7u1ijt7odyialnih9 + +echo 'Default store - in our case United States / en' +export MAGENTO_URL=http://demo-magento2.vuestorefront.io/rest + +node --harmony cli.js products --removeNonExistent=true --partitions=1