Sha256: 9383f75f79ecaa74344f9caf2ad9bc0988d741f1fc9a1e526019265c624f2626

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

namespace :solidus_product_assembly do
  desc 'Link legacy inventory units to an order line item'
  task upgrade: :environment do
    shipments = Spree::Shipment.includes(:inventory_units).where("spree_inventory_units.line_item_id IS NULL")

    shipments.each do |shipment|
      shipment.inventory_units.includes(:variant).group_by(&:variant).each do |variant, units|
        line_item = shipment.order.line_items.detect { |line_item| line_item.variant_id == variant.id }

        next if line_item

        begin
          master = shipment.order.products.detect { |p| variant.assemblies.include? p }.master
          supposed_line_item = shipment.order.line_items.detect { |line_item| line_item.variant_id == master.id }

          if supposed_line_item
            Spree::InventoryUnit.where(id: units.map(&:id)).update_all "line_item_id = #{supposed_line_item.id}"
          else
            puts "Couldn't find a matching line item for #{variant.name}"
          end
        rescue StandardError
          puts "Couldn't find a matching line item for #{variant.name}"
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
solidus_product_assembly-1.4.0 lib/tasks/spree2_upgrade.rake
solidus_product_assembly-1.3.0 lib/tasks/spree2_upgrade.rake
solidus_product_assembly-1.2.0 lib/tasks/spree2_upgrade.rake
solidus_product_assembly-1.1.0 lib/tasks/spree2_upgrade.rake