Method: Spree::Product#set_property

Defined in:
app/models/spree/product.rb

#set_property(property_name, property_value) ⇒ Object

Assigns the given value to the given property.

Parameters:

  • property_name (String)

    the name of the property

  • property_value (String)

    the property value



249
250
251
252
253
254
255
256
257
# File 'app/models/spree/product.rb', line 249

def set_property(property_name, property_value)
  ActiveRecord::Base.transaction do
    # Works around spree_i18n https://github.com/spree/spree/issues/301
    property = Spree::Property.create_with(presentation: property_name).find_or_create_by(name: property_name)
    product_property = Spree::ProductProperty.where(product: self, property:).first_or_initialize
    product_property.value = property_value
    product_property.save!
  end
end