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.

Replace services with just model setters for Product and Variant

Damian Legawiece3bdbd5131ad
diff --git a/spree/api/app/controllers/spree/api/v3/admin/products/variants_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products/variants_controller.rb
index aa9ceae8ef..94618fde46 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/products/variants_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/products/variants_controller.rb
@@ -4,38 +4,6 @@ module Spree
module Admin
module Products
class VariantsController < ResourceController
- # POST /api/v3/admin/products/:product_id/variants
- def create
- authorize!(:create, Spree::Variant)
-
- result = Spree.variant_create_service.call(
- product: @parent,
- params: variant_service_params
- )
-
- if result.success?
- @resource = result.value[:variant]
- render json: serialize_resource(@resource), status: :created
- else
- render_result_error(result)
- end
- end
-
- # PATCH /api/v3/admin/products/:product_id/variants/:id
- def update
- result = Spree.variant_update_service.call(
- variant: @resource,
- params: variant_service_params
- )
-
- if result.success?
- @resource = result.value[:variant]
- render json: serialize_resource(@resource)
- else
- render_result_error(result)
- end
- end
-
protected
def model_class
@@ -59,15 +27,13 @@ module Spree
[:prices, stock_items: :stock_location]
end
- private
-
- def variant_service_params
+ def permitted_params
params.permit(
:sku, :barcode, :price, :compare_at_price,
:cost_price, :cost_currency,
:weight, :height, :width, :depth, :weight_unit, :dimensions_unit,
- :track_inventory, :tax_category_id,
- :option_type, :option_value, :position,
+ :track_inventory, :tax_category_id, :position,
+ options: [:name, :value],
prices: [:amount, :compare_at_amount, :currency],
stock_items: [:stock_location_id, :count_on_hand, :backorderable]
)
diff --git a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
index fc8bb1c4f0..fbf92cc86e 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
@@ -10,36 +10,12 @@ module Spree
'best_selling' => :by_best_selling
}.freeze
- # POST /api/v3/admin/products
- def create
- authorize!(:create, Spree::Product)
-
- result = Spree.product_create_service.call(
- store: current_store,
- params: product_service_params
- )
-
- if result.success?
- @resource = result.value[:product]
- render json: serialize_resource(@resource), status: :created
- else
- render_result_error(result)
- end
- end
-
# PATCH /api/v3/admin/products/:id
def update
- result = Spree.product_update_service.call(
- product: @resource,
- store: current_store,
- params: product_service_params
- )
-
- if result.success?
- @resource = result.value[:product]
+ if @resource.update(update_params)
render json: serialize_resource(@resource)
else
- render_result_error(result)
+ render_validation_error(@resource.errors)
end
end
@@ -101,26 +77,41 @@ module Spree
rp
end
- private
-
- def custom_sort_requested?
- CUSTOM_SORT_SCOPES.key?(sort_param)
- end
-
- def product_service_params
+ def permitted_params
params.permit(
*Spree::PermittedAttributes.product_attributes,
tags: [],
variants: [
- :sku, :barcode, :price, :compare_at_price,
+ :id, :sku, :barcode, :price, :compare_at_price,
:cost_price, :cost_currency,
:weight, :height, :width, :depth, :weight_unit, :dimensions_unit,
- :track_inventory, :tax_category_id,
- :option_type, :option_value, :position,
- prices: [:amount, :compare_at_amount, :currency]
+ :track_inventory, :tax_category_id, :position,
+ options: [:name, :value],
+ prices: [:amount, :compare_at_amount, :currency],
+ stock_items: [:stock_location_id, :count_on_hand, :backorderable]
]
)
end
+
+ private
+
+ def update_params
+ p = permitted_params.to_h.with_indifferent_access
+
+ if p.key?(:taxon_ids)
+ other_store_taxon_ids = @resource.taxons
+ .joins(:taxonomy)
+ .where.not(spree_taxonomies: { store_id: current_store.id })
+ .pluck(:id)
+ p[:taxon_ids] = (Array(p[:taxon_ids]) + other_store_taxon_ids).uniq
+ end
+
+ p
+ end
+
+ def custom_sort_requested?
+ CUSTOM_SORT_SCOPES.key?(sort_param)
+ end
end
end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/products/variants_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/products/variants_controller_spec.rb
index 9fbeb07e0c..672ad80b0a 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/products/variants_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/products/variants_controller_spec.rb
@@ -47,14 +47,13 @@ RSpec.describe Spree::Api::V3::Admin::Products::VariantsController, type: :contr
before { product.option_types << option_type }
- it 'creates a variant with option_type and option_value' do
+ it 'creates a variant with options' do
expect {
post :create, params: {
product_id: product.prefixed_id,
sku: 'NEW-SKU-001',
price: 29.99,
- option_type: option_type.name,
- option_value: option_value.name
+ options: [{ name: option_type.name, value: option_value.name }]
}, as: :json
}.to change(product.variants, :count).by(1)
@@ -72,8 +71,7 @@ RSpec.describe Spree::Api::V3::Admin::Products::VariantsController, type: :contr
product_id: product.prefixed_id,
sku: 'MULTI-001',
price: 19.99,
- option_type: option_type.name,
- option_value: 'New Value',
+ options: [{ name: option_type.name, value: 'New Value' }],
prices: [
{ currency: 'USD', amount: 19.99, compare_at_amount: 24.99 },
{ currency: 'EUR', amount: 17.99 }
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
index c5a1d2013b..345a7430c8 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
@@ -147,8 +147,7 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
tags: ['eco', 'best-seller'],
variants: [
{
- option_type: 'size',
- option_value: 'small',
+ options: [{ name: 'size', value: 'small' }],
sku: 'TEST-SM',
total_on_hand: 10,
track_inventory: true,
diff --git a/spree/api/spec/integration/spree/api/v3/admin/variants_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/variants_spec.rb
index 0846474f9f..ee4b1c47f4 100644
--- a/spree/api/spec/integration/spree/api/v3/admin/variants_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/admin/variants_spec.rb
@@ -68,8 +68,18 @@ RSpec.describe 'Admin Variants API', type: :request, swagger_doc: 'api-reference
dimensions_unit: { type: :string },
track_inventory: { type: :boolean },
tax_category_id: { type: :string },
- option_type: { type: :string, example: 'Size', description: 'Option type name (auto-created if needed)' },
- option_value: { type: :string, example: 'Large', description: 'Option value name (auto-created if needed)' },
+ options: {
+ type: :array,
+ description: 'Option types and values (auto-created if needed)',
+ items: {
+ type: :object,
+ properties: {
+ name: { type: :string, example: 'Size' },
+ value: { type: :string, example: 'Large' }
+ },
+ required: %w[name value]
+ }
+ },
total_on_hand: { type: :integer, example: 100 },
position: { type: :integer },
barcode: { type: :string },
@@ -102,7 +112,7 @@ RSpec.describe 'Admin Variants API', type: :request, swagger_doc: 'api-reference
response '201', 'variant created' do
let(:'x-spree-api-key') { secret_api_key.plaintext_token }
let(:product_id) { product.prefixed_id }
- let(:body) { { sku: 'NEW-SKU-001', price: 24.99, option_type: 'Size', option_value: 'XL' } }
+ let(:body) { { sku: 'NEW-SKU-001', price: 24.99, options: [{ name: 'Size', value: 'XL' }] } }
run_test! do |response|
data = JSON.parse(response.body)
@@ -187,8 +197,16 @@ RSpec.describe 'Admin Variants API', type: :request, swagger_doc: 'api-reference
dimensions_unit: { type: :string },
track_inventory: { type: :boolean },
tax_category_id: { type: :string },
- option_type: { type: :string, example: 'Size' },
- option_value: { type: :string, example: 'Large' },
+ options: {
+ type: :array,
+ items: {
+ type: :object,
+ properties: {
+ name: { type: :string, example: 'Size' },
+ value: { type: :string, example: 'Large' }
+ }
+ }
+ },
total_on_hand: { type: :integer, example: 100 },
position: { type: :integer },
barcode: { type: :string },
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index fe3d942fd5..8da0709665 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -133,6 +133,7 @@ module Spree
after_create :add_associations_from_prototype
after_create :build_variants_from_option_values_hash, if: :option_values_hash
+ after_create :apply_pending_variants, if: :pending_variants?
after_save :save_master
after_save :run_touch_callbacks, if: :anything_changed?
@@ -197,6 +198,29 @@ module Spree
alias options product_option_types
+ # Maps tags array to tag_list for API convenience.
+ # @param tags [Array<String>]
+ def tags=(tags)
+ self.tag_list = tags
+ end
+
+ # Syncs variants from an array of hashes.
+ # Creates new variants, updates existing ones (matched by :id), and removes unlisted ones.
+ # Must be called on a persisted product (use after_save or call explicitly after create).
+ # @param variants_params [Array<Hash>] array of variant attribute hashes
+ # @return [void]
+ def variants=(variants_params)
+ return super if variants_params.blank? || variants_params.first.is_a?(Spree::Variant)
+
+ # Store for deferred processing if product is not yet persisted
+ if new_record?
+ @pending_variants_params = variants_params
+ return
+ end
+
+ apply_variants(variants_params)
+ end
+
self.whitelisted_ransackable_attributes = %w[description name slug discontinue_on status available_on created_at updated_at]
self.whitelisted_ransackable_associations = %w[taxons categories stores variants_including_master master variants tags labels
shipping_category classifications option_types]
@@ -649,6 +673,40 @@ module Spree
nil
end
+ def pending_variants?
+ @pending_variants_params.present?
+ end
+
+ def apply_pending_variants
+ return unless @pending_variants_params
+
+ apply_variants(@pending_variants_params)
+ @pending_variants_params = nil
+ end
+
+ def apply_variants(variants_params)
+ variant_ids_in_payload = []
+
+ variants_params.each do |variant_data|
+ variant_data = variant_data.to_h.with_indifferent_access
+ variant_id = variant_data.delete(:id)
+
+ if variant_id.present?
+ variant = variants_including_master.find_by_param!(variant_id)
+ variant.update!(variant_data)
+ variant_ids_in_payload << variant.id
+ else
+ variant = variants.build
+ variant.assign_attributes(variant_data)
+ variant.save!
+ variant_ids_in_payload << variant.id
+ end
+ end
+
+ # Remove variants not in the payload (only non-master)
+ variants.where.not(id: variant_ids_in_payload).destroy_all if variant_ids_in_payload.any?
+ end
+
def add_associations_from_prototype
if prototype_id && prototype = Spree::Prototype.find_by(id: prototype_id)
self.option_types = prototype.option_types
@@ -695,7 +753,7 @@ module Spree
end
def assign_default_tax_category
- self.tax_category = Spree::TaxCategory.default if new_record?
+ self.tax_category = Spree::TaxCategory.default if new_record? && self[:tax_category_id].blank?
end
def anything_changed?
diff --git a/spree/core/app/models/spree/variant.rb b/spree/core/app/models/spree/variant.rb
index 7db37d8de9..3dc876c6bc 100644
--- a/spree/core/app/models/spree/variant.rb
+++ b/spree/core/app/models/spree/variant.rb
@@ -61,6 +61,7 @@ module Spree
has_many :digitals
before_validation :set_cost_currency
+ before_validation :apply_pending_options, if: :pending_options?
validate :check_price, if: -> { Spree::Config.enable_legacy_default_price }
@@ -76,6 +77,7 @@ module Spree
validates :weight_unit, inclusion: { in: WEIGHT_UNITS }, allow_blank: true
after_create :create_stock_items
+ after_create :apply_pending_stock_items, if: :pending_stock_items?
after_create :set_master_out_of_stock, unless: :is_master?
after_commit :clear_line_items_cache, on: :update
@@ -330,6 +332,11 @@ module Spree
# @param options [Array<Hash>] the options to set
# @return [void]
def options=(options = {})
+ if product.nil?
+ @pending_options = options
+ return
+ end
+
options.each do |option|
next if option[:name].blank? || option[:value].blank?
@@ -438,6 +445,62 @@ module Spree
price_in(currency).try(:compare_at_amount)
end
+ # Syncs base prices from an array of hashes.
+ # Upserts prices for listed currencies, removes base prices for unlisted currencies.
+ # On new records, builds prices in memory (saved when variant is saved).
+ # On persisted records, saves prices immediately and removes unlisted currencies.
+ # @param prices_params [Array<Hash>] array of { currency:, amount:, compare_at_amount: }
+ # @return [void]
+ def prices=(prices_params)
+ return super if prices_params.blank? || prices_params.first.is_a?(Spree::Price)
+
+ currencies_in_payload = []
+
+ prices_params.each do |price_data|
+ price_data = price_data.to_h.with_indifferent_access
+ currencies_in_payload << price_data[:currency]
+
+ if persisted?
+ set_price(price_data[:currency], price_data[:amount], price_data[:compare_at_amount])
+ else
+ # Check default_price first (built via price= setter)
+ if default_price && default_price.new_record? && default_price.currency == price_data[:currency]
+ default_price.amount = price_data[:amount]
+ default_price.compare_at_amount = price_data[:compare_at_amount] if price_data[:compare_at_amount].present?
+ else
+ # Replace any existing in-memory price for this currency
+ existing = prices.detect { |p| p.currency == price_data[:currency] && p.new_record? }
+ if existing
+ existing.amount = price_data[:amount]
+ existing.compare_at_amount = price_data[:compare_at_amount] if price_data[:compare_at_amount].present?
+ else
+ price = prices.build(currency: price_data[:currency], amount: price_data[:amount])
+ price.compare_at_amount = price_data[:compare_at_amount] if price_data[:compare_at_amount].present?
+ end
+ end
+ end
+ end
+
+ # Remove base prices for currencies not in the payload
+ prices.base_prices.where.not(currency: currencies_in_payload).destroy_all if persisted?
+ end
+
+ # Syncs stock items from an array of hashes.
+ # Upserts stock for listed locations, soft-deletes stock items for unlisted locations.
+ # On new records, defers to after_create callback.
+ # @param stock_items_params [Array<Hash>] array of { stock_location_id:, count_on_hand:, backorderable: }
+ # @return [void]
+ def stock_items=(stock_items_params)
+ return super if stock_items_params.blank? || stock_items_params.first.is_a?(Spree::StockItem)
+
+ if new_record?
+ @pending_stock_items_params = stock_items_params
+ return
+ end
+
+ apply_stock_items(stock_items_params)
+ end
+
# Sets the base price (global price, not for a price list) for the given currency.
# @param currency [String] the currency to set the price for
# @param amount [BigDecimal] the amount to set
@@ -585,6 +648,53 @@ module Spree
private
+ def pending_options?
+ @pending_options.present?
+ end
+
+ def apply_pending_options
+ return unless @pending_options
+
+ options_to_apply = @pending_options
+ @pending_options = nil
+
+ options_to_apply.each do |option|
+ next if option[:name].blank? || option[:value].blank?
+
+ set_option_value(option[:name], option[:value], option[:position])
+ end
+ end
+
+ def pending_stock_items?
+ @pending_stock_items_params.present?
+ end
+
+ def apply_pending_stock_items
+ return unless @pending_stock_items_params
+
+ apply_stock_items(@pending_stock_items_params)
+ @pending_stock_items_params = nil
+ end
+
+ def apply_stock_items(stock_items_params)
+ location_ids_in_payload = []
+
+ stock_items_params.each do |stock_data|
+ stock_data = stock_data.to_h.with_indifferent_access
+ location_id = stock_data[:stock_location_id]
+ location = if Spree::PrefixedId.prefixed_id?(location_id)
+ Spree::StockLocation.find_by_prefix_id!(location_id)
+ else
+ Spree::StockLocation.find(location_id)
+ end
+ location_ids_in_payload << location.id
+ set_stock(stock_data[:count_on_hand], stock_data[:backorderable], location)
+ end
+
+ # Soft-delete stock items for locations not in the payload
+ stock_items.where.not(stock_location_id: location_ids_in_payload).update_all(deleted_at: Time.current)
+ end
+
def ensure_not_in_complete_orders
if orders.complete.any?
errors.add(:base, :cannot_destroy_if_attached_to_line_items)
diff --git a/spree/core/app/services/spree/products/create.rb b/spree/core/app/services/spree/products/create.rb
deleted file mode 100644
index ab699713b9..0000000000
--- a/spree/core/app/services/spree/products/create.rb
+++ /dev/null
@@ -1,53 +0,0 @@
-module Spree
- module Products
- class Create
- prepend Spree::ServiceModule::Base
-
- def call(store:, params:)
- ApplicationRecord.transaction do
- run :create_product
- run :create_variants
- end
- end
-
- private
-
- def create_product(store:, params:)
- params = params.to_h.with_indifferent_access
- variants_params = params.delete(:variants)
- product_params = params
- product_params[:store_ids] = [store.id] if product_params[:store_ids].blank?
-
- # Map tags array to tag_list
- product_params[:tag_list] = product_params.delete(:tags) if product_params.key?(:tags)
-
- # Extract IDs that after_initialize callbacks may overwrite
- tax_category_id = product_params.delete(:tax_category_id)
- shipping_category_id = product_params.delete(:shipping_category_id)
-
- product = Spree::Product.new(product_params)
- overrides = {}
- overrides[:tax_category_id] = tax_category_id if tax_category_id.present?
- overrides[:shipping_category_id] = shipping_category_id if shipping_category_id.present?
- product.assign_attributes(overrides) if overrides.any?
-
- if product.save
- success(product: product, store: store, variants_params: variants_params)
- else
- failure(product, product.errors)
- end
- end
-
- def create_variants(product:, store:, variants_params:)
- return success(product: product) if variants_params.blank?
-
- variants_params.each do |variant_data|
- result = Spree::Variants::Create.call(product: product, params: variant_data)
- return result unless result.success?
- end
-
- success(product: product.reload)
- end
- end
- end
-end
diff --git a/spree/core/app/services/spree/products/update.rb b/spree/core/app/services/spree/products/update.rb
deleted file mode 100644
index 5c6210a1e3..0000000000
--- a/spree/core/app/services/spree/products/update.rb
+++ /dev/null
@@ -1,60 +0,0 @@
-module Spree
- module Products
- class Update
- prepend Spree::ServiceModule::Base
-
- def call(product:, store:, params:)
- ApplicationRecord.transaction do
- run :update_product
- run :update_variants
- end
- end
-
- private
-
- def update_product(product:, store:, params:)
- params = params.to_h.with_indifferent_access
- variants_params = params.delete(:variants)
- product_params = params
-
- # Map tags array to tag_list
- product_params[:tag_list] = product_params.delete(:tags) if product_params.key?(:tags)
-
- # Preserve taxon associations from other stores
- if product_params.key?(:taxon_ids)
- other_store_taxon_ids = product.taxons
- .joins(:taxonomy)
- .where.not(spree_taxonomies: { store_id: store.id })
- .pluck(:id)
- product_params[:taxon_ids] = (product_params[:taxon_ids] + other_store_taxon_ids).uniq
- end
-
- if product.update(product_params)
- success(product: product, store: store, variants_params: variants_params)
- else
- failure(product, product.errors)
- end
- end
-
- def update_variants(product:, store:, variants_params:)
- return success(product: product) if variants_params.blank?
-
- variants_params.each do |variant_data|
- variant_data = variant_data.to_h.with_indifferent_access
- variant_id = variant_data.delete(:id)
-
- if variant_id.present?
- variant = product.variants_including_master.find_by_prefix_id!(variant_id)
- result = Spree::Variants::Update.call(variant: variant, params: variant_data)
- else
- result = Spree::Variants::Create.call(product: product, params: variant_data)
- end
-
- return result unless result.success?
- end
-
- success(product: product.reload)
- end
- end
- end
-end
diff --git a/spree/core/app/services/spree/variants/create.rb b/spree/core/app/services/spree/variants/create.rb
deleted file mode 100644
index e6ea52b835..0000000000
--- a/spree/core/app/services/spree/variants/create.rb
+++ /dev/null
@@ -1,60 +0,0 @@
-module Spree
- module Variants
- class Create
- prepend Spree::ServiceModule::Base
-
- def call(product:, params:)
- ApplicationRecord.transaction do
- run :create_variant
- run :sync_prices
- run :set_stock
- end
- end
-
- private
-
- def create_variant(product:, params:)
- params = params.to_h.with_indifferent_access
- @prices_params = params.delete(:prices)
- @stock_items_params = params.delete(:stock_items)
- option_type_name = params.delete(:option_type)
- option_value_name = params.delete(:option_value)
-
- variant = product.variants.build(params)
-
- if option_type_name.present? && option_value_name.present?
- variant.set_option_value(option_type_name, option_value_name)
- end
-
- if variant.save
- success(variant: variant)
- else
- failure(variant, variant.errors)
- end
- end
-
- def sync_prices(variant:)
- return success(variant: variant) if @prices_params.blank?
-
- @prices_params.each do |price_data|
- price_data = price_data.to_h.with_indifferent_access
- variant.set_price(price_data[:currency], price_data[:amount], price_data[:compare_at_amount])
- end
-
- success(variant: variant.reload)
- end
-
- def set_stock(variant:)
- return success(variant: variant) if @stock_items_params.blank?
-
- @stock_items_params.each do |stock_data|
- stock_data = stock_data.to_h.with_indifferent_access
- location = Spree::StockLocation.find_by_prefix_id!(stock_data[:stock_location_id])
- variant.set_stock(stock_data[:count_on_hand], stock_data[:backorderable], location)
- end
-
- success(variant: variant.reload)
- end
- end
- end
-end
diff --git a/spree/core/app/services/spree/variants/update.rb b/spree/core/app/services/spree/variants/update.rb
deleted file mode 100644
index 3daa0073e0..0000000000
--- a/spree/core/app/services/spree/variants/update.rb
+++ /dev/null
@@ -1,77 +0,0 @@
-module Spree
- module Variants
- class Update
- prepend Spree::ServiceModule::Base
-
- def call(variant:, params:)
- ApplicationRecord.transaction do
- run :update_variant
- run :sync_prices
- run :sync_stock
- end
- end
-
- private
-
- def update_variant(variant:, params:)
- params = params.to_h.with_indifferent_access
- @prices_params = params.delete(:prices)
- @stock_items_params = params.delete(:stock_items)
- option_type_name = params.delete(:option_type)
- option_value_name = params.delete(:option_value)
-
- # When explicit prices are provided, remove shortcut attrs to avoid conflicts
- if @prices_params
- params.delete(:price)
- params.delete(:compare_at_price)
- params.delete(:currency)
- end
-
- if option_type_name.present? && option_value_name.present?
- variant.set_option_value(option_type_name, option_value_name)
- end
-
- if variant.update(params)
- success(variant: variant)
- else
- failure(variant, variant.errors)
- end
- end
-
- def sync_prices(variant:)
- return success(variant: variant) unless @prices_params
-
- currencies_in_payload = []
-
- @prices_params.each do |price_data|
- price_data = price_data.to_h.with_indifferent_access
- currencies_in_payload << price_data[:currency]
- variant.set_price(price_data[:currency], price_data[:amount], price_data[:compare_at_amount])
- end
-
- # Soft-delete prices for currencies not in the payload
- variant.prices.base_prices.where.not(currency: currencies_in_payload).destroy_all
-
- success(variant: variant.reload)
- end
-
- def sync_stock(variant:)
- return success(variant: variant) unless @stock_items_params
-
- location_ids_in_payload = []
-
- @stock_items_params.each do |stock_data|
- stock_data = stock_data.to_h.with_indifferent_access
- location = Spree::StockLocation.find_by_prefix_id!(stock_data[:stock_location_id])
- location_ids_in_payload << location.id
- variant.set_stock(stock_data[:count_on_hand], stock_data[:backorderable], location)
- end
-
- # Soft-delete stock items for locations not in the payload
- variant.stock_items.where.not(stock_location_id: location_ids_in_payload).update_all(deleted_at: Time.current)
-
- success(variant: variant.reload)
- end
- end
- end
-end
diff --git a/spree/core/lib/spree/core/dependencies.rb b/spree/core/lib/spree/core/dependencies.rb
index da148b53fd..3eeec639ee 100644
--- a/spree/core/lib/spree/core/dependencies.rb
+++ b/spree/core/lib/spree/core/dependencies.rb
@@ -41,14 +41,6 @@ module Spree
gift_card_remove_service: 'Spree::GiftCards::Remove',
gift_card_redeem_service: 'Spree::GiftCards::Redeem',
- # products
- product_create_service: 'Spree::Products::Create',
- product_update_service: 'Spree::Products::Update',
-
- # variants
- variant_create_service: 'Spree::Variants::Create',
- variant_update_service: 'Spree::Variants::Update',
-
# option types
option_type_create_service: 'Spree::OptionTypes::Create',
option_type_update_service: 'Spree::OptionTypes::Update',
diff --git a/spree/core/spec/models/spree/product_spec.rb b/spree/core/spec/models/spree/product_spec.rb
index e689f74240..38e5cff25b 100644
--- a/spree/core/spec/models/spree/product_spec.rb
+++ b/spree/core/spec/models/spree/product_spec.rb
@@ -1830,4 +1830,106 @@ describe Spree::Product, type: :model do
end
end
end
+
+ describe '#tags=' do
+ let(:product) { create(:product, stores: [store]) }
+
+ it 'sets tag_list from array' do
+ product.tags = ['eco', 'sale']
+ product.save!
+ expect(product.reload.tag_list).to match_array(['eco', 'sale'])
+ end
+
+ it 'clears tags with empty array' do
+ product.update!(tag_list: ['old'])
+ product.tags = []
+ product.save!
+ expect(product.reload.tag_list).to be_empty
+ end
+ end
+
+ describe '#variants=' do
+ let(:product) { create(:product, stores: [store]) }
+ let(:shipping_category) { product.shipping_category }
+
+ context 'with hash params' do
+ it 'creates new variants' do
+ product.variants = [
+ { sku: 'V1', price: 10, options: [{ name: 'Size', value: 'S' }] },
+ { sku: 'V2', price: 20, options: [{ name: 'Size', value: 'M' }] }
+ ]
+
+ expect(product.variants.count).to eq(2)
+ expect(product.variants.pluck(:sku)).to match_array(%w[V1 V2])
+ end
+
+ it 'updates existing variants by prefixed id' do
+ variant = create(:variant, product: product)
+ product.variants = [{ id: variant.prefixed_id, sku: 'UPDATED' }]
+
+ expect(variant.reload.sku).to eq('UPDATED')
+ end
+
+ it 'removes unlisted variants' do
+ v1 = create(:variant, product: product)
+ v2 = create(:variant, product: product)
+
+ product.variants = [{ id: v1.prefixed_id, sku: v1.sku }]
+
+ expect(product.variants.reload).to include(v1)
+ expect(product.variants).not_to include(v2)
+ end
+
+ it 'defers creation on new records' do
+ new_product = Spree::Product.new(
+ name: 'Deferred',
+ price: 10,
+ shipping_category: shipping_category,
+ stores: [store],
+ variants: [{ sku: 'DEF-1', options: [{ name: 'Color', value: 'Red' }] }]
+ )
+ new_product.save!
+
+ expect(new_product.variants.count).to eq(1)
+ expect(new_product.variants.first.sku).to eq('DEF-1')
+ end
+ end
+
+ context 'with Variant objects' do
+ it 'passes through to ActiveRecord setter' do
+ variant = build(:variant)
+ product.variants = [variant]
+ expect(product.variants).to include(variant)
+ end
+ end
+ end
+
+ describe 'assign_default_tax_category with explicit tax_category_id' do
+ let!(:default_tc) { create(:tax_category, is_default: true) }
+ let!(:custom_tc) { create(:tax_category, is_default: false) }
+
+ it 'does not overwrite explicit tax_category_id' do
+ product = Spree::Product.new(
+ name: 'Test',
+ price: 10,
+ shipping_category: create(:shipping_category),
+ stores: [store],
+ tax_category_id: custom_tc.id
+ )
+ product.save!
+
+ expect(product.reload.tax_category).to eq(custom_tc)
+ end
+
+ it 'assigns default when tax_category_id is not provided' do
+ product = Spree::Product.new(
+ name: 'Test',
+ price: 10,
+ shipping_category: create(:shipping_category),
+ stores: [store]
+ )
+
+ expect(product.tax_category).to eq(default_tc)
+ end
+ end
end
diff --git a/spree/core/spec/models/spree/variant_spec.rb b/spree/core/spec/models/spree/variant_spec.rb
index c84e9c8f25..a50fd657c7 100644
--- a/spree/core/spec/models/spree/variant_spec.rb
+++ b/spree/core/spec/models/spree/variant_spec.rb
@@ -1505,4 +1505,135 @@ describe Spree::Variant, type: :model do
end
end
end
+
+ describe '#prices= (hash array)' do
+ context 'on persisted variant' do
+ it 'upserts prices by currency' do
+ variant.prices = [
+ { currency: 'USD', amount: 19.99, compare_at_amount: 24.99 },
+ { currency: 'EUR', amount: 17.50 }
+ ]
+
+ expect(variant.prices.find_by(currency: 'USD').amount.to_f).to eq(19.99)
+ expect(variant.prices.find_by(currency: 'USD').compare_at_amount.to_f).to eq(24.99)
+ expect(variant.prices.find_by(currency: 'EUR').amount.to_f).to eq(17.50)
+ end
+
+ it 'removes base prices for unlisted currencies' do
+ variant.set_price('GBP', 15.00)
+ variant.prices = [{ currency: 'USD', amount: 10.00 }]
+
+ expect(variant.prices.reload.find_by(currency: 'GBP')).to be_nil
+ end
+ end
+
+ context 'on new variant' do
+ let(:new_variant) { build(:variant, product: variant.product) }
+
+ it 'builds prices in memory and saves with variant' do
+ new_variant.prices.clear
+ new_variant.prices = [
+ { currency: 'EUR', amount: 25.00 },
+ { currency: 'GBP', amount: 20.00 }
+ ]
+ new_variant.save!
+
+ expect(new_variant.prices.where(currency: %w[EUR GBP]).count).to eq(2)
+ expect(new_variant.prices.find_by(currency: 'GBP').amount.to_f).to eq(20.00)
+ end
+ end
+
+ context 'with Price objects' do
+ it 'passes through to ActiveRecord setter' do
+ price = build(:price, currency: 'CAD', amount: 30)
+ variant.prices = [price]
+ expect(variant.prices).to include(price)
+ end
+ end
+ end
+
+ describe '#stock_items= (hash array)' do
+ let!(:stock_location) { Spree::StockLocation.first || create(:stock_location) }
+ let!(:stock_location_2) { create(:stock_location, name: 'Warehouse 2') }
+
+ context 'on persisted variant' do
+ it 'upserts stock by location' do
+ variant.stock_items = [
+ { stock_location_id: stock_location.id, count_on_hand: 50, backorderable: true }
+ ]
+
+ si = variant.stock_items.find_by(stock_location: stock_location)
+ expect(si.count_on_hand).to eq(50)
+ expect(si.backorderable).to eq(true)
+ end
+
+ it 'accepts prefixed IDs for stock_location_id' do
+ variant.stock_items = [
+ { stock_location_id: stock_location.prefixed_id, count_on_hand: 30 }
+ ]
+
+ si = variant.stock_items.find_by(stock_location: stock_location)
+ expect(si.count_on_hand).to eq(30)
+ end
+
+ it 'soft-deletes stock items for unlisted locations' do
+ variant.set_stock(10, false, stock_location)
+ variant.set_stock(5, false, stock_location_2)
+
+ variant.stock_items = [
+ { stock_location_id: stock_location.id, count_on_hand: 20 }
+ ]
+
+ expect(variant.stock_items.find_by(stock_location: stock_location).count_on_hand).to eq(20)
+ expect(Spree::StockItem.unscoped.find_by(variant: variant, stock_location: stock_location_2).deleted_at).not_to be_nil
+ end
+ end
+
+ context 'on new variant' do
+ let(:new_variant) { build(:variant, product: variant.product) }
+
+ it 'defers stock_items creation to after_create' do
+ new_variant.stock_items = [
+ { stock_location_id: stock_location.prefixed_id, count_on_hand: 99 }
+ ]
+ new_variant.save!
+
+ si = new_variant.stock_items.find_by(stock_location: stock_location)
+ expect(si.count_on_hand).to eq(99)
+ end
+ end
+ end
+
+ describe '#options=' do
+ it 'sets option values via set_option_value' do
+ product = create(:product, stores: [store])
+ v = create(:variant, product: product)
+ v.set_option_value('color', 'blue')
+
+ expect(v.option_values.reload.map(&:name)).to include('blue')
+ end
+
+ it 'calls set_option_value for each option hash' do
+ product = create(:product, stores: [store])
+ v = create(:variant, product: product)
+
+ expect(v).to receive(:set_option_value).with('Color', 'Blue', nil)
+ v.options = [{ name: 'Color', value: 'Blue' }]
+ end
+
+ it 'skips options with blank name or value' do
+ product = create(:product, stores: [store])
+ v = create(:variant, product: product)
+
+ expect(v).not_to receive(:set_option_value)
+ v.options = [{ name: '', value: 'Blue' }, { name: 'Color', value: '' }]
+ end
+
+ it 'defers when product is nil' do
+ v = Spree::Variant.new
+ v.options = [{ name: 'Color', value: 'Blue' }]
+
+ expect(v.instance_variable_get(:@pending_options)).to eq([{ name: 'Color', value: 'Blue' }])
+ end
+ end
end
diff --git a/spree/core/spec/services/spree/products/create_spec.rb b/spree/core/spec/services/spree/products/create_spec.rb
deleted file mode 100644
index 522e834d67..0000000000
--- a/spree/core/spec/services/spree/products/create_spec.rb
+++ /dev/null
@@ -1,169 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Products::Create do
- subject(:result) { described_class.call(store: store, params: params) }
-
- let(:store) { @default_store }
- let(:shipping_category) { create(:shipping_category) }
-
- describe 'basic product creation' do
- let(:params) { { name: 'Test Product', price: 19.99, shipping_category_id: shipping_category.id } }
-
- it 'creates a product' do
- expect(result).to be_success
- expect(result.value[:product].name).to eq('Test Product')
- expect(result.value[:product].stores).to include(store)
- end
- end
-
- describe 'validation errors' do
- let(:params) { { name: '' } }
-
- it 'returns failure with errors' do
- expect(result).not_to be_success
- expect(result.error).to be_present
- end
- end
-
- describe 'tags mapping' do
- let(:params) { { name: 'Tagged Product', price: 10, shipping_category_id: shipping_category.id, tags: ['eco', 'sale'] } }
-
- it 'maps tags to tag_list' do
- expect(result).to be_success
- expect(result.value[:product].tag_list).to match_array(['eco', 'sale'])
- end
- end
-
- describe 'prefixed ID resolution' do
- let(:tax_category) { create(:tax_category) }
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:taxon) { create(:taxon, taxonomy: taxonomy) }
-
- let(:params) do
- {
- name: 'Resolved Product',
- price: 10,
- shipping_category_id: shipping_category.prefixed_id,
- tax_category_id: tax_category.prefixed_id,
- taxon_ids: [taxon.prefixed_id]
- }
- end
-
- it 'resolves prefixed IDs for associations' do
- expect(result).to be_success
- product = result.value[:product]
- expect(product.shipping_category).to eq(shipping_category)
- expect(product.tax_category).to eq(tax_category)
- expect(product.taxons).to include(taxon)
- end
- end
-
- describe 'invalid prefixed ID' do
- let(:params) { { name: 'Bad ID', price: 10, shipping_category_id: shipping_category.id, tax_category_id: 'tc_nonexistent' } }
-
- it 'raises RecordNotFound' do
- expect { result }.to raise_error(ActiveRecord::RecordNotFound)
- end
- end
-
- describe 'nested variants' do
- let(:params) do
- {
- name: 'Product With Variants',
- price: 10,
- shipping_category_id: shipping_category.id,
- variants: [
- { sku: 'VAR-1', option_type: 'Size', option_value: 'Small', price: 10 },
- { sku: 'VAR-2', option_type: 'Size', option_value: 'Large', price: 15 }
- ]
- }
- end
-
- it 'creates variants with option types auto-created' do
- expect(result).to be_success
- product = result.value[:product]
- expect(product.variants.count).to eq(2)
- expect(product.variants.pluck(:sku)).to match_array(%w[VAR-1 VAR-2])
- expect(product.option_types.map(&:name)).to include('size')
- end
- end
-
- describe 'variant with prices' do
- let(:params) do
- {
- name: 'Multi-price Product',
- price: 10,
- shipping_category_id: shipping_category.id,
- variants: [
- {
- sku: 'MP-1',
- option_type: 'Color',
- option_value: 'Red',
- prices: [
- { currency: 'USD', amount: 10.99, compare_at_amount: 14.99 },
- { currency: 'EUR', amount: 9.50 }
- ]
- }
- ]
- }
- end
-
- it 'sets prices for the variant' do
- expect(result).to be_success
- variant = result.value[:product].variants.find_by(sku: 'MP-1')
- usd = variant.prices.find_by(currency: 'USD')
- eur = variant.prices.find_by(currency: 'EUR')
-
- expect(usd.amount.to_f).to eq(10.99)
- expect(usd.compare_at_amount.to_f).to eq(14.99)
- expect(eur.amount.to_f).to eq(9.50)
- end
- end
-
- describe 'variant with stock_items' do
- let!(:stock_location) { create(:stock_location, propagate_all_variants: true) }
-
- let(:params) do
- {
- name: 'Stocked Product',
- price: 10,
- shipping_category_id: shipping_category.id,
- variants: [
- {
- sku: 'STK-1',
- option_type: 'Size',
- option_value: 'M',
- stock_items: [
- { stock_location_id: stock_location.prefixed_id, count_on_hand: 25 }
- ]
- }
- ]
- }
- end
-
- it 'sets stock level' do
- expect(result).to be_success
- variant = result.value[:product].variants.find_by(sku: 'STK-1')
- si = variant.stock_items.find_by(stock_location: stock_location)
- expect(si.count_on_hand).to eq(25)
- end
- end
-
- describe 'empty variants array' do
- let(:params) { { name: 'No Variants', price: 5, shipping_category_id: shipping_category.id, variants: [] } }
-
- it 'creates product without extra variants' do
- expect(result).to be_success
- expect(result.value[:product].variants.count).to eq(0)
- end
- end
-
- describe 'without store_ids assigns current store' do
- let(:params) { { name: 'Auto Store', price: 5, shipping_category_id: shipping_category.id } }
-
- it 'assigns the provided store' do
- expect(result).to be_success
- expect(result.value[:product].stores).to eq([store])
- end
- end
-end
diff --git a/spree/core/spec/services/spree/products/update_spec.rb b/spree/core/spec/services/spree/products/update_spec.rb
deleted file mode 100644
index 818cb78072..0000000000
--- a/spree/core/spec/services/spree/products/update_spec.rb
+++ /dev/null
@@ -1,148 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Products::Update do
- subject(:result) { described_class.call(product: product, store: store, params: params) }
-
- let(:store) { @default_store }
- let!(:product) { create(:product, stores: [store]) }
-
- describe 'basic update' do
- let(:params) { { name: 'Updated Name' } }
-
- it 'updates the product' do
- expect(result).to be_success
- expect(result.value[:product].name).to eq('Updated Name')
- end
- end
-
- describe 'validation errors' do
- let(:params) { { name: '' } }
-
- it 'returns failure' do
- expect(result).not_to be_success
- end
- end
-
- describe 'tags update' do
- let(:params) { { tags: ['new-tag'] } }
-
- it 'updates tag_list' do
- expect(result).to be_success
- expect(result.value[:product].tag_list).to eq(['new-tag'])
- end
- end
-
- describe 'clearing tags' do
- before { product.update!(tag_list: ['old-tag']) }
- let(:params) { { tags: [] } }
-
- it 'clears all tags' do
- expect(result).to be_success
- expect(result.value[:product].tag_list).to be_empty
- end
- end
-
- describe 'taxon_ids preserves other store taxons' do
- let(:other_store) { create(:store) }
- let(:taxonomy_this) { create(:taxonomy, store: store) }
- let(:taxonomy_other) { create(:taxonomy, store: other_store) }
- let(:taxon_this) { create(:taxon, taxonomy: taxonomy_this) }
- let(:taxon_other) { create(:taxon, taxonomy: taxonomy_other) }
-
- before { product.taxons = [taxon_this, taxon_other] }
-
- let(:new_taxon) { create(:taxon, taxonomy: taxonomy_this) }
- let(:params) { { taxon_ids: [new_taxon.id] } }
-
- it 'keeps taxons from other stores and replaces current store taxons' do
- expect(result).to be_success
- expect(result.value[:product].taxons).to include(new_taxon, taxon_other)
- expect(result.value[:product].taxons).not_to include(taxon_this)
- end
- end
-
- describe 'prefixed ID resolution' do
- let(:tax_category) { create(:tax_category) }
- let(:params) { { tax_category_id: tax_category.prefixed_id } }
-
- it 'resolves prefixed tax_category_id' do
- expect(result).to be_success
- expect(result.value[:product].tax_category).to eq(tax_category)
- end
- end
-
- describe 'update existing variant by prefixed ID' do
- let!(:variant) { create(:variant, product: product) }
- let(:params) do
- {
- variants: [
- { id: variant.prefixed_id, sku: 'UPDATED-SKU' }
- ]
- }
- end
-
- it 'updates the variant' do
- expect(result).to be_success
- expect(variant.reload.sku).to eq('UPDATED-SKU')
- end
- end
-
- describe 'create new variant via update' do
- let(:option_type) { create(:option_type) }
-
- before { product.option_types << option_type }
-
- let(:params) do
- {
- variants: [
- { sku: 'NEW-VIA-UPDATE', option_type: option_type.name, option_value: 'XL', price: 20 }
- ]
- }
- end
-
- it 'creates a new variant' do
- expect { result }.to change { product.variants.count }.by(1)
- expect(result).to be_success
- expect(product.variants.find_by(sku: 'NEW-VIA-UPDATE')).to be_present
- end
- end
-
- describe 'variant price upsert on update' do
- let!(:variant) { create(:variant, product: product) }
- let(:params) do
- {
- variants: [
- {
- id: variant.prefixed_id,
- prices: [
- { currency: 'GBP', amount: 12.50 }
- ]
- }
- ]
- }
- end
-
- it 'upserts prices and soft-deletes removed currencies' do
- expect(result).to be_success
- variant.reload
- expect(variant.prices.find_by(currency: 'GBP').amount.to_f).to eq(12.50)
- end
- end
-
- describe 'nonexistent variant prefixed ID' do
- let(:params) { { variants: [{ id: 'variant_nonexistent', sku: 'X' }] } }
-
- it 'raises RecordNotFound' do
- expect { result }.to raise_error(ActiveRecord::RecordNotFound)
- end
- end
-
- describe 'no variants key' do
- let(:params) { { name: 'Just Name' } }
-
- it 'does not touch variants' do
- expect(result).to be_success
- expect(result.value[:product].name).to eq('Just Name')
- end
- end
-end
diff --git a/spree/core/spec/services/spree/variants/create_spec.rb b/spree/core/spec/services/spree/variants/create_spec.rb
deleted file mode 100644
index d776662f76..0000000000
--- a/spree/core/spec/services/spree/variants/create_spec.rb
+++ /dev/null
@@ -1,138 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Variants::Create do
- subject(:result) { described_class.call(product: product, params: params) }
-
- let(:store) { @default_store }
- let!(:product) { create(:product, stores: [store]) }
- let(:option_type) { create(:option_type) }
- let(:option_value) { create(:option_value, option_type: option_type) }
-
- before { product.option_types << option_type }
-
- describe 'basic variant creation' do
- let(:params) { { sku: 'NEW-V1', price: 10, option_type: option_type.name, option_value: option_value.name } }
-
- it 'creates a variant' do
- expect(result).to be_success
- expect(result.value[:variant].sku).to eq('NEW-V1')
- expect(result.value[:variant].product).to eq(product)
- end
- end
-
- describe 'validation error - missing option values' do
- let(:params) { { sku: 'NO-OPT' } }
-
- it 'returns failure' do
- expect(result).not_to be_success
- end
- end
-
- describe 'validation error - duplicate sku' do
- let!(:existing) { create(:variant, product: product, sku: 'DUPE') }
- let(:params) { { sku: 'DUPE', option_type: option_type.name, option_value: 'unique-val' } }
-
- it 'returns failure with sku error' do
- expect(result).not_to be_success
- end
- end
-
- describe 'with nested prices' do
- let(:params) do
- {
- sku: 'PRICED-V',
- option_type: option_type.name,
- option_value: 'priced',
- price: 10,
- prices: [
- { currency: 'USD', amount: 25.00, compare_at_amount: 30.00 },
- { currency: 'EUR', amount: 22.00 }
- ]
- }
- end
-
- it 'creates prices' do
- expect(result).to be_success
- variant = result.value[:variant]
- expect(variant.prices.find_by(currency: 'USD').amount.to_f).to eq(25.00)
- expect(variant.prices.find_by(currency: 'USD').compare_at_amount.to_f).to eq(30.00)
- expect(variant.prices.find_by(currency: 'EUR').amount.to_f).to eq(22.00)
- end
- end
-
- describe 'with nested stock_items' do
- let!(:stock_location_1) { Spree::StockLocation.first || create(:stock_location) }
- let!(:stock_location_2) { create(:stock_location, name: 'Warehouse 2') }
-
- let(:params) do
- {
- sku: 'STOCKED-V',
- option_type: option_type.name,
- option_value: 'stocked',
- price: 10,
- stock_items: [
- { stock_location_id: stock_location_1.prefixed_id, count_on_hand: 50, backorderable: false },
- { stock_location_id: stock_location_2.prefixed_id, count_on_hand: 10, backorderable: true }
- ]
- }
- end
-
- it 'sets stock levels' do
- expect(result).to be_success
- variant = result.value[:variant]
- si1 = variant.stock_items.find_by(stock_location: stock_location_1)
- si2 = variant.stock_items.find_by(stock_location: stock_location_2)
- expect(si1.count_on_hand).to eq(50)
- expect(si1.backorderable).to eq(false)
- expect(si2.count_on_hand).to eq(10)
- expect(si2.backorderable).to eq(true)
- end
- end
-
- describe 'invalid stock_location prefixed ID' do
- let(:params) do
- {
- sku: 'BAD-LOC',
- option_type: option_type.name,
- option_value: 'badloc',
- price: 10,
- stock_items: [
- { stock_location_id: 'sloc_nonexistent', count_on_hand: 5 }
- ]
- }
- end
-
- it 'raises RecordNotFound' do
- expect { result }.to raise_error(ActiveRecord::RecordNotFound)
- end
- end
-
- describe 'option type auto-creation' do
- let(:params) { { sku: 'AUTO-OPT', price: 10, option_type: 'Material', option_value: 'Cotton' } }
-
- it 'creates option type and value if they do not exist' do
- expect(result).to be_success
- expect(Spree::OptionType.find_by(name: 'material')).to be_present
- expect(Spree::OptionValue.find_by(name: 'cotton')).to be_present
- end
- end
-
- describe 'prefixed tax_category_id' do
- let(:tax_category) { create(:tax_category) }
- let(:params) { { sku: 'TAX-V', option_type: option_type.name, option_value: 'taxed', price: 10, tax_category_id: tax_category.prefixed_id } }
-
- it 'resolves prefixed ID' do
- expect(result).to be_success
- expect(result.value[:variant].read_attribute(:tax_category_id)).to eq(tax_category.id)
- end
- end
-
- describe 'no prices or stock_items' do
- let(:params) { { sku: 'PLAIN-V', option_type: option_type.name, option_value: 'plain', price: 10 } }
-
- it 'creates variant without extra prices or stock changes' do
- expect(result).to be_success
- expect(result.value[:variant].sku).to eq('PLAIN-V')
- end
- end
-end
diff --git a/spree/core/spec/services/spree/variants/update_spec.rb b/spree/core/spec/services/spree/variants/update_spec.rb
deleted file mode 100644
index 018e75932c..0000000000
--- a/spree/core/spec/services/spree/variants/update_spec.rb
+++ /dev/null
@@ -1,132 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Variants::Update do
- subject(:result) { described_class.call(variant: variant, params: params) }
-
- let(:store) { @default_store }
- let!(:product) { create(:product, stores: [store]) }
- let!(:variant) { create(:variant, product: product) }
-
- describe 'basic update' do
- let(:params) { { sku: 'UPDATED-SKU' } }
-
- it 'updates variant attributes' do
- expect(result).to be_success
- expect(result.value[:variant].sku).to eq('UPDATED-SKU')
- end
- end
-
- describe 'validation error' do
- let!(:other_variant) { create(:variant, product: product, sku: 'TAKEN') }
- let(:params) { { sku: 'TAKEN' } }
-
- it 'returns failure for duplicate sku' do
- expect(result).not_to be_success
- end
- end
-
- describe 'sync prices' do
- let(:params) do
- {
- prices: [
- { currency: 'USD', amount: 50.00, compare_at_amount: 60.00 },
- { currency: 'GBP', amount: 40.00 }
- ]
- }
- end
-
- it 'creates new prices' do
- expect(result).to be_success
- variant.reload
- expect(variant.prices.find_by(currency: 'USD').amount.to_f).to eq(50.00)
- expect(variant.prices.find_by(currency: 'GBP').amount.to_f).to eq(40.00)
- end
- end
-
- describe 'sync prices removes missing currencies' do
- before do
- variant.prices.create!(currency: 'EUR', amount: 20)
- variant.prices.create!(currency: 'GBP', amount: 15)
- end
-
- let(:params) do
- {
- prices: [
- { currency: 'EUR', amount: 25.00 }
- ]
- }
- end
-
- it 'soft-deletes GBP price and updates EUR' do
- expect(result).to be_success
- variant.reload
- expect(variant.prices.find_by(currency: 'EUR').amount.to_f).to eq(25.00)
- # GBP should be soft-deleted
- gbp = Spree::Price.with_deleted.find_by(variant_id: variant.id, currency: 'GBP')
- expect(gbp.deleted_at).to be_present
- end
- end
-
- describe 'sync stock_items' do
- let!(:stock_location) { Spree::StockLocation.first || create(:stock_location) }
-
- let(:params) do
- {
- stock_items: [
- { stock_location_id: stock_location.prefixed_id, count_on_hand: 100, backorderable: true }
- ]
- }
- end
-
- it 'sets stock levels' do
- expect(result).to be_success
- si = variant.reload.stock_items.find_by(stock_location: stock_location)
- expect(si.count_on_hand).to eq(100)
- expect(si.backorderable).to eq(true)
- end
- end
-
- describe 'sync stock_items removes missing locations' do
- let!(:loc1) { Spree::StockLocation.first || create(:stock_location) }
- let!(:loc2) { create(:stock_location, name: 'Secondary') }
-
- before do
- variant.stock_items.find_or_create_by!(stock_location: loc1).update!(count_on_hand: 10)
- variant.stock_items.find_or_create_by!(stock_location: loc2).update!(count_on_hand: 20)
- end
-
- let(:params) do
- {
- stock_items: [
- { stock_location_id: loc1.prefixed_id, count_on_hand: 50 }
- ]
- }
- end
-
- it 'soft-deletes stock item for loc2' do
- expect(result).to be_success
- variant.reload
- expect(variant.stock_items.find_by(stock_location: loc1).count_on_hand).to eq(50)
- deleted_si = Spree::StockItem.with_deleted.find_by(variant_id: variant.id, stock_location_id: loc2.id)
- expect(deleted_si.deleted_at).to be_present
- end
- end
-
- describe 'option value reassignment' do
- let(:params) { { option_type: 'Color', option_value: 'Blue' } }
-
- it 'assigns new option value' do
- expect(result).to be_success
- expect(result.value[:variant].option_values.map(&:presentation)).to include('Blue')
- end
- end
-
- describe 'no prices or stock params' do
- let(:params) { { weight: 2.5 } }
-
- it 'updates only variant attributes' do
- expect(result).to be_success
- expect(result.value[:variant].weight.to_f).to eq(2.5)
- end
- end
-end