Sha256: accf984e38e1cf1ee409d30cf784b25fd7ec0dad792f38eb45c0875439d14002

Contents?: true

Size: 1.1 KB

Versions: 16

Compression:

Stored size: 1.1 KB

Contents

module Workarea
  module Admin
    module BulkVariantSaving
      def save_variant_on_product(product, attributes: {}, variant: nil)
        variant ||= product.variants.build

        variant.sku = attributes[:sku]
        variant.update_details(
          attributes[:detail_1_name] => attributes[:detail_1_value],
          attributes[:detail_2_name] => attributes[:detail_2_value],
          attributes[:detail_3_name] => attributes[:detail_3_value]
        )
        variant.save!

        if attributes[:price].present? || attributes[:tax_code].present?
          pricing = Pricing::Sku.find_or_create_by(id: variant.sku)
          pricing.tax_code = attributes[:tax_code]
          price = pricing.prices.first || pricing.prices.build
          price.regular = attributes[:price]
          pricing.save!
        end

        if attributes[:inventory].present?
          inventory = Inventory::Sku.find_or_initialize_by(id: variant.sku)
          inventory.available = attributes[:inventory]
          inventory.policy = 'standard' if inventory.new_record?
          inventory.save!
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
workarea-admin-3.5.27 app/controllers/workarea/admin/bulk_variant_saving.rb
workarea-admin-3.5.26 app/controllers/workarea/admin/bulk_variant_saving.rb
workarea-admin-3.4.45 app/controllers/workarea/admin/bulk_variant_saving.rb
workarea-admin-3.5.25 app/controllers/workarea/admin/bulk_variant_saving.rb
workarea-admin-3.5.23 app/controllers/workarea/admin/bulk_variant_saving.rb
workarea-admin-3.4.44 app/controllers/workarea/admin/bulk_variant_saving.rb
workarea-admin-3.5.22 app/controllers/workarea/admin/bulk_variant_saving.rb
workarea-admin-3.4.43 app/controllers/workarea/admin/bulk_variant_saving.rb
workarea-admin-3.5.21 app/controllers/workarea/admin/bulk_variant_saving.rb
workarea-admin-3.4.42 app/controllers/workarea/admin/bulk_variant_saving.rb
workarea-admin-3.5.20 app/controllers/workarea/admin/bulk_variant_saving.rb
workarea-admin-3.4.41 app/controllers/workarea/admin/bulk_variant_saving.rb
workarea-admin-3.5.19 app/controllers/workarea/admin/bulk_variant_saving.rb
workarea-admin-3.4.40 app/controllers/workarea/admin/bulk_variant_saving.rb
workarea-admin-3.5.18 app/controllers/workarea/admin/bulk_variant_saving.rb
workarea-admin-3.4.39 app/controllers/workarea/admin/bulk_variant_saving.rb