diff --git a/commerce_migrate.services.yml b/commerce_migrate.services.yml index e728d95..e970948 100644 --- a/commerce_migrate.services.yml +++ b/commerce_migrate.services.yml @@ -7,3 +7,7 @@ services: class: Drupal\commerce_migrate\EventSubscriber\MigrateOrderItem tags: - { name: event_subscriber } + commerce_migrate.migrate_product: + class: Drupal\commerce_migrate\EventSubscriber\MigrateProduct + tags: + - { name: event_subscriber } diff --git a/modules/commerce/migration_templates/d7_commerce_product.yml b/modules/commerce/migration_templates/d7_commerce_product.yml index 438de61..9f01d41 100644 --- a/modules/commerce/migration_templates/d7_commerce_product.yml +++ b/modules/commerce/migration_templates/d7_commerce_product.yml @@ -16,9 +16,9 @@ process: changed: changed path: path variations: - plugin: commerce_migrate_commerce_reference + plugin: migration_lookup + migration: d7_commerce_product_variation source: variations_field - target_key: product_id destination: plugin: 'entity:commerce_product' migration_dependencies: diff --git a/modules/commerce/migration_templates/d7_commerce_product_variation.yml b/modules/commerce/migration_templates/d7_commerce_product_variation.yml index 9e5281f..80ab65c 100644 --- a/modules/commerce/migration_templates/d7_commerce_product_variation.yml +++ b/modules/commerce/migration_templates/d7_commerce_product_variation.yml @@ -6,7 +6,13 @@ migration_tags: source: plugin: d7_product process: - product_id: product_id + variation_id: product_id + # The product_id field is NULL after this migration. + # It is back reference to the product id which is + # migrated in the product migration, d7_product, and + # is run after this migration. The value is updated + # via the event subscriber MigrateProduct. + #product_id: revision_id: revision_id sku: sku title: title diff --git a/modules/commerce/tests/src/Kernel/Migrate/d7/ProductTest.php b/modules/commerce/tests/src/Kernel/Migrate/d7/ProductTest.php index 99493c4..c008a64 100644 --- a/modules/commerce/tests/src/Kernel/Migrate/d7/ProductTest.php +++ b/modules/commerce/tests/src/Kernel/Migrate/d7/ProductTest.php @@ -54,7 +54,13 @@ class ProductTest extends Commerce1TestBase { $product = Product::load(15); $variation_id = $product->variations->target_id; - $this->assertProductVariationEntity($variation_id, '0', 'TOT1-GRN-OS', '16.000000', 'USD', '1', 'Tote Bag 1', 'default'); + $this->assertProductVariationEntity($variation_id, '0', 'TOT1-GRN-OS', '16.000000', 'USD', '15', 'Tote Bag 1', 'default'); + + // Tests a product with multiple variations. + $this->assertProductEntity(26, '1', 'Commerce Guys USB Key', TRUE, ['1'], ['28', '29', '30']); + $this->assertProductVariationEntity(28, '0', 'USB-BLU-08', '11.990000', 'USD', '26', 'Storage 1', 'default'); + $this->assertProductVariationEntity(29, '0', 'USB-BLU-16', '17.990000', 'USD', '26', 'Storage 1', 'default'); + $this->assertProductVariationEntity(30, '0', 'USB-BLU-32', '29.990000', 'USD', '26', 'Storage 1', 'default'); } } diff --git a/modules/commerce/tests/src/Kernel/Migrate/d7/ProductVariationTest.php b/modules/commerce/tests/src/Kernel/Migrate/d7/ProductVariationTest.php index 4725450..634d87b 100644 --- a/modules/commerce/tests/src/Kernel/Migrate/d7/ProductVariationTest.php +++ b/modules/commerce/tests/src/Kernel/Migrate/d7/ProductVariationTest.php @@ -42,13 +42,13 @@ class ProductVariationTest extends Commerce1TestBase { * Test product variation migration from Drupal 7 Commerce to Drupal 8. */ public function testProductVariation() { - $this->assertProductVariationEntity(1, '0', 'TOT1-GRN-OS', '16.000000', 'USD', '1', 'Tote Bag 1', 'default'); - $this->assertProductVariationEntity(11, '0', 'HAT1-GRY-OS', '16.000000', 'USD', '11', 'Hat 1', 'default'); - $this->assertProductVariationEntity(19, '0', 'SHO2-PRL-04', '40.000000', 'USD', '19', 'Shoe 2', 'default'); - $this->assertProductVariationEntity(20, '0', 'SHO2-PRL-05', '40.000000', 'USD', '20', 'Shoe 2', 'default'); - $this->assertProductVariationEntity(28, '0', 'USB-BLU-08', '11.990000', 'USD', '28', 'Storage 1', 'default'); - $this->assertProductVariationEntity(29, '0', 'USB-BLU-16', '17.990000', 'USD', '29', 'Storage 1', 'default'); - $this->assertProductVariationEntity(30, '0', 'USB-BLU-32', '29.990000', 'USD', '30', 'Storage 1', 'default'); + $this->assertProductVariationEntity(1, '0', 'TOT1-GRN-OS', '16.000000', 'USD', NULL, 'Tote Bag 1', 'default'); + $this->assertProductVariationEntity(11, '0', 'HAT1-GRY-OS', '16.000000', 'USD', NULL, 'Hat 1', 'default'); + $this->assertProductVariationEntity(19, '0', 'SHO2-PRL-04', '40.000000', 'USD', NULL, 'Shoe 2', 'default'); + $this->assertProductVariationEntity(20, '0', 'SHO2-PRL-05', '40.000000', 'USD', NULL, 'Shoe 2', 'default'); + $this->assertProductVariationEntity(28, '0', 'USB-BLU-08', '11.990000', 'USD', NULL, 'Storage 1', 'default'); + $this->assertProductVariationEntity(29, '0', 'USB-BLU-16', '17.990000', 'USD', NULL, 'Storage 1', 'default'); + $this->assertProductVariationEntity(30, '0', 'USB-BLU-32', '29.990000', 'USD', NULL, 'Storage 1', 'default'); $product = ProductVariation::load(1); $this->assertEquals($product->getCreatedTime(), 1493287314); diff --git a/src/EventSubscriber/MigrateProduct.php b/src/EventSubscriber/MigrateProduct.php new file mode 100644 index 0000000..800d129 --- /dev/null +++ b/src/EventSubscriber/MigrateProduct.php @@ -0,0 +1,54 @@ +getMigration()->getDestinationConfiguration(); + + // Ensure the back reference to the product is correct for all variations + // of this product. + if ($event->getMigration()->id() == 'd7_commerce_product') { + $variations = $event->getRow()->getDestinationProperty('variations'); + $product_id = $event->getRow()->getDestinationProperty('product_id'); + $product = Product::load($product_id); + if ($product) { + foreach ($variations as $variation) { + $variation = ProductVariation::load($variation); + if ($variation) { + $variation->set('product_id', $product_id); + $variation->save(); + } + } + } + } + } + +} +