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.

Extract Price into a dedicated serializer

Damian Legawiecef71d3c85bce
diff --git a/api/app/serializers/spree/api/v3/admin/price_serializer.rb b/api/app/serializers/spree/api/v3/admin/price_serializer.rb
new file mode 100644
index 0000000000..80b5df1c19
--- /dev/null
+++ b/api/app/serializers/spree/api/v3/admin/price_serializer.rb
@@ -0,0 +1,20 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ # Admin API Price Serializer
+ # Extends Store Price Serializer with admin-only fields
+ class PriceSerializer < V3::PriceSerializer
+ typelize_from Spree::Price
+ typelize variant_id: 'string | null'
+
+ attribute :variant_id do |price|
+ price&.variant&.prefix_id
+ end
+
+ attributes created_at: :iso8601, updated_at: :iso8601
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/api/v3/admin/variant_serializer.rb b/api/app/serializers/spree/api/v3/admin/variant_serializer.rb
index 489b72878f..bb8a011f7a 100644
--- a/api/app/serializers/spree/api/v3/admin/variant_serializer.rb
+++ b/api/app/serializers/spree/api/v3/admin/variant_serializer.rb
@@ -27,7 +27,12 @@ module Spree
variant.total_on_hand
end
- # TODO: Add stock_items and prices associations when Admin API is implemented
+ # All prices for this variant (for admin management)
+ many :prices,
+ resource: Spree::Api::V3::Admin::PriceSerializer,
+ if: proc { params[:includes]&.include?('prices') }
+
+ # TODO: Add stock_items association when Admin API is implemented
end
end
end
diff --git a/api/app/serializers/spree/api/v3/price_serializer.rb b/api/app/serializers/spree/api/v3/price_serializer.rb
new file mode 100644
index 0000000000..3d57f07edb
--- /dev/null
+++ b/api/app/serializers/spree/api/v3/price_serializer.rb
@@ -0,0 +1,36 @@
+module Spree
+ module Api
+ module V3
+ # Store API Price Serializer
+ # Represents a resolved/calculated price for storefront display
+ # Can represent either a calculated price (with price list resolution) or a base price
+ class PriceSerializer < BaseSerializer
+ typelize amount: 'number | null',
+ amount_in_cents: 'number | null',
+ display_amount: 'string | null',
+ compare_at_amount: 'number | null',
+ compare_at_amount_in_cents: 'number | null',
+ display_compare_at_amount: 'string | null',
+ currency: 'string | null',
+ price_list_id: 'string | null'
+
+ attributes :amount, :amount_in_cents, :compare_at_amount,
+ :compare_at_amount_in_cents, :currency
+
+ attribute :display_amount do |price|
+ price.display_amount&.to_s
+ end
+
+ attribute :display_compare_at_amount do |price|
+ price.display_compare_at_amount&.to_s
+ end
+
+ attribute :price_list_id do |price|
+ next nil unless price&.price_list_id.present?
+
+ price.price_list.prefix_id
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/api/v3/product_serializer.rb b/api/app/serializers/spree/api/v3/product_serializer.rb
index da0b981b5a..41cc92793d 100644
--- a/api/app/serializers/spree/api/v3/product_serializer.rb
+++ b/api/app/serializers/spree/api/v3/product_serializer.rb
@@ -10,11 +10,8 @@ module Spree
variant_count: :number,
available_on: 'string | null',
purchasable: :boolean, in_stock: :boolean, backorderable: :boolean, available: :boolean,
- price: 'number | null', price_in_cents: 'number | null', display_price: 'string | null',
- compare_at_price: 'number | null', compare_at_price_in_cents: 'number | null',
- display_compare_at_price: 'string | null',
- original_price: 'number | null', original_price_in_cents: 'number | null', display_original_price: 'string | null',
- on_sale: :boolean, price_list_id: 'string | null',
+ price: 'StorePrice',
+ original_price: 'StorePrice',
tags: 'string[]'
attributes :name, :description, :slug, :sku, :barcode,
@@ -38,67 +35,20 @@ module Spree
product.available?
end
- attribute :price do |product|
- price_object(product)&.amount&.to_f
- end
-
- attribute :price_in_cents do |product|
- price_object(product)&.display_amount&.amount_in_cents
- end
-
- attribute :display_price do |product|
- price_object(product)&.display_price&.to_s
- end
-
- attribute :compare_at_price do |product|
- price_object(product)&.compare_at_amount&.to_f
- end
-
- attribute :compare_at_price_in_cents do |product|
- price_object(product)&.display_compare_at_amount&.amount_in_cents if price_object(product)&.compare_at_amount&.present?
+ attribute :tags do |product|
+ product.taggings.map(&:tag)
end
- attribute :display_compare_at_price do |product|
- price_object(product)&.display_compare_at_amount&.to_s if price_object(product)&.compare_at_amount&.present?
+ # Price object - calculated price with price list resolution
+ attribute :price do |product|
+ price = price_for(product.default_variant)
+ Spree.api.price_serializer.new(price, params: params).to_h if price.present?
end
- # Original price (base price without price list resolution)
+ # Original price - base price without price list resolution (for showing strikethrough)
attribute :original_price do |product|
- original_price_object(product)&.amount&.to_f
- end
-
- attribute :original_price_in_cents do |product|
- original_price_object(product)&.display_amount&.amount_in_cents
- end
-
- attribute :display_original_price do |product|
- original_price_object(product)&.display_price&.to_s
- end
-
- # Whether the product is on sale (price list applied or compare_at_price set)
- attribute :on_sale do |product|
- price = price_object(product)
- original = original_price_object(product)
-
- next false unless price&.amount.present?
-
- # On sale if: price list applied with lower price, OR compare_at_price is higher
- from_price_list = price.price_list_id.present? && original&.amount.present? && price.amount < original.amount
- has_compare_at = price.compare_at_amount.present? && price.amount < price.compare_at_amount
-
- from_price_list || has_compare_at
- end
-
- # ID of the price list if one was applied
- attribute :price_list_id do |product|
- price = price_object(product)
- next nil unless price&.price_list_id.present?
-
- Spree::PriceList.find_by(id: price.price_list_id)&.prefix_id
- end
-
- attribute :tags do |product|
- product.taggings.map(&:tag)
+ price = price_in(product.default_variant)
+ Spree.api.price_serializer.new(price, params: params).to_h if price.present?
end
# Conditional associations
@@ -130,16 +80,6 @@ module Spree
},
resource: Spree.api.taxon_serializer,
if: proc { params[:includes]&.include?('taxons') }
-
- private
-
- def price_object(product)
- @price_object ||= price_for(product.default_variant)
- end
-
- def original_price_object(product)
- @original_price_object ||= price_in(product.default_variant)
- end
end
end
end
diff --git a/api/app/serializers/spree/api/v3/variant_serializer.rb b/api/app/serializers/spree/api/v3/variant_serializer.rb
index 88be22798b..62380c5da6 100644
--- a/api/app/serializers/spree/api/v3/variant_serializer.rb
+++ b/api/app/serializers/spree/api/v3/variant_serializer.rb
@@ -4,15 +4,12 @@ module Spree
# Store API Variant Serializer
# Customer-facing variant data with limited fields
class VariantSerializer < BaseSerializer
-
typelize product_id: :string, sku: 'string | null', barcode: 'string | null',
is_master: :boolean, options_text: :string, track_inventory: :boolean, image_count: :number,
purchasable: :boolean, in_stock: :boolean, backorderable: :boolean,
weight: 'number | null', height: 'number | null', width: 'number | null', depth: 'number | null',
- price: 'number | null', price_in_cents: 'number | null', display_price: 'string | null',
- compare_at_price: 'number | null', compare_at_price_in_cents: 'number | null', display_compare_at_price: 'string | null',
- original_price: 'number | null', original_price_in_cents: 'number | null', display_original_price: 'string | null',
- on_sale: :boolean, price_list_id: 'string | null'
+ price: 'StorePrice',
+ original_price: 'StorePrice'
attribute :product_id do |variant|
variant.product&.prefix_id
@@ -49,69 +46,16 @@ module Spree
variant.depth&.to_f
end
+ # Price object - calculated price with price list resolution
attribute :price do |variant|
- price_object(variant)&.amount&.to_f
- end
-
- attribute :price_in_cents do |variant|
- price_object(variant)&.display_amount&.amount_in_cents
- end
-
- attribute :display_price do |variant|
- price_object(variant)&.display_price&.to_s
- end
-
- attribute :compare_at_price do |variant|
- price_object(variant)&.compare_at_amount&.to_f
+ price = price_for(variant)
+ Spree.api.price_serializer.new(price, params: params).to_h if price.present?
end
- attribute :compare_at_price_in_cents do |variant|
- price = price_object(variant)
- next unless price&.compare_at_amount.present?
-
- Spree::Money.new(price.compare_at_amount, currency: current_currency).amount_in_cents
- end
-
- attribute :display_compare_at_price do |variant|
- price = price_object(variant)
- next unless price&.compare_at_amount.present?
-
- Spree::Money.new(price.compare_at_amount, currency: current_currency).to_s
- end
-
- # Original price (base price without price list resolution)
+ # Original price - base price without price list resolution (for showing strikethrough)
attribute :original_price do |variant|
- original_price_object(variant)&.amount&.to_f
- end
-
- attribute :original_price_in_cents do |variant|
- original_price_object(variant)&.display_amount&.amount_in_cents
- end
-
- attribute :display_original_price do |variant|
- original_price_object(variant)&.display_price&.to_s
- end
-
- # Whether the variant is on sale (price list applied or compare_at_price set)
- attribute :on_sale do |variant|
- price = price_object(variant)
- original = original_price_object(variant)
-
- next false unless price&.amount.present?
-
- # On sale if: price list applied with lower price, OR compare_at_price is higher
- from_price_list = price.price_list_id.present? && original&.amount.present? && price.amount < original.amount
- has_compare_at = price.compare_at_amount.present? && price.amount < price.compare_at_amount
-
- from_price_list || has_compare_at
- end
-
- # ID of the price list if one was applied
- attribute :price_list_id do |variant|
- price = price_object(variant)
- next nil unless price&.price_list_id.present?
-
- Spree::PriceList.find_by(id: price.price_list_id)&.prefix_id
+ price = price_in(variant)
+ Spree.api.price_serializer.new(price, params: params).to_h if price.present?
end
# Conditional associations
@@ -120,16 +64,6 @@ module Spree
if: proc { params[:includes]&.include?('images') }
many :option_values, resource: Spree.api.option_value_serializer
-
- private
-
- def price_object(variant)
- @price_object ||= price_for(variant)
- end
-
- def original_price_object(variant)
- @original_price_object ||= price_in(variant)
- end
end
end
end
diff --git a/api/lib/spree/api/dependencies.rb b/api/lib/spree/api/dependencies.rb
index 0207959898..03b43f2930 100644
--- a/api/lib/spree/api/dependencies.rb
+++ b/api/lib/spree/api/dependencies.rb
@@ -92,6 +92,7 @@ module Spree
# v3 serializers (API v3)
credit_card_serializer: 'Spree::Api::V3::CreditCardSerializer',
+ price_serializer: 'Spree::Api::V3::PriceSerializer',
product_serializer: 'Spree::Api::V3::ProductSerializer',
variant_serializer: 'Spree::Api::V3::VariantSerializer',
image_serializer: 'Spree::Api::V3::ImageSerializer',
diff --git a/api/spec/serializers/spree/api/v3/price_serializer_spec.rb b/api/spec/serializers/spree/api/v3/price_serializer_spec.rb
new file mode 100644
index 0000000000..0778fefecd
--- /dev/null
+++ b/api/spec/serializers/spree/api/v3/price_serializer_spec.rb
@@ -0,0 +1,89 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::PriceSerializer do
+ let(:store) { @default_store }
+ let(:product) { create(:product, stores: [store], price: 19.99) }
+ let(:variant) { product.master }
+ let(:price) { variant.prices.first }
+ let(:base_params) { { store: store, currency: 'USD' } }
+
+ describe 'store serializer' do
+ subject { described_class.new(price, params: base_params).to_h }
+
+ it 'includes amount fields' do
+ expect(subject).to include(
+ 'amount' => 19.99,
+ 'amount_in_cents' => 1999,
+ 'display_amount' => '$19.99'
+ )
+ end
+
+ it 'includes currency' do
+ expect(subject['currency']).to eq('USD')
+ end
+
+ it 'includes nil compare_at_amount when not set' do
+ expect(subject['compare_at_amount']).to be_nil
+ expect(subject['compare_at_amount_in_cents']).to be_nil
+ expect(subject['display_compare_at_amount']).to be_nil
+ end
+
+ it 'includes nil price_list_id when no price list' do
+ expect(subject['price_list_id']).to be_nil
+ end
+
+ context 'with compare_at_amount set' do
+ before do
+ price.update!(compare_at_amount: 29.99)
+ end
+
+ it 'includes compare_at_amount fields' do
+ expect(subject).to include(
+ 'compare_at_amount' => 29.99,
+ 'compare_at_amount_in_cents' => 2999,
+ 'display_compare_at_amount' => '$29.99'
+ )
+ end
+ end
+
+ context 'with price list' do
+ let(:price_list) { create(:price_list, :active, store: store) }
+ let(:price) { create(:price, variant: variant, amount: 14.99, currency: 'USD', price_list: price_list) }
+
+ it 'includes price_list_id' do
+ expect(subject['price_list_id']).to eq(price_list.prefix_id)
+ end
+ end
+
+ end
+end
+
+RSpec.describe Spree::Api::V3::Admin::PriceSerializer do
+ let(:store) { @default_store }
+ let(:product) { create(:product, stores: [store], price: 19.99) }
+ let(:variant) { product.master }
+ let(:price) { variant.prices.first }
+ let(:base_params) { { store: store, currency: 'USD' } }
+
+ describe 'admin serializer' do
+ subject { described_class.new(price, params: base_params).to_h }
+
+ it 'includes store price attributes' do
+ expect(subject).to include(
+ 'amount' => 19.99,
+ 'amount_in_cents' => 1999,
+ 'display_amount' => '$19.99',
+ 'currency' => 'USD'
+ )
+ end
+
+ it 'includes admin-only variant_id' do
+ expect(subject['variant_id']).to eq(variant.prefix_id)
+ end
+
+ it 'includes timestamps' do
+ expect(subject).to have_key('created_at')
+ expect(subject).to have_key('updated_at')
+ end
+ end
+end
diff --git a/api/spec/serializers/spree/api/v3/product_serializer_spec.rb b/api/spec/serializers/spree/api/v3/product_serializer_spec.rb
index f8230e4624..f10499ab50 100644
--- a/api/spec/serializers/spree/api/v3/product_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v3/product_serializer_spec.rb
@@ -28,31 +28,29 @@ RSpec.describe Spree::Api::V3::ProductSerializer do
expect(subject).not_to have_key('deleted_at')
end
- describe 'price attributes' do
+ describe 'price object' do
let(:product) { create(:product, stores: [store], price: 19.99) }
- it 'includes price fields' do
- expect(subject).to include(
- 'price' => 19.99,
- 'display_price' => '$19.99'
+ it 'includes price as a nested object' do
+ expect(subject['price']).to include(
+ 'amount' => 19.99,
+ 'display_amount' => '$19.99',
+ 'currency' => 'USD'
)
- expect(subject['price_in_cents']).to eq(1999)
+ expect(subject['price']['amount_in_cents']).to eq(1999)
end
- it 'includes original_price fields' do
- expect(subject).to include(
- 'original_price' => 19.99,
- 'display_original_price' => '$19.99'
+ it 'includes original_price as a nested object' do
+ expect(subject['original_price']).to include(
+ 'amount' => 19.99,
+ 'display_amount' => '$19.99',
+ 'currency' => 'USD'
)
- expect(subject['original_price_in_cents']).to eq(1999)
- end
-
- it 'includes on_sale as false when no discount' do
- expect(subject['on_sale']).to be false
+ expect(subject['original_price']['amount_in_cents']).to eq(1999)
end
it 'includes price_list_id as nil when no price list' do
- expect(subject['price_list_id']).to be_nil
+ expect(subject['price']['price_list_id']).to be_nil
end
context 'with compare_at_price set' do
@@ -60,17 +58,14 @@ RSpec.describe Spree::Api::V3::ProductSerializer do
product.master.prices.first.update!(compare_at_amount: 29.99)
end
- it 'includes compare_at_price fields' do
- expect(subject).to include(
- 'compare_at_price' => 29.99,
- 'display_compare_at_price' => '$29.99'
+ it 'includes compare_at_amount in price object' do
+ expect(subject['price']).to include(
+ 'compare_at_amount' => 29.99,
+ 'display_compare_at_amount' => '$29.99'
)
- expect(subject['compare_at_price_in_cents']).to eq(2999)
+ expect(subject['price']['compare_at_amount_in_cents']).to eq(2999)
end
- it 'sets on_sale to true' do
- expect(subject['on_sale']).to be true
- end
end
context 'with price list applied' do
@@ -81,35 +76,20 @@ RSpec.describe Spree::Api::V3::ProductSerializer do
end
it 'returns price list price as the main price' do
- expect(subject['price']).to eq(14.99)
- expect(subject['display_price']).to eq('$14.99')
+ expect(subject['price']['amount']).to eq(14.99)
+ expect(subject['price']['display_amount']).to eq('$14.99')
end
it 'returns base price as original_price' do
- expect(subject['original_price']).to eq(19.99)
- expect(subject['display_original_price']).to eq('$19.99')
+ expect(subject['original_price']['amount']).to eq(19.99)
+ expect(subject['original_price']['display_amount']).to eq('$19.99')
end
- it 'sets on_sale to true' do
- expect(subject['on_sale']).to be true
- end
-
- it 'includes the price_list_id' do
- expect(subject['price_list_id']).to eq(price_list.prefix_id)
+ it 'includes the price_list_id in price object' do
+ expect(subject['price']['price_list_id']).to eq(price_list.prefix_id)
end
end
- context 'with price list that increases price' do
- let(:price_list) { create(:price_list, :active, store: store) }
-
- before do
- create(:price, variant: product.master, amount: 24.99, currency: 'USD', price_list: price_list)
- end
-
- it 'sets on_sale to false when price list price is higher' do
- expect(subject['on_sale']).to be false
- end
- end
end
end
end
diff --git a/api/spec/serializers/spree/api/v3/variant_serializer_spec.rb b/api/spec/serializers/spree/api/v3/variant_serializer_spec.rb
index 9dcb2cce00..7d604235a8 100644
--- a/api/spec/serializers/spree/api/v3/variant_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v3/variant_serializer_spec.rb
@@ -30,31 +30,29 @@ RSpec.describe Spree::Api::V3::VariantSerializer do
expect(subject).not_to have_key('deleted_at')
end
- describe 'price attributes' do
+ describe 'price object' do
let(:variant) { create(:variant, product: product, price: 19.99) }
- it 'includes price fields' do
- expect(subject).to include(
- 'price' => 19.99,
- 'display_price' => '$19.99'
+ it 'includes price as a nested object' do
+ expect(subject['price']).to include(
+ 'amount' => 19.99,
+ 'display_amount' => '$19.99',
+ 'currency' => 'USD'
)
- expect(subject['price_in_cents']).to eq(1999)
+ expect(subject['price']['amount_in_cents']).to eq(1999)
end
- it 'includes original_price fields' do
- expect(subject).to include(
- 'original_price' => 19.99,
- 'display_original_price' => '$19.99'
+ it 'includes original_price as a nested object' do
+ expect(subject['original_price']).to include(
+ 'amount' => 19.99,
+ 'display_amount' => '$19.99',
+ 'currency' => 'USD'
)
- expect(subject['original_price_in_cents']).to eq(1999)
- end
-
- it 'includes on_sale as false when no discount' do
- expect(subject['on_sale']).to be false
+ expect(subject['original_price']['amount_in_cents']).to eq(1999)
end
it 'includes price_list_id as nil when no price list' do
- expect(subject['price_list_id']).to be_nil
+ expect(subject['price']['price_list_id']).to be_nil
end
context 'with compare_at_price set' do
@@ -62,17 +60,14 @@ RSpec.describe Spree::Api::V3::VariantSerializer do
variant.prices.first.update!(compare_at_amount: 29.99)
end
- it 'includes compare_at_price fields' do
- expect(subject).to include(
- 'compare_at_price' => 29.99,
- 'display_compare_at_price' => '$29.99'
+ it 'includes compare_at_amount in price object' do
+ expect(subject['price']).to include(
+ 'compare_at_amount' => 29.99,
+ 'display_compare_at_amount' => '$29.99'
)
- expect(subject['compare_at_price_in_cents']).to eq(2999)
+ expect(subject['price']['compare_at_amount_in_cents']).to eq(2999)
end
- it 'sets on_sale to true' do
- expect(subject['on_sale']).to be true
- end
end
context 'with price list applied' do
@@ -83,35 +78,20 @@ RSpec.describe Spree::Api::V3::VariantSerializer do
end
it 'returns price list price as the main price' do
- expect(subject['price']).to eq(14.99)
- expect(subject['display_price']).to eq('$14.99')
+ expect(subject['price']['amount']).to eq(14.99)
+ expect(subject['price']['display_amount']).to eq('$14.99')
end
it 'returns base price as original_price' do
- expect(subject['original_price']).to eq(19.99)
- expect(subject['display_original_price']).to eq('$19.99')
- end
-
- it 'sets on_sale to true' do
- expect(subject['on_sale']).to be true
+ expect(subject['original_price']['amount']).to eq(19.99)
+ expect(subject['original_price']['display_amount']).to eq('$19.99')
end
- it 'includes the price_list_id' do
- expect(subject['price_list_id']).to eq(price_list.prefix_id)
+ it 'includes the price_list_id in price object' do
+ expect(subject['price']['price_list_id']).to eq(price_list.prefix_id)
end
end
- context 'with price list that increases price' do
- let(:price_list) { create(:price_list, :active, store: store) }
-
- before do
- create(:price, variant: variant, amount: 24.99, currency: 'USD', price_list: price_list)
- end
-
- it 'sets on_sale to false when price list price is higher' do
- expect(subject['on_sale']).to be false
- end
- end
end
end
end
@@ -139,5 +119,14 @@ RSpec.describe Spree::Api::V3::Admin::VariantSerializer do
expect(subject).to have_key('total_on_hand')
expect(subject).to have_key('deleted_at')
end
+
+ context 'with prices include' do
+ subject { described_class.new(variant, params: base_params.merge(includes: ['prices'])).to_h }
+
+ it 'includes prices array' do
+ expect(subject).to have_key('prices')
+ expect(subject['prices']).to be_an(Array)
+ end
+ end
end
end
diff --git a/core/app/models/spree/price.rb b/core/app/models/spree/price.rb
index 8bba9b1085..c066e0aa7b 100644
--- a/core/app/models/spree/price.rb
+++ b/core/app/models/spree/price.rb
@@ -40,13 +40,13 @@ module Spree
scope :discounted, -> { where('compare_at_amount > amount') }
scope :base_prices, -> { where(price_list_id: nil) }
scope :for_price_list, ->(price_list) { where(price_list_id: price_list) }
- scope :for_products, ->(products, currency = nil) do
+ scope :for_products, lambda { |products, currency = nil|
currency ||= Spree::Store.default.default_currency
with_currency(currency).joins(:variant).where(
Spree::Variant.table_name => { product_id: products }
)
- end
+ }
extend DisplayMoney
money_methods :amount, :price, :compare_at_amount
@@ -66,6 +66,12 @@ module Spree
self[:amount] = amount.blank? ? nil : Spree::LocalizedNumber.parse(amount)
end
+ # Returns the amount in cents
+ # @return [Integer]
+ def amount_in_cents
+ display_amount&.amount_in_cents
+ end
+
def compare_at_money
Spree::Money.new(compare_at_amount || 0, currency: currency)
end
@@ -76,6 +82,22 @@ module Spree
self[:compare_at_amount] = calculated_value
end
+ # Returns the compare at amount for display
+ # @return [Spree::Money, nil]
+ def display_compare_at_amount
+ return nil if compare_at_amount.nil?
+
+ Spree::Money.new(compare_at_amount, currency: currency)
+ end
+
+ # Returns the compare at amount in cents
+ # @return [Integer, nil]
+ def compare_at_amount_in_cents
+ return nil if compare_at_amount.nil?
+
+ display_compare_at_amount.amount_in_cents
+ end
+
alias_attribute :price, :amount
alias_method :price=, :amount=
alias_attribute :compare_at_price, :compare_at_amount
diff --git a/core/spec/models/spree/price_spec.rb b/core/spec/models/spree/price_spec.rb
index a37231319a..e9bdea7351 100644
--- a/core/spec/models/spree/price_spec.rb
+++ b/core/spec/models/spree/price_spec.rb
@@ -319,6 +319,47 @@ describe Spree::Price, type: :model do
end
end
+ describe '#display_compare_at_amount' do
+ let(:price) { build(:price, amount: 10, compare_at_amount: compare_at_amount, currency: 'USD') }
+
+ context 'when compare_at_amount is set' do
+ let(:compare_at_amount) { 19.99 }
+
+ it 'returns a Spree::Money object' do
+ expect(price.display_compare_at_amount).to be_a(Spree::Money)
+ expect(price.display_compare_at_amount.to_s).to eq('$19.99')
+ end
+ end
+
+ context 'when compare_at_amount is nil' do
+ let(:compare_at_amount) { nil }
+
+ it 'returns nil' do
+ expect(price.display_compare_at_amount).to be_nil
+ end
+ end
+ end
+
+ describe '#compare_at_amount_in_cents' do
+ let(:price) { build(:price, amount: 10, compare_at_amount: compare_at_amount, currency: 'USD') }
+
+ context 'when compare_at_amount is set' do
+ let(:compare_at_amount) { 19.99 }
+
+ it 'returns the amount in cents' do
+ expect(price.compare_at_amount_in_cents).to eq(1999)
+ end
+ end
+
+ context 'when compare_at_amount is nil' do
+ let(:compare_at_amount) { nil }
+
+ it 'returns nil' do
+ expect(price.compare_at_amount_in_cents).to be_nil
+ end
+ end
+ end
+
describe '#discounted?' do
subject { price.discounted? }
diff --git a/docs/api-reference/store.yaml b/docs/api-reference/store.yaml
index bac486a6c7..9e19bad2d1 100644
--- a/docs/api-reference/store.yaml
+++ b/docs/api-reference/store.yaml
@@ -3132,6 +3132,43 @@ components:
- created_at
- updated_at
- payment_method
+ StorePrice:
+ type: object
+ description: A resolved/calculated price object. Contains the price amount, formatted display string, and optional compare_at pricing for sales.
+ properties:
+ amount:
+ type: number
+ nullable: true
+ description: The price amount as a decimal number
+ amount_in_cents:
+ type: integer
+ nullable: true
+ description: The price amount in cents (integer)
+ display_amount:
+ type: string
+ nullable: true
+ description: Formatted price string with currency symbol (e.g., "$19.99")
+ compare_at_amount:
+ type: number
+ nullable: true
+ description: Original price before discount (for showing strikethrough)
+ compare_at_amount_in_cents:
+ type: integer
+ nullable: true
+ description: Original price in cents before discount
+ display_compare_at_amount:
+ type: string
+ nullable: true
+ description: Formatted original price string
+ currency:
+ type: string
+ nullable: true
+ description: ISO 4217 currency code (e.g., "USD", "EUR")
+ price_list_id:
+ type: string
+ nullable: true
+ description: ID of the price list if one was applied
+ x-typelizer: true
StoreProduct:
type: object
properties:
@@ -3174,42 +3211,11 @@ components:
available:
type: boolean
price:
- type: number
- nullable: true
- price_in_cents:
- type: number
- nullable: true
- display_price:
- type: string
- nullable: true
- compare_at_price:
- type: number
- nullable: true
- compare_at_price_in_cents:
- type: number
- nullable: true
- display_compare_at_price:
- type: string
- nullable: true
+ "$ref": "#/components/schemas/StorePrice"
+ description: Current calculated price (may include price list resolution)
original_price:
- type: number
- nullable: true
- description: Base price without price list resolution
- original_price_in_cents:
- type: number
- nullable: true
- description: Base price in cents without price list resolution
- display_original_price:
- type: string
- nullable: true
- description: Formatted base price without price list resolution
- on_sale:
- type: boolean
- description: Whether the product is on sale (price list applied with lower price, or compare_at_price set)
- price_list_id:
- type: string
- nullable: true
- description: ID of the price list if one was applied
+ "$ref": "#/components/schemas/StorePrice"
+ description: Base price without price list resolution (for showing strikethrough)
tags:
type: array
items:
@@ -3246,7 +3252,8 @@ components:
- in_stock
- backorderable
- available
- - on_sale
+ - price
+ - original_price
- tags
StoreShipment:
type: object
@@ -3641,42 +3648,11 @@ components:
type: number
nullable: true
price:
- type: number
- nullable: true
- price_in_cents:
- type: number
- nullable: true
- display_price:
- type: string
- nullable: true
- compare_at_price:
- type: number
- nullable: true
- compare_at_price_in_cents:
- type: number
- nullable: true
- display_compare_at_price:
- type: string
- nullable: true
+ "$ref": "#/components/schemas/StorePrice"
+ description: Current calculated price (may include price list resolution)
original_price:
- type: number
- nullable: true
- description: Base price without price list resolution
- original_price_in_cents:
- type: number
- nullable: true
- description: Base price in cents without price list resolution
- display_original_price:
- type: string
- nullable: true
- description: Formatted base price without price list resolution
- on_sale:
- type: boolean
- description: Whether the variant is on sale (price list applied with lower price, or compare_at_price set)
- price_list_id:
- type: string
- nullable: true
- description: ID of the price list if one was applied
+ "$ref": "#/components/schemas/StorePrice"
+ description: Base price without price list resolution (for showing strikethrough)
images:
type: array
items:
@@ -3698,7 +3674,8 @@ components:
- purchasable
- in_stock
- backorderable
- - on_sale
+ - price
+ - original_price
- option_values
StoreWishedItem:
type: object
diff --git a/sdk/src/types/generated/AdminPrice.ts b/sdk/src/types/generated/AdminPrice.ts
new file mode 100644
index 0000000000..b0fd517333
--- /dev/null
+++ b/sdk/src/types/generated/AdminPrice.ts
@@ -0,0 +1,20 @@
+// Typelizer digest ba2451165fb0a1e6f72cf671c24cb860
+//
+// DO NOT MODIFY: This file was automatically generated by Typelizer.
+
+type AdminPrice = {
+ id: string;
+ amount: number | null;
+ amount_in_cents: number | null;
+ compare_at_amount: number | null;
+ compare_at_amount_in_cents: number | null;
+ currency: string | null;
+ display_amount: string | null;
+ display_compare_at_amount: string | null;
+ price_list_id: string | null;
+ variant_id: string | null;
+ created_at: string;
+ updated_at: string;
+}
+
+export default AdminPrice;
diff --git a/sdk/src/types/generated/AdminProduct.ts b/sdk/src/types/generated/AdminProduct.ts
index 42c90c50b9..2605e9beae 100644
--- a/sdk/src/types/generated/AdminProduct.ts
+++ b/sdk/src/types/generated/AdminProduct.ts
@@ -1,7 +1,7 @@
-// Typelizer digest 3144c61e517435cf25d49a05080eaebe
+// Typelizer digest e888533dab2f822712ace0328c03d514
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
-import type {StoreImage, AdminVariant, StoreOptionType, StoreTaxon} from '@/types'
+import type {StorePrice, StoreImage, AdminVariant, StoreOptionType, StoreTaxon} from '@/types'
type AdminProduct = {
id: string;
@@ -20,18 +20,9 @@ type AdminProduct = {
in_stock: boolean;
backorderable: boolean;
available: boolean;
- price: number | null;
- price_in_cents: number | null;
- display_price: string | null;
- compare_at_price: number | null;
- compare_at_price_in_cents: number | null;
- display_compare_at_price: string | null;
- original_price: number | null;
- original_price_in_cents: number | null;
- display_original_price: string | null;
- on_sale: boolean;
- price_list_id: string | null;
tags: Array<string>;
+ price: StorePrice;
+ original_price: StorePrice;
images?: Array<StoreImage>;
variants?: Array<AdminVariant>;
default_variant?: AdminVariant;
diff --git a/sdk/src/types/generated/AdminVariant.ts b/sdk/src/types/generated/AdminVariant.ts
index c948205f23..28d106e637 100644
--- a/sdk/src/types/generated/AdminVariant.ts
+++ b/sdk/src/types/generated/AdminVariant.ts
@@ -1,7 +1,7 @@
-// Typelizer digest 605ff5c5f60f6c522c7d75bc57e7426e
+// Typelizer digest e8eb7989ec4e6f10f86fc7fca2e85498
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
-import type {StoreImage, StoreOptionValue} from '@/types'
+import type {StorePrice, StoreImage, StoreOptionValue, AdminPrice} from '@/types'
type AdminVariant = {
id: string;
@@ -21,17 +21,8 @@ type AdminVariant = {
height: number | null;
width: number | null;
depth: number | null;
- price: number | null;
- price_in_cents: number | null;
- display_price: string | null;
- compare_at_price: number | null;
- compare_at_price_in_cents: number | null;
- display_compare_at_price: string | null;
- original_price: number | null;
- original_price_in_cents: number | null;
- display_original_price: string | null;
- on_sale: boolean;
- price_list_id: string | null;
+ price: StorePrice;
+ original_price: StorePrice;
images?: Array<StoreImage>;
option_values: Array<StoreOptionValue>;
position: number;
@@ -40,6 +31,7 @@ type AdminVariant = {
cost_price: number | null;
cost_currency: string | null;
total_on_hand: number | null;
+ prices?: Array<AdminPrice>;
}
export default AdminVariant;
diff --git a/sdk/src/types/generated/StorePrice.ts b/sdk/src/types/generated/StorePrice.ts
new file mode 100644
index 0000000000..13336bb372
--- /dev/null
+++ b/sdk/src/types/generated/StorePrice.ts
@@ -0,0 +1,17 @@
+// Typelizer digest ed8fae5b4dacdd11d8e2dcf1236dd834
+//
+// DO NOT MODIFY: This file was automatically generated by Typelizer.
+
+type StorePrice = {
+ id: string;
+ amount: number | null;
+ amount_in_cents: number | null;
+ compare_at_amount: number | null;
+ compare_at_amount_in_cents: number | null;
+ currency: string | null;
+ display_amount: string | null;
+ display_compare_at_amount: string | null;
+ price_list_id: string | null;
+}
+
+export default StorePrice;
diff --git a/sdk/src/types/generated/StoreProduct.ts b/sdk/src/types/generated/StoreProduct.ts
index b2fdd3814b..168ef20e58 100644
--- a/sdk/src/types/generated/StoreProduct.ts
+++ b/sdk/src/types/generated/StoreProduct.ts
@@ -1,7 +1,7 @@
-// Typelizer digest a927e41324e876d6c0ecd9d69cbd265d
+// Typelizer digest b86ac64c5ca2b122a28b835784664081
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
-import type {StoreImage, StoreVariant, StoreOptionType, StoreTaxon} from '@/types'
+import type {StorePrice, StoreImage, StoreVariant, StoreOptionType, StoreTaxon} from '@/types'
type StoreProduct = {
id: string;
@@ -20,18 +20,9 @@ type StoreProduct = {
in_stock: boolean;
backorderable: boolean;
available: boolean;
- price: number | null;
- price_in_cents: number | null;
- display_price: string | null;
- compare_at_price: number | null;
- compare_at_price_in_cents: number | null;
- display_compare_at_price: string | null;
- original_price: number | null;
- original_price_in_cents: number | null;
- display_original_price: string | null;
- on_sale: boolean;
- price_list_id: string | null;
tags: Array<string>;
+ price: StorePrice;
+ original_price: StorePrice;
images?: Array<StoreImage>;
variants?: Array<StoreVariant>;
default_variant?: StoreVariant;
diff --git a/sdk/src/types/generated/StoreVariant.ts b/sdk/src/types/generated/StoreVariant.ts
index e6d9e4afeb..6ff35b2e84 100644
--- a/sdk/src/types/generated/StoreVariant.ts
+++ b/sdk/src/types/generated/StoreVariant.ts
@@ -1,7 +1,7 @@
-// Typelizer digest 12cf536abf625c1798662b3c3edd3eed
+// Typelizer digest bc4f456cedca5c21d10912c352e05bac
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
-import type {StoreImage, StoreOptionValue} from '@/types'
+import type {StorePrice, StoreImage, StoreOptionValue} from '@/types'
type StoreVariant = {
id: string;
@@ -21,17 +21,8 @@ type StoreVariant = {
height: number | null;
width: number | null;
depth: number | null;
- price: number | null;
- price_in_cents: number | null;
- display_price: string | null;
- compare_at_price: number | null;
- compare_at_price_in_cents: number | null;
- display_compare_at_price: string | null;
- original_price: number | null;
- original_price_in_cents: number | null;
- display_original_price: string | null;
- on_sale: boolean;
- price_list_id: string | null;
+ price: StorePrice;
+ original_price: StorePrice;
images?: Array<StoreImage>;
option_values: Array<StoreOptionValue>;
}
diff --git a/sdk/src/types/generated/index.ts b/sdk/src/types/generated/index.ts
index 3598032b19..e3a90cac9f 100644
--- a/sdk/src/types/generated/index.ts
+++ b/sdk/src/types/generated/index.ts
@@ -1,8 +1,9 @@
-// Typelizer digest 8289e02566ea7af68d23ddf2763c3009
+// Typelizer digest c292f915b999e7aa63056776d0851a8b
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
export type { default as StoreAddress } from './StoreAddress'
export type { default as AdminOrder } from './AdminOrder'
+export type { default as AdminPrice } from './AdminPrice'
export type { default as AdminProduct } from './AdminProduct'
export type { default as AdminUser } from './AdminUser'
export type { default as AdminVariant } from './AdminVariant'
@@ -17,6 +18,7 @@ export type { default as StoreOrderPromotion } from './StoreOrderPromotion'
export type { default as StoreOrder } from './StoreOrder'
export type { default as StorePaymentMethod } from './StorePaymentMethod'
export type { default as StorePayment } from './StorePayment'
+export type { default as StorePrice } from './StorePrice'
export type { default as StoreProduct } from './StoreProduct'
export type { default as StoreShipment } from './StoreShipment'
export type { default as StoreShippingMethod } from './StoreShippingMethod'