You are here

class MigrateOrder in Commerce Migrate 8.2

Same name and namespace in other branches
  1. 3.1.x src/EventSubscriber/MigrateOrder.php \Drupal\commerce_migrate\EventSubscriber\MigrateOrder
  2. 3.0.x src/EventSubscriber/MigrateOrder.php \Drupal\commerce_migrate\EventSubscriber\MigrateOrder

Handles order and order variation references.

@package \Drupal\commerce_migrate\EventSubscriber

Hierarchy

  • class \Drupal\commerce_migrate\EventSubscriber\MigrateOrder implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of MigrateOrder

1 string reference to 'MigrateOrder'
commerce_migrate.services.yml in ./commerce_migrate.services.yml
commerce_migrate.services.yml
1 service uses MigrateOrder
commerce_migrate.migrate_order in ./commerce_migrate.services.yml
Drupal\commerce_migrate\EventSubscriber\MigrateOrder

File

src/EventSubscriber/MigrateOrder.php, line 15

Namespace

Drupal\commerce_migrate\EventSubscriber
View source
class MigrateOrder implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[MigrateEvents::PRE_ROW_DELETE][] = 'onPreRowDelete';
    return $events;
  }

  /**
   * Reacts to the PRE_ROW_DELETE event.
   *
   * For commerce_order rollbacks we want to preserve the order items and
   * prevent the 'usual' commerce behavior of deleting all order items
   * belonging to an order when deleting that order. That is done by
   * removing all order items from the order and saving the order.
   *
   * The extra steps to remove the order items from the order may significantly
   * increase the time to perform a rollback. To mitigate this it is best to use
   * drush and limit the number of records migrated during development. This can
   * be done with the 'limit' option or the 'idlist' option, as shown in the
   * following examples.
   * @code
   * drush migrate-import --limit=5 some_migration
   * drush migrate-import --idlist=42 some_migration
   * @endcode
   *
   * It is also possible to disable this feature and use a custom event
   * subscriber. Instructions on how to do that is in the Symfony documentation.
   *
   * @see https://symfony.com/doc/current/components/event_dispatcher.html#stopping-event-flow-propagation
   *
   * @param \Drupal\migrate\Event\MigrateRowDeleteEvent $event
   *   The event.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function onPreRowDelete(MigrateRowDeleteEvent $event) {
    $destination_config = $event
      ->getMigration()
      ->getDestinationConfiguration();
    if ($destination_config['plugin'] === "entity:commerce_order") {
      $order_ids = $event
        ->getDestinationIdValues();
      foreach ($order_ids as $order_id) {

        /** @var \Drupal\commerce_order\Entity\Order $order */
        $order = Order::load($order_id);
        if ($order && $order
          ->hasItems()) {

          // Save the order, keeping the variations.

          /** @var \Drupal\commerce_order\Entity\OrderItem $item */
          foreach ($order
            ->getItems() as $item) {
            $order
              ->removeItem($item);
          }
          $order
            ->save();
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateOrder::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
MigrateOrder::onPreRowDelete public function Reacts to the PRE_ROW_DELETE event.