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.

Wire pickup stock locations and CSV product type

Damian Legawiec5e4d09d15abf
diff --git a/packages/admin-sdk/src/types/generated/DeliveryMethod.ts b/packages/admin-sdk/src/types/generated/DeliveryMethod.ts
index 3f03266e23..e89dfd556b 100644
--- a/packages/admin-sdk/src/types/generated/DeliveryMethod.ts
+++ b/packages/admin-sdk/src/types/generated/DeliveryMethod.ts
@@ -1,4 +1,4 @@
-// Typelizer digest fe953fd920a31b03b36a4f4e19cced51
+// Typelizer digest 4ffece4ffd69fb4a834a12fdc8f38429
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
@@ -19,6 +19,7 @@ interface DeliveryMethod {
deleted_at: string;
tax_category_id: string | null;
delivery_zone_ids: Array<string>;
+ stock_location_ids: Array<string>;
calculator_type: string | null;
calculator_preferences: Record<string, unknown> | null;
}
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/orders/$orderId.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/orders/$orderId.tsx
index 9cf65b50cc..ce39e80fa3 100644
--- a/packages/dashboard/src/routes/_authenticated/$storeId/orders/$orderId.tsx
+++ b/packages/dashboard/src/routes/_authenticated/$storeId/orders/$orderId.tsx
@@ -144,7 +144,7 @@ function OrderDetailPage() {
main={
<>
<LineItemsCard order={order} />
- <ShipmentsCard order={order} />
+ <FulfillmentsCard order={order} />
<PaymentsCard order={order} />
<AdjustmentLinesCard order={order} />
<OrderSummaryCard order={order} />
@@ -715,7 +715,7 @@ function LineItemsCard({ order }: { order: Order }) {
}
// ---------------------------------------------------------------------------
-// Shipments
+// Fulfillments
// ---------------------------------------------------------------------------
function EditTrackingDialog({
@@ -777,7 +777,7 @@ function EditTrackingDialog({
)
}
-function ShipmentsCard({ order }: { order: Order }) {
+function FulfillmentsCard({ order }: { order: Order }) {
const { t } = useTranslation()
const { orderId } = Route.useParams()
const confirm = useConfirm()
diff --git a/packages/dashboard/src/schemas/delivery-method.ts b/packages/dashboard/src/schemas/delivery-method.ts
index 4d6bc1537b..9ae45aa7cb 100644
--- a/packages/dashboard/src/schemas/delivery-method.ts
+++ b/packages/dashboard/src/schemas/delivery-method.ts
@@ -15,6 +15,7 @@ export const deliveryMethodFormSchema = z.object({
calculator_type: z.string().optional(),
calculator_preferences: z.record(z.string(), z.unknown()).optional(),
delivery_zone_ids: z.array(z.string()),
+ stock_location_ids: z.array(z.string()),
})
export type DeliveryMethodFormValues = z.infer<typeof deliveryMethodFormSchema>
@@ -32,6 +33,7 @@ export const DELIVERY_METHOD_DEFAULTS: DeliveryMethodFormValues = {
calculator_type: '',
calculator_preferences: {},
delivery_zone_ids: [],
+ stock_location_ids: [],
}
export function deliveryMethodValuesToParams(values: DeliveryMethodFormValues) {
@@ -54,5 +56,6 @@ export function deliveryMethodValuesToParams(values: DeliveryMethodFormValues) {
? { calculator_preferences: values.calculator_preferences }
: {}),
delivery_zone_ids: values.delivery_zone_ids,
+ stock_location_ids: values.stock_location_ids,
}
}
diff --git a/spree/api/app/controllers/spree/api/v3/admin/delivery_methods_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/delivery_methods_controller.rb
index b62ae8715b..5bb36cb0a0 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/delivery_methods_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/delivery_methods_controller.rb
@@ -65,7 +65,7 @@ module Spree
:pickup_point_provider, :storefront_visible, :tracking_url,
:estimated_transit_business_days_min, :estimated_transit_business_days_max,
:tax_category_id, :calculator_type,
- delivery_zone_ids: [], calculator_preferences: {}
+ delivery_zone_ids: [], stock_location_ids: [], calculator_preferences: {}
)
end
@@ -77,13 +77,16 @@ module Spree
# Resolves prefixed-ID params to records; calculator handled separately.
def assignable_params
- attributes = permitted_params.except(:tax_category_id, :delivery_zone_ids, :calculator_type, :calculator_preferences)
+ attributes = permitted_params.except(:tax_category_id, :delivery_zone_ids, :stock_location_ids, :calculator_type, :calculator_preferences)
if params.key?(:tax_category_id)
attributes[:tax_category] = params[:tax_category_id].present? ? Spree::TaxCategory.find_by_prefix_id!(params[:tax_category_id]) : nil
end
if params.key?(:delivery_zone_ids)
attributes[:delivery_zones] = Array(params[:delivery_zone_ids]).map { |id| Spree::DeliveryZone.find_by_prefix_id!(id) }
end
+ if params.key?(:stock_location_ids)
+ attributes[:pickup_locations] = Array(params[:stock_location_ids]).map { |id| Spree::StockLocation.find_by_prefix_id!(id) }
+ end
attributes
end
diff --git a/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb b/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb
index 9ffd752d55..6c2994882d 100644
--- a/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb
@@ -11,7 +11,7 @@ module Spree
# narrowed to locations that can fulfill the whole cart from local
# stock (package-scoped availability).
def pickup_locations
- locations = Spree::StockLocation.active.pickup_enabled
+ locations = find_resource.available_pickup_locations
locations = locations.select { |location| covers_cart?(location, pickup_cart) } if pickup_cart
render json: { data: locations.map { |location| Spree.api.stock_location_serializer.new(location, params: serializer_params).to_h } }
@@ -58,9 +58,12 @@ module Spree
@pickup_cart = params[:cart_id].present? ? current_store.carts.find_by_prefix_id!(params[:cart_id]) : nil
end
- # Every cart item must be on hand at the location (local stock only —
- # a pickup location can't promise items it would need transferred).
+ # 'local' policy: every cart item must be on hand at the location.
+ # 'any' policy: the location offers pickup regardless of local stock
+ # (the merchant transfers goods in).
def covers_cart?(location, cart)
+ return true if location.pickup_stock_policy == 'any'
+
cart.line_items.includes(:variant).all? do |line_item|
next true unless line_item.variant.should_track_inventory?
diff --git a/spree/api/app/serializers/spree/api/v3/admin/delivery_method_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/delivery_method_serializer.rb
index c7c7b2d554..ad3cc389a9 100644
--- a/spree/api/app/serializers/spree/api/v3/admin/delivery_method_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/admin/delivery_method_serializer.rb
@@ -8,6 +8,7 @@ module Spree
storefront_visible: :boolean, tracking_url: [:string, nullable: true],
tax_category_id: [:string, nullable: true],
delivery_zone_ids: [:string, multi: true],
+ stock_location_ids: [:string, multi: true],
calculator_type: [:string, nullable: true],
calculator_preferences: ['Record<string, unknown>', nullable: true]
@@ -23,6 +24,10 @@ module Spree
record.delivery_zones.map(&:prefixed_id)
end
+ attribute :stock_location_ids do |record|
+ record.pickup_locations.map(&:prefixed_id)
+ end
+
attribute :calculator_type do |record|
record.calculator&.type
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/delivery_methods_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/delivery_methods_controller_spec.rb
index 62a9f7bdb2..7c8d5f78d2 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/delivery_methods_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/delivery_methods_controller_spec.rb
@@ -66,6 +66,19 @@ RSpec.describe Spree::Api::V3::Admin::DeliveryMethodsController, type: :controll
expect(json_response['fulfillment_type']).to eq('pickup')
end
+ it 'assigns configured pickup locations' do
+ location = create(:stock_location, pickup_enabled: true)
+
+ post :create, params: {
+ name: 'Counter pickup',
+ fulfillment_type: 'pickup',
+ stock_location_ids: [location.prefixed_id]
+ }, as: :json
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['stock_location_ids']).to eq([location.prefixed_id])
+ end
+
it 'rejects unknown calculator types' do
post :create, params: {
name: 'Sneaky',
diff --git a/spree/api/spec/controllers/spree/api/v3/store/delivery_methods_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/delivery_methods_controller_spec.rb
index f7a412e770..6cf45bf854 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/delivery_methods_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/delivery_methods_controller_spec.rb
@@ -44,6 +44,31 @@ RSpec.describe Spree::Api::V3::Store::DeliveryMethodsController, type: :controll
expect(names).not_to include('Warehouse')
end
+ context 'when the method has configured pickup locations' do
+ let!(:uptown) { create(:stock_location, name: 'Uptown', pickup_enabled: true) }
+
+ it 'lists only the configured locations' do
+ pickup_method.pickup_locations << pickup_location
+
+ get :pickup_locations, params: { id: pickup_method.prefixed_id }, as: :json
+
+ expect(json_response['data'].map { |row| row['name'] }).to eq(['Downtown'])
+ end
+ end
+
+ context 'when the location pickup_stock_policy is any' do
+ let(:cart) { create(:cart_with_line_items, store: store, customer: user) }
+
+ it 'keeps the location even without local stock' do
+ pickup_location.update!(pickup_stock_policy: 'any')
+ pickup_location.stock_item_or_create(cart.line_items.first.variant).set_count_on_hand(0)
+
+ get :pickup_locations, params: { id: pickup_method.prefixed_id, cart_id: cart.prefixed_id }, as: :json
+
+ expect(json_response['data'].map { |row| row['name'] }).to include('Downtown')
+ end
+ end
+
context 'with a cart' do
let(:cart) { create(:cart_with_line_items, store: store, customer: user) }
let(:variant) { cart.line_items.first.variant }
diff --git a/spree/core/app/models/spree/delivery_method_stock_location.rb b/spree/core/app/models/spree/delivery_method_stock_location.rb
new file mode 100644
index 0000000000..bc33144f80
--- /dev/null
+++ b/spree/core/app/models/spree/delivery_method_stock_location.rb
@@ -0,0 +1,12 @@
+module Spree
+ # Restricts a delivery method to specific stock locations. For pickup
+ # methods this is the configured pickup-location set; an empty set means
+ # every pickup-enabled location qualifies.
+ class DeliveryMethodStockLocation < Spree.base_class
+ belongs_to :delivery_method, class_name: 'Spree::DeliveryMethod', inverse_of: :delivery_method_stock_locations
+ belongs_to :stock_location, class_name: 'Spree::StockLocation'
+
+ validates :delivery_method, :stock_location, presence: true
+ validates :stock_location_id, uniqueness: { scope: :delivery_method_id }
+ end
+end
diff --git a/spree/core/app/models/spree/import_schemas/products.rb b/spree/core/app/models/spree/import_schemas/products.rb
index 0f692c01a2..a52cf210f4 100644
--- a/spree/core/app/models/spree/import_schemas/products.rb
+++ b/spree/core/app/models/spree/import_schemas/products.rb
@@ -26,7 +26,8 @@ module Spree
{ name: 'inventory_count', label: 'Inventory Count' },
{ name: 'inventory_backorderable', label: 'Inventory Backorderable' },
{ name: 'tax_category', label: 'Tax Category' },
- { name: 'shipping_category', label: 'Shipping Category' },
+ { name: 'product_type', label: 'Product Type' },
+ { name: 'shipping_category', label: 'Shipping Category (deprecated — use Product Type)' },
{ name: 'image1_src', label: 'Image 1 URL' },
{ name: 'image2_src', label: 'Image 2 URL' },
{ name: 'image3_src', label: 'Image 3 URL' },
diff --git a/spree/core/app/services/spree/imports/row_processors/product_variant.rb b/spree/core/app/services/spree/imports/row_processors/product_variant.rb
index ab4208042f..75fefbdef9 100644
--- a/spree/core/app/services/spree/imports/row_processors/product_variant.rb
+++ b/spree/core/app/services/spree/imports/row_processors/product_variant.rb
@@ -128,6 +128,14 @@ module Spree
product.status = to_spree_status(attributes['status']) if attributes['status'].present?
if options.empty?
+ if attributes['product_type'].present?
+ product_type = prepare_product_type
+ product.product_type = product_type if product_type.present?
+ end
+
+ # Deprecated column — accepted through 6.0 (removal with the
+ # ShippingCategory drop in 6.1); importers should move to
+ # product_type.
if attributes['shipping_category'].present?
shipping_category = prepare_shipping_category
product.shipping_category = shipping_category if shipping_category.present?
@@ -137,6 +145,14 @@ module Spree
product
end
+ def prepare_product_type
+ product_type_name = attributes['product_type'].strip
+ cached_lookup(:product_type, product_type_name) do
+ Spree::ProductType.where(store_id: store.id).find_by(name: product_type_name) ||
+ Spree::ProductType.create!(name: product_type_name, store_id: store.id)
+ end
+ end
+
def prepare_shipping_category
shipping_category_name = attributes['shipping_category'].strip
cached_lookup(:shipping_category, shipping_category_name) do