medusa
medusa copied to clipboard
[v1.x] Inventory_quantity returns 0 when using publishable key linked with multiple sales_channel.
Bug report
Describe the bug
When using pulishable_key to map multipel sales_channel to pull products on store front. The inventory_quantity returns 0 which results in out of stuck of selected product.
System information
Medusa version (including plugins): "@medusajs/medusa": "1.20.7", "@medusajs/inventory": "^1.11.6", "@medusajs/admin": "^7.1.14", "@medusajs/stock-location": "^1.11.5",
Node.js version: v18.19.0 Database: postgres Operating system: Mac OS (Sonoma 14.5) Browser (if relevant): Chrome
Steps to reproduce the behavior
- Install the inventory and stock location modules
- Go to settings -> Create sales channel
- Go to Products -> Create Product and link to newly created sales channel
- Go to Inventory -> Create stock locations and link with differnent sales channel. Update the inventory of each stock location of product.
- Go to setttings -> Create publishable key and link with all sales channel
- Store front -> Pass the publishable key in Medusa client or use
x-publishable-api-key
header - Store front -> Selected product which is linked to newly created sales channel returns inventory_quantity 0 results in out of stock issue.
Expected behavior
Publishable key linked with multiple sales channel should return the sum of inventory_quantity of multiple stock locations.
Code snippets
I would like to understand why the publishable_key is currently assigned based on the sales_channel_ids list length being exactly 1. Ideally, the code should check if the length of sales_channel_ids is greater than 0 and assign all sales_channel_ids instead of just the 0th index.
https://github.com/medusajs/medusa/blob/93e5078d0151b170c90b57ab60c6c33944ee3f4f/packages/medusa/src/api/routes/store/products/get-product.ts#L127-L129
Code should be like:
if (req.publishableApiKeyScopes?.sales_channel_ids.length > 0) {
sales_channel_id = req.publishableApiKeyScopes.sales_channel_ids
}