Created
September 15, 2014 17:36
-
-
Save BenMorganIO/fd673781c9061b3162d3 to your computer and use it in GitHub Desktop.
Create a hash for the variants of a product based on the options value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Spree::Product.class_eval do | |
| def variants_hash | |
| product_variants = self.variants.to_a | |
| color_hash = {} | |
| option_value_variants = -> (color_id) do | |
| sizes_hash = {} | |
| option_values_for('Size').map do |option_value| | |
| size_variants = option_value.variants.to_a.keep_if do |variant| | |
| product_variants.include? variant | |
| end | |
| size_variant = size_variants.keep_if do |variant| | |
| variant.option_values.map(&:id).include? color_id | |
| end.first # there should only be one left over | |
| size_variants.map do |size_variant| | |
| if size_variant.option_values.find_by(option_type_id: 1) | |
| sizes_hash[option_value.value_name.to_sym] = size_variant.gon_object | |
| end | |
| end | |
| end | |
| sizes_hash | |
| end | |
| option_values_for('Color').map do |option_value| | |
| color_hash[option_value.value_name.to_sym] = option_value_variants.call(option_value.id) | |
| end | |
| color_hash | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment