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.

Remove remark. Return wished variant for add, remove, quantity.

Matthew Kennedya3af2e97634e
diff --git a/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb b/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb
index 4a16a720ba..533faa22f5 100644
--- a/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb
@@ -78,19 +78,21 @@ module Spree
resource.reload
if wished_variant.persisted?
- render_serialized_payload { serialize_resource(resource) }
+ render_serialized_payload { serialize_wished_variant(wished_variant) }
else
render_error_payload(resource.errors.full_messages.to_sentence)
end
end
- def update_item
+ def update_item_quantity
+ return render_error_item_quantity unless params[:quantity].to_i > 0
+
spree_authorize! :update, wished_variant
- wished_variant.update(params.permit(:quantity, :remark))
+ wished_variant.update(params.permit(:quantity))
if wished_variant.errors.empty?
- render_serialized_payload { serialize_resource(resource) }
+ render_serialized_payload { serialize_wished_variant(wished_variant) }
else
render_error_payload(resource.errors.full_messages.to_sentence)
end
@@ -100,7 +102,7 @@ module Spree
spree_authorize! :update, wished_variant
if wished_variant.destroy
- render_serialized_payload { serialize_resource(resource) }
+ render_serialized_payload { serialize_wished_variant(wished_variant) }
else
render_error_payload('Something went wrong')
end
@@ -108,6 +110,18 @@ module Spree
private
+ def resource
+ @resource ||= current_store.wishlists.find_by!(token: params[:id])
+ end
+
+ def resource_serializer
+ ::Spree::V2::Storefront::WishlistSerializer
+ end
+
+ def collection_serializer
+ ::Spree::V2::Storefront::WishlistSerializer
+ end
+
def wishlist_attributes
params.require(:wishlist).permit(permitted_wishlist_attributes)
end
@@ -116,20 +130,25 @@ module Spree
params.permit(permitted_wished_variant_attributes)
end
- def resource
- @resource ||= current_store.wishlists.find_by!(token: params[:id])
- end
-
def wished_variant
@wished_variant ||= resource.wished_variants.find(params[:wished_variant_id])
end
- def resource_serializer
- ::Spree::V2::Storefront::WishlistSerializer
+ def serialize_wished_variant(wished_variant)
+ wished_variant_serializer.new(
+ wished_variant,
+ params: serializer_params,
+ include: resource_includes,
+ fields: sparse_fields
+ ).serializable_hash
end
- def collection_serializer
- ::Spree::V2::Storefront::WishlistSerializer
+ def wished_variant_serializer
+ ::Spree::V2::Storefront::WishedVariantSerializer
+ end
+
+ def render_error_item_quantity
+ render json: { error: I18n.t('spree.api.v2.wishlist.wrong_quantity') }, status: 422
end
end
end
diff --git a/api/app/serializers/spree/v2/storefront/wished_variant_serializer.rb b/api/app/serializers/spree/v2/storefront/wished_variant_serializer.rb
index 5e2bf67810..26c2348b0b 100644
--- a/api/app/serializers/spree/v2/storefront/wished_variant_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/wished_variant_serializer.rb
@@ -4,7 +4,7 @@ module Spree
class WishedVariantSerializer < BaseSerializer
set_type :wished_variant
- attributes :remark, :quantity
+ attributes :quantity
attribute :price do |wished_variant, params|
wished_variant.price(currency: params[:currency])
diff --git a/api/config/locales/en.yml b/api/config/locales/en.yml
index 96a6e3075a..5c119bc871 100644
--- a/api/config/locales/en.yml
+++ b/api/config/locales/en.yml
@@ -34,3 +34,5 @@ en:
cart:
wrong_quantity: "Quantity has to be greater than 0"
no_coupon_code: "No coupon code provided and the Order doesn't have any coupon code promotions applied"
+ wishlist:
+ wrong_quantity: "Quantity has to be greater than 0"
diff --git a/api/config/routes.rb b/api/config/routes.rb
index 7eaf4657fe..b313240c50 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -172,7 +172,7 @@ Spree::Core::Engine.add_routes do
member do
post :add_item
- patch 'update_item/:wished_variant_id', to: 'wishlists#update_item', as: :update_item
+ patch 'update_item_quantity/:wished_variant_id', to: 'wishlists#update_item_quantity', as: :update_item_quantity
delete 'remove_item/:wished_variant_id', to: 'wishlists#remove_item', as: :remove_item
end
end
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index 3cfff20888..3c0aeb2876 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -145,6 +145,6 @@ module Spree
@@wishlist_attributes = [:name, :is_default, :is_private]
- @@wished_variant_attributes = [:variant_id, :remark, :quantity]
+ @@wished_variant_attributes = [:variant_id, :quantity]
end
end