Search code examples
productspreevariant

spree get variant option value


I created products and their variants. Variants have options size and color.

I'm trying to read all variants and their color names:

variants = product.variants_including_master.active(current_currency).includes([:option_values])

variants.each do |variant|
  # here I want to read variant options color and size
  # something like:  variant.option_values['color']
end

I've seen a lot of stuff on internet and can't get anything.


Solution

  • I solved my situation with this:

          variants = product.variants_including_master.active(current_currency).includes([:option_values])
    
          variants.each do |variant|
    
            color = variant.option_values.select { |a| a.option_type.id == 2 }.first
    
            if not color.nil? then
              @product_colors << color[:name]
            end
          end