public function MigrateProduct::onPreRowDelete in Commerce Migrate 3.1.x
Same name and namespace in other branches
- 8.2 src/EventSubscriber/MigrateProduct.php \Drupal\commerce_migrate\EventSubscriber\MigrateProduct::onPreRowDelete()
- 3.0.x src/EventSubscriber/MigrateProduct.php \Drupal\commerce_migrate\EventSubscriber\MigrateProduct::onPreRowDelete()
Reacts to the PRE_ROW_DELETE event.
For commerce_product rollbacks we want to preserve the product variations and prevent the 'usual' commerce behavior of deleting all variations belonging to a product when deleting that product. That is done by unsetting the variations field on the product and saving the product before it is deleted.
Parameters
\Drupal\migrate\Event\MigrateRowDeleteEvent $event: The event.
File
- src/
EventSubscriber/ MigrateProduct.php, line 37
Class
- MigrateProduct
- Handles product and product variation references.
Namespace
Drupal\commerce_migrate\EventSubscriberCode
public function onPreRowDelete(MigrateRowDeleteEvent $event) {
$destination_config = $event
->getMigration()
->getDestinationConfiguration();
if ($destination_config['plugin'] === "entity:commerce_product") {
$product_ids = $event
->getDestinationIdValues();
foreach ($product_ids as $product_id) {
/** @var \Drupal\commerce_product\Entity\product $product */
$product = Product::load($product_id);
if ($product && $product
->hasVariations()) {
// Save the product, keeping the variations.
$product
->setVariations([]);
$product
->save();
}
}
}
}