Class: Spree::CSV::ProductVariantPresenter

Inherits:
Object
  • Object
show all
Includes:
ImagesHelper
Defined in:
app/presenters/spree/csv/product_variant_presenter.rb

Constant Summary collapse

CSV_HEADERS =
[
  'product_id',
  'sku',
  'name',
  'slug',
  'status',
  'vendor_name',
  'brand_name',
  'description',
  'meta_title',
  'meta_description',
  'meta_keywords',
  'tags',
  'labels',
  'price',
  'compare_at_price',
  'currency',
  'width',
  'height',
  'depth',
  'dimensions_unit',
  'weight',
  'weight_unit',
  'available_on',
  'discontinue_on',
  'track_inventory',
  'inventory_count',
  'inventory_backorderable',
  'tax_category',
  'shipping_category',
  'image1_src',
  'image2_src',
  'image3_src',
  'option1_name',
  'option1_value',
  'option2_name',
  'option2_value',
  'option3_name',
  'option3_value',
  'category1',
  'category2',
  'category3',
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ImagesHelper

#spree_asset_aspect_ratio, #spree_image_tag, #spree_image_url, #spree_image_variant_options

Constructor Details

#initialize(product, variant, index = 0, properties = [], taxons = [], store = nil, metafields = [], currency = nil) ⇒ ProductVariantPresenter

Returns a new instance of ProductVariantPresenter.



50
51
52
53
54
55
56
57
58
59
60
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 50

def initialize(product, variant, index = 0, properties = [], taxons = [], store = nil, metafields = [], currency = nil)
  @product = product
  @variant = variant
  @index = index
  @properties = properties
  @taxons = taxons
  @store = store || product.stores.first
  @currency = currency || @store.default_currency
  @price_only = @currency != @store.default_currency
  @metafields = metafields
end

Instance Attribute Details

#currencyObject

Returns the value of attribute currency.



62
63
64
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62

def currency
  @currency
end

#indexObject

Returns the value of attribute index.



62
63
64
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62

def index
  @index
end

#metafieldsObject

Returns the value of attribute metafields.



62
63
64
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62

def metafields
  @metafields
end

#price_onlyObject

Returns the value of attribute price_only.



62
63
64
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62

def price_only
  @price_only
end

#productObject

Returns the value of attribute product.



62
63
64
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62

def product
  @product
end

#propertiesObject

Returns the value of attribute properties.



62
63
64
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62

def properties
  @properties
end

#storeObject

Returns the value of attribute store.



62
63
64
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62

def store
  @store
end

#taxonsObject

Returns the value of attribute taxons.



62
63
64
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62

def taxons
  @taxons
end

#variantObject

Returns the value of attribute variant.



62
63
64
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 62

def variant
  @variant
end

Instance Method Details

#callArray

Generates an array representing a CSV row of product variant data.

For the primary variant row (when the index is zero), product-level details such as name, slug, status, vendor and brand names, description, meta tags, and tag/label lists are included. In all cases, variant-specific attributes (e.g., id, SKU, pricing, dimensions, weight, availability dates, inventory count, shipping category, tax category, image URLs via original_url, and the first three option types and corresponding option values) are appended. Additionally, when the index is zero, associated taxons and properties are added.

Returns:

  • (Array)

    An array containing the combined product and variant CSV data.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 75

def call
  return price_only_row if price_only

  total_on_hand = variant.total_on_hand

  csv = [
    product.id,
    variant.sku,
    index.zero? ? product.name : nil,
    product.slug,
    index.zero? ? product.status : nil,
    index.zero? ? product.try(:vendor_name) : nil,
    index.zero? ? product.try(:brand_name) : nil,
    index.zero? ? product.description&.html_safe : nil,
    index.zero? ? product.meta_title : nil,
    index.zero? ? product.meta_description : nil,
    index.zero? ? product.meta_keywords : nil,
    index.zero? ? product.tag_list.to_s : nil,
    index.zero? ? product.label_list.to_s : nil,
    variant.amount_in(currency).to_f,
    variant.compare_at_amount_in(currency).to_f,
    currency,
    variant.width,
    variant.height,
    variant.depth,
    variant.dimensions_unit,
    variant.weight,
    variant.weight_unit,
    variant.available_on&.strftime('%Y-%m-%d %H:%M:%S'),
    (variant.discontinue_on || product.discontinue_on)&.strftime('%Y-%m-%d %H:%M:%S'),
    variant.track_inventory?,
    total_on_hand == BigDecimal::INFINITY ? '' : total_on_hand,
    variant.backorderable?,
    variant.tax_category&.name,
    product.shipping_category&.name,
    spree_image_url(variant.images[0], image_url_options),
    spree_image_url(variant.images[1], image_url_options),
    spree_image_url(variant.images[2], image_url_options),
    index.positive? ? option_type(0)&.presentation : nil,
    index.positive? ? option_value(option_type(0)) : nil,
    index.positive? ? option_type(1)&.presentation : nil,
    index.positive? ? option_value(option_type(1)) : nil,
    index.positive? ? option_type(2)&.presentation : nil,
    index.positive? ? option_value(option_type(2)) : nil
  ]

  if index.zero?
    csv += taxons
    csv += properties
    csv += metafields
  end

  csv
end

#option_type(index) ⇒ Object



130
131
132
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 130

def option_type(index)
  product.option_types[index]
end

#option_value(option_type) ⇒ Object



134
135
136
# File 'app/presenters/spree/csv/product_variant_presenter.rb', line 134

def option_value(option_type)
  variant.option_values.find { |ov| ov.option_type == option_type }&.presentation
end