Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix: Seller product price and stock not loading on edit (#14652)

Damian Legawiece72a01c6cc80
diff --git a/packages/seller-dashboard/src/pages/product.tsx b/packages/seller-dashboard/src/pages/product.tsx
index 291aa45a52..fec67a362c 100644
--- a/packages/seller-dashboard/src/pages/product.tsx
+++ b/packages/seller-dashboard/src/pages/product.tsx
@@ -28,7 +28,18 @@ import { ProductStatusCard } from '../components/product-status-card'
import { RetryableError } from '../components/retryable-error'
/** Everything the form edits, in one request. */
-const PRODUCT_EXPAND = 'variants,media,default_variant,submission'
+const PRODUCT_EXPAND = [
+ 'variants',
+ 'variants.prices',
+ 'variants.stock_levels',
+ 'variants.stock_levels.stock_location',
+ 'default_variant',
+ 'default_variant.prices',
+ 'default_variant.stock_levels',
+ 'default_variant.stock_levels.stock_location',
+ 'media',
+ 'submission',
+].join(',')
/**
* One product, created or edited.
diff --git a/packages/seller-sdk/src/seller-client.ts b/packages/seller-sdk/src/seller-client.ts
index 085bab6f12..02360dd8c2 100644
--- a/packages/seller-sdk/src/seller-client.ts
+++ b/packages/seller-sdk/src/seller-client.ts
@@ -310,7 +310,9 @@ export class SellerClient {
/**
* @param expand associations to include — the product form asks for
- * `variants,media,default_variant`, which is everything it edits.
+ * nested variant prices and stock levels (e.g.
+ * `variants.prices,variants.stock_levels`) alongside
+ * `variants,media,default_variant,submission`.
*/
get: (id: string, expand?: string, options?: RequestOptions): Promise<Product> =>
this.request<Product>('GET', `/products/${id}`, {
diff --git a/spree/api/spec/controllers/spree/api/v3/seller/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/products_controller_spec.rb
index c649b8f859..6642278abd 100644
--- a/spree/api/spec/controllers/spree/api/v3/seller/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/seller/products_controller_spec.rb
@@ -229,14 +229,38 @@ RSpec.describe Spree::Api::V3::Seller::ProductsController, type: :controller do
# The form loads a product with everything it edits in one request, so each
# of these has to resolve — a missing seller-side serializer raises rather
- # than quietly omitting the key.
+ # than quietly omitting the key. Prices and stock live on variants and are
+ # expand-gated; without the nested keys the form hydrates with empty arrays
+ # and the next save wipes what was stored.
it 'expands the collections the form edits' do
- get :show,
- params: { id: mine.prefixed_id, expand: 'variants,media,default_variant' },
- as: :json
+ expand = [
+ 'variants',
+ 'variants.prices',
+ 'variants.stock_levels',
+ 'variants.stock_levels.stock_location',
+ 'default_variant',
+ 'default_variant.prices',
+ 'default_variant.stock_levels',
+ 'default_variant.stock_levels.stock_location',
+ 'media',
+ 'submission'
+ ].join(',')
+
+ get :show, params: { id: mine.prefixed_id, expand: expand }, as: :json
expect(response).to have_http_status(:ok)
- expect(json_response).to include('variants', 'media', 'default_variant')
+ expect(json_response).to include('variants', 'media', 'default_variant', 'submission')
+
+ variant = json_response['variants'].first
+ expect(variant).to include('prices', 'stock_levels')
+ expect(variant['prices']).to be_present
+ expect(variant['prices'].first).to include('amount', 'currency')
+
+ default_variant = json_response['default_variant']
+ expect(default_variant).to include('prices', 'stock_levels')
+ expect(default_variant['prices']).to be_present
+ expect(default_variant['prices'].first).to include('amount', 'currency')
+ expect(default_variant['stock_levels']).to be_present
end
describe 'writing variants, media and memberships' do