medusa icon indicating copy to clipboard operation
medusa copied to clipboard

[v1.x] Inventory_quantity returns 0 when using publishable key linked with multiple sales_channel.

Open sumanchy88 opened this issue 7 months ago • 2 comments

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

  1. Install the inventory and stock location modules
  2. Go to settings -> Create sales channel
  3. Go to Products -> Create Product and link to newly created sales channel
  4. Go to Inventory -> Create stock locations and link with differnent sales channel. Update the inventory of each stock location of product.
  5. Go to setttings -> Create publishable key and link with all sales channel
  6. Store front -> Pass the publishable key in Medusa client or use x-publishable-api-key header
  7. 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
}

sumanchy88 avatar Jul 02 '24 12:07 sumanchy88