Add :in_stock, :stock_location_id, :stock_location_name to admin variant serializer, these are needed for the variant_autocomplete js code from spree

This commit is contained in:
luisramos0
2019-07-23 16:32:10 +01:00
parent 7e6259da31
commit 2d5eccbf97
3 changed files with 27 additions and 6 deletions

View File

@@ -1,6 +1,9 @@
class Api::Admin::VariantSerializer < ActiveModel::Serializer
attributes :id, :options_text, :unit_value, :unit_description, :unit_to_display, :on_demand, :display_as, :display_name, :name_to_display, :sku
attributes :on_hand, :price, :import_date, :name, :producer_name, :image
attributes :id, :name, :producer_name, :image, :sku, :import_date
attributes :options_text, :unit_value, :unit_description, :unit_to_display
attributes :display_as, :display_name, :name_to_display
attributes :price, :on_demand, :on_hand, :in_stock, :stock_location_id, :stock_location_name
has_many :variant_overrides
def name
@@ -17,7 +20,7 @@ class Api::Admin::VariantSerializer < ActiveModel::Serializer
end
def price
# Decimals are passed to json as strings, we need to run parseFloat.toFixed(2) on the client side.
# Decimals are passed to json as strings, we need to run parseFloat.toFixed(2) on the client.
object.price.nil? ? 0.to_f : object.price
end
@@ -26,6 +29,21 @@ class Api::Admin::VariantSerializer < ActiveModel::Serializer
end
def image
return if object.product.images.empty?
object.product.images.first.mini_url
end
def in_stock
object.in_stock?
end
def stock_location_id
return if object.stock_items.empty?
object.stock_items.first.stock_location.id
end
def stock_location_name
return if object.stock_items.empty?
object.stock_items.first.stock_location.name
end
end