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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Expand Product/Variant API to include original vs calculated price (via price lists)
Damian Legawiecab042c4c50f4
diff --git a/api/app/serializers/spree/api/v3/base_serializer.rb b/api/app/serializers/spree/api/v3/base_serializer.rbindex 6e78b44174..cdcd7affc5 100644--- a/api/app/serializers/spree/api/v3/base_serializer.rb+++ b/api/app/serializers/spree/api/v3/base_serializer.rb@@ -53,6 +53,7 @@ module Spreeend# Returns price for a variant using full Price List resolution+ # This may return a price from a price list if applicabledef price_for(variant, quantity: nil)return nil unless variant.respond_to?(:price_for)@@ -64,6 +65,14 @@ module Spree)end+ # Returns the base price for a variant without Price List resolution+ # This is the "original" price before any price list discounts+ def price_in(variant)+ return nil unless variant.respond_to?(:price_in)++ variant.price_in(current_currency)+ end+def image_url_for(image)return nil unless image&.attachment&.attached?diff --git a/api/app/serializers/spree/api/v3/product_serializer.rb b/api/app/serializers/spree/api/v3/product_serializer.rbindex 1bd67b02e6..da0b981b5a 100644--- a/api/app/serializers/spree/api/v3/product_serializer.rb+++ b/api/app/serializers/spree/api/v3/product_serializer.rb@@ -12,7 +12,10 @@ module Spreepurchasable: :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', tags: 'string[]'+ 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: 'string[]'attributes :name, :description, :slug, :sku, :barcode,:meta_description, :meta_keywords,@@ -59,6 +62,41 @@ module Spreeprice_object(product)&.display_compare_at_amount&.to_s if price_object(product)&.compare_at_amount&.present?end+ # Original price (base price without price list resolution)+ 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)end@@ -98,6 +136,10 @@ module Spreedef price_object(product)@price_object ||= price_for(product.default_variant)end++ def original_price_object(product)+ @original_price_object ||= price_in(product.default_variant)+ endendendenddiff --git a/api/app/serializers/spree/api/v3/variant_serializer.rb b/api/app/serializers/spree/api/v3/variant_serializer.rbindex b3447c7450..88be22798b 100644--- a/api/app/serializers/spree/api/v3/variant_serializer.rb+++ b/api/app/serializers/spree/api/v3/variant_serializer.rb@@ -10,7 +10,9 @@ module Spreepurchasable: :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', display_compare_at_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'attribute :product_id do |variant|variant.product&.prefix_id@@ -63,13 +65,55 @@ module Spreeprice_object(variant)&.compare_at_amount&.to_fend+ 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+ next unless price&.compare_at_amount.present?Spree::Money.new(price.compare_at_amount, currency: current_currency).to_send+ # Original price (base price without price list resolution)+ 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+ end+# Conditional associationsmany :images,resource: Spree.api.image_serializer,@@ -82,6 +126,10 @@ module Spreedef price_object(variant)@price_object ||= price_for(variant)end++ def original_price_object(variant)+ @original_price_object ||= price_in(variant)+ endendendenddiff --git a/api/spec/serializers/spree/api/v3/product_serializer_spec.rb b/api/spec/serializers/spree/api/v3/product_serializer_spec.rbindex 69e6984b3c..f8230e4624 100644--- a/api/spec/serializers/spree/api/v3/product_serializer_spec.rb+++ b/api/spec/serializers/spree/api/v3/product_serializer_spec.rb@@ -27,6 +27,90 @@ RSpec.describe Spree::Api::V3::ProductSerializer doexpect(subject).not_to have_key('private_metadata')expect(subject).not_to have_key('deleted_at')end++ describe 'price attributes' 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'+ )+ expect(subject['price_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'+ )+ 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+ end++ it 'includes price_list_id as nil when no price list' do+ expect(subject['price_list_id']).to be_nil+ end++ context 'with compare_at_price set' do+ before 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'+ )+ expect(subject['compare_at_price_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+ let(:price_list) { create(:price_list, :active, store: store) }++ before do+ create(:price, variant: product.master, amount: 14.99, currency: 'USD', price_list: price_list)+ 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')+ 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+ end++ it 'includes the price_list_id' do+ expect(subject['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+ endendenddiff --git a/api/spec/serializers/spree/api/v3/variant_serializer_spec.rb b/api/spec/serializers/spree/api/v3/variant_serializer_spec.rbindex 877063b19f..9dcb2cce00 100644--- a/api/spec/serializers/spree/api/v3/variant_serializer_spec.rb+++ b/api/spec/serializers/spree/api/v3/variant_serializer_spec.rb@@ -29,6 +29,90 @@ RSpec.describe Spree::Api::V3::VariantSerializer doexpect(subject).not_to have_key('total_on_hand')expect(subject).not_to have_key('deleted_at')end++ describe 'price attributes' 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'+ )+ expect(subject['price_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'+ )+ 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+ end++ it 'includes price_list_id as nil when no price list' do+ expect(subject['price_list_id']).to be_nil+ end++ context 'with compare_at_price set' do+ before 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'+ )+ expect(subject['compare_at_price_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+ let(:price_list) { create(:price_list, :active, store: store) }++ before do+ create(:price, variant: variant, amount: 14.99, currency: 'USD', price_list: price_list)+ 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')+ 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+ end++ it 'includes the price_list_id' do+ expect(subject['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+ endendenddiff --git a/docs/api-reference/store.yaml b/docs/api-reference/store.yamlindex 927e75552b..bac486a6c7 100644--- a/docs/api-reference/store.yaml+++ b/docs/api-reference/store.yaml@@ -3191,6 +3191,25 @@ components:display_compare_at_price:type: stringnullable: true+ 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 appliedtags:type: arrayitems:@@ -3227,6 +3246,7 @@ components:- in_stock- backorderable- available+ - on_sale- tagsStoreShipment:type: object@@ -3632,9 +3652,31 @@ components:compare_at_price:type: numbernullable: true+ compare_at_price_in_cents:+ type: number+ nullable: truedisplay_compare_at_price:type: stringnullable: true+ 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 appliedimages:type: arrayitems:@@ -3656,6 +3698,7 @@ components:- purchasable- in_stock- backorderable+ - on_sale- option_valuesStoreWishedItem:type: objectdiff --git a/sdk/src/types/generated/AdminProduct.ts b/sdk/src/types/generated/AdminProduct.tsindex 75e40c02e5..42c90c50b9 100644--- a/sdk/src/types/generated/AdminProduct.ts+++ b/sdk/src/types/generated/AdminProduct.ts@@ -1,4 +1,4 @@-// Typelizer digest d0e72f517599548c58a39e9a7c05e26d+// Typelizer digest 3144c61e517435cf25d49a05080eaebe//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {StoreImage, AdminVariant, StoreOptionType, StoreTaxon} from '@/types'@@ -26,6 +26,11 @@ type AdminProduct = {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>;images?: Array<StoreImage>;variants?: Array<AdminVariant>;diff --git a/sdk/src/types/generated/AdminVariant.ts b/sdk/src/types/generated/AdminVariant.tsindex 23d57cdede..c948205f23 100644--- a/sdk/src/types/generated/AdminVariant.ts+++ b/sdk/src/types/generated/AdminVariant.ts@@ -1,4 +1,4 @@-// Typelizer digest 6a6bd3ff12996bc446bc9162393e058a+// Typelizer digest 605ff5c5f60f6c522c7d75bc57e7426e//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {StoreImage, StoreOptionValue} from '@/types'@@ -25,7 +25,13 @@ type AdminVariant = {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;images?: Array<StoreImage>;option_values: Array<StoreOptionValue>;position: number;diff --git a/sdk/src/types/generated/StoreProduct.ts b/sdk/src/types/generated/StoreProduct.tsindex 0479059ebf..b2fdd3814b 100644--- a/sdk/src/types/generated/StoreProduct.ts+++ b/sdk/src/types/generated/StoreProduct.ts@@ -1,4 +1,4 @@-// Typelizer digest 3630374e7e7ec3c06750a7fd436c8c2c+// Typelizer digest a927e41324e876d6c0ecd9d69cbd265d//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {StoreImage, StoreVariant, StoreOptionType, StoreTaxon} from '@/types'@@ -26,6 +26,11 @@ type StoreProduct = {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>;images?: Array<StoreImage>;variants?: Array<StoreVariant>;diff --git a/sdk/src/types/generated/StoreVariant.ts b/sdk/src/types/generated/StoreVariant.tsindex 3f2506a316..e6d9e4afeb 100644--- a/sdk/src/types/generated/StoreVariant.ts+++ b/sdk/src/types/generated/StoreVariant.ts@@ -1,4 +1,4 @@-// Typelizer digest 2e439a3855b32bf1c380a1ac7d0e989d+// Typelizer digest 12cf536abf625c1798662b3c3edd3eed//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {StoreImage, StoreOptionValue} from '@/types'@@ -25,7 +25,13 @@ type StoreVariant = {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;images?: Array<StoreImage>;option_values: Array<StoreOptionValue>;}