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.

Add display price, in currency and validations.

Matthew Kennedye67efa9e11fe
diff --git a/api/app/serializers/spree/v2/storefront/wished_product_serializer.rb b/api/app/serializers/spree/v2/storefront/wished_product_serializer.rb
index 7487054fd8..978ce45278 100644
--- a/api/app/serializers/spree/v2/storefront/wished_product_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/wished_product_serializer.rb
@@ -4,7 +4,23 @@ module Spree
class WishedProductSerializer < BaseSerializer
set_type :wished_product
- attributes :remark, :quantity, :total, :display_total
+ attributes :remark, :quantity
+
+ attribute :price do |wished_variant, params|
+ wished_variant.price(currency: params[:currency])
+ end
+
+ attribute :total do |wished_variant, params|
+ wished_variant.total(currency: params[:currency])
+ end
+
+ attribute :display_price do |wished_variant, params|
+ wished_variant.display_price(currency: params[:currency])
+ end
+
+ attribute :display_total do |wished_variant, params|
+ wished_variant.display_total(currency: params[:currency])
+ end
belongs_to :variant
belongs_to :wishlist, serializer: ::Spree::V2::Storefront::WishlistSerializer
diff --git a/core/app/models/spree/wished_product.rb b/core/app/models/spree/wished_product.rb
index 425395a571..ff331d028a 100644
--- a/core/app/models/spree/wished_product.rb
+++ b/core/app/models/spree/wished_product.rb
@@ -1,16 +1,21 @@
module Spree
class WishedProduct < Spree::Base
+ extend DisplayMoney
+ money_methods :total, :price
+
belongs_to :variant
belongs_to :wishlist
has_one :product, through: :variant
- def total
- quantity * (variant.price || 0)
+ validates :variant, :wishlist, presence: true
+
+ def price(currency)
+ variant.price_in(currency[:currency]).amount.to_i
end
- def display_total
- Spree::Money.new(total)
+ def total(currency)
+ quantity * variant.price_in(currency[:currency]).amount.to_i
end
end
end
diff --git a/core/app/models/spree/wishlist.rb b/core/app/models/spree/wishlist.rb
index 89eff3d80c..931a961835 100644
--- a/core/app/models/spree/wishlist.rb
+++ b/core/app/models/spree/wishlist.rb
@@ -10,6 +10,7 @@ module Spree
has_many :wished_products, dependent: :destroy
validates :name, presence: true
+ validates :is_default, uniqueness: { scope: [:store] }
def include?(variant_id)
wished_products.map(&:variant_id).include? variant_id.to_i