Sha256: 432e8e4d4e68addbebe5ce4dce7f4a7b35bc1b86ddeddad535eb605b10736841

Contents?: true

Size: 1.62 KB

Versions: 42

Compression:

Stored size: 1.62 KB

Contents

require 'csv'

Spree::Sample.load_sample('option_values')
Spree::Sample.load_sample('products')
Spree::Sample.load_sample('tax_categories')

VARIANTS = CSV.read(File.join(__dir__, 'variants.csv'))

clothing_tax_category = Spree::TaxCategory.find_by!(name: 'Clothing')
color_option_values = Spree::OptionType.find_by!(name: 'color').option_values
length_option_values = Spree::OptionType.find_by!(name: 'length').option_values
size_option_values = Spree::OptionType.find_by!(name: 'size').option_values

VARIANTS.each do |(parent_name, taxon_name, product_name, color_name)|
  parent = Spree::Taxon.find_by!(name: parent_name)
  taxon = parent.children.find_by!(name: taxon_name)
  product = Spree::Product.find_by!(name: product_name.titleize)
  color = color_option_values.find_by!(name: color_name)

  size_option_values.each do |size|
    if parent_name == 'Women' and %w[Dresses Skirts].include?(taxon_name)
      length_option_values.each do |length|
        option_values = [color, length, size]
        product.variants.first_or_create! do |variant|
          variant.cost_price = product.price
          variant.option_values = option_values
          variant.sku = product.sku + '_' + option_values.map(&:name).join('_')
          variant.tax_category = clothing_tax_category
        end
      end
    else
      option_values = [color, size]
      product.variants.first_or_create! do |variant|
        variant.cost_price = product.price
        variant.option_values = option_values
        variant.sku = product.sku + '_' + option_values.map(&:name).join('_')
        variant.tax_category = clothing_tax_category

      end
    end
  end
end

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
spree_sample-4.1.15 db/samples/variants.rb
spree_sample-4.2.7 db/samples/variants.rb
spree_sample-4.3.3 db/samples/variants.rb
spree_sample-4.4.1 db/samples/variants.rb
spree_sample-4.2.6 db/samples/variants.rb
spree_sample-4.3.2 db/samples/variants.rb
spree_sample-4.4.0 db/samples/variants.rb
spree_sample-4.4.0.rc2 db/samples/variants.rb
spree_sample-4.3.1 db/samples/variants.rb
spree_sample-4.4.0.rc1 db/samples/variants.rb
spree_sample-4.3.0 db/samples/variants.rb
spree_sample-4.3.0.rc3 db/samples/variants.rb
spree_sample-4.3.0.rc2 db/samples/variants.rb
spree_sample-4.3.0.rc1 db/samples/variants.rb
spree_sample-4.2.5 db/samples/variants.rb
spree_sample-4.1.14 db/samples/variants.rb
spree_sample-4.2.4 db/samples/variants.rb
spree_sample-4.2.3.1 db/samples/variants.rb
spree_sample-4.1.13.1 db/samples/variants.rb
spree_sample-4.2.3 db/samples/variants.rb