Class: Spree::Imports::RowProcessors::ProductVariant

Inherits:
Base
  • Object
show all
Defined in:
app/services/spree/imports/row_processors/product_variant.rb

Constant Summary collapse

OPTION_TYPES_COUNT =
3

Instance Attribute Summary collapse

Attributes inherited from Base

#attributes, #import, #row

Instance Method Summary collapse

Constructor Details

#initialize(row) ⇒ ProductVariant

Returns a new instance of ProductVariant.



7
8
9
10
11
# File 'app/services/spree/imports/row_processors/product_variant.rb', line 7

def initialize(row)
  super
  @store = row.store
  @product = ensure_product_exists
end

Instance Attribute Details

#productObject (readonly)

Returns the value of attribute product.



13
14
15
# File 'app/services/spree/imports/row_processors/product_variant.rb', line 13

def product
  @product
end

#storeObject (readonly)

Returns the value of attribute store.



13
14
15
# File 'app/services/spree/imports/row_processors/product_variant.rb', line 13

def store
  @store
end

Instance Method Details

#process!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/services/spree/imports/row_processors/product_variant.rb', line 15

def process!
  variant_scope = options.empty? ? product.variants_including_master : product.variants

  variant = if attributes['sku'].present?
              variant_scope.where(
                Spree::Variant.arel_table[:sku].lower.eq(attributes['sku'].strip.downcase)
              ).first || product.variants.new
            elsif options.empty?
              product.master
            else
              product.variants.new
            end

  variant.sku = attributes['sku'] if attributes['sku'].present?
  variant.cost_price = attributes['cost_price'] if attributes['cost_price'].present?
  variant.weight = attributes['weight'] if attributes['weight'].present?
  variant.height = attributes['height'] if attributes['height'].present?
  variant.width = attributes['width'] if attributes['width'].present?
  variant.depth = attributes['depth'] if attributes['depth'].present?
  variant.track_inventory = attributes['track_inventory'] if attributes['track_inventory'].present?
  variant.option_value_variants = prepare_option_value_variants

  if attributes['tax_category'].present?
    tax_category = prepare_tax_category
    variant.tax_category = tax_category if tax_category.present?
  end

  variant.save!

  if attributes['price'].present?
    currency = (attributes['currency'].presence || store.default_currency).to_s.strip.upcase
    variant.set_price(currency, attributes['price'], attributes['compare_at_price'])
  end

  if attributes['inventory_count'].present?
    variant.set_stock(attributes['inventory_count'].to_i, attributes['inventory_backorderable']&.to_b, store.default_stock_location)
  end

  handle_images(variant)

  variant
end