modules/acm_sku/src/Commands/AcmSkuDrushCommands.php
public function removeCategoryDuplicates() {
    $this->output->writeln(dt('Cleaning all commerce categories, please wait...'));

    $db = $this->connection;

    /** @var \Drupal\taxonomy\TermStorageInterface $termStorage */
    $termStorage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');

    /** @var \Drupal\node\NodeStorageInterface $nodeStorage */
    $nodeStorage = \Drupal::entityTypeManager()->getStorage('node');

    $query = $db->select('taxonomy_term__field_commerce_id', 'ttfci');
    $query->addField('ttfci', 'field_commerce_id_value', 'commerce_id');
    $query->groupBy('commerce_id');
    $query->having('count(*) > 1');
    $result = $query->execute()->fetchAllKeyed(0, 0);

    if (empty($result)) {
      $this->output->writeln(dt('No duplicate categories found.'));
      return;
    }

    foreach ($result as $commerce_id) {
      $this->output->writeln(dt('Duplicate categories found for commerce id: @commerce_id.', [
        '@commerce_id' => $commerce_id,
      ]));

      $query = $db->select('taxonomy_term__field_commerce_id', 'ttfci');
      $query->addField('ttfci', 'entity_id', 'tid');
      $query->condition('ttfci.field_commerce_id_value', $commerce_id);
      $query->orderBy('tid', 'DESC');
      $tids = $query->execute()->fetchAllKeyed(0, 0);

      foreach ($tids as $tid) {
        $query = $nodeStorage->getQuery();
        $query->condition('field_category', $tid);
        $nodes = $query->execute();

        if (empty($nodes)) {
          $this->output->writeln(dt('No nodes found for tid: @tid for commerce id: @commerce_id. Deleting', [
            '@commerce_id' => $commerce_id,
            '@tid' => $tid,
          ]));

          $term = $termStorage->load($tid);
          $term->delete();

          unset($tids[$tid]);

          // Break the loop if only one left now, we might not have any products
          // added yet and categories are synced which means there will be no
          // nodes for any term.
          if (count($tids) == 1) {
            break;
          }
        }
        else {
          $this->output->writeln(dt('@count nodes found for tid: @tid for commerce id: @commerce_id. Not Deleting', [
            '@commerce_id' => $commerce_id,
            '@tid' => $tid,
            '@count' => count($nodes),
          ]));
        }
      }
    }

    $this->output->writeln(dt('Done.'));
  }
public function removeCategoryDuplicates() {
    $this->output->writeln(dt('Cleaning all commerce categories, please wait...'));

    $db = $this->connection;

    /** @var \Drupal\taxonomy\TermStorageInterface $termStorage */
    $termStorage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');

    /** @var \Drupal\node\NodeStorageInterface $nodeStorage */
    $nodeStorage = \Drupal::entityTypeManager()->getStorage('node');

    $query = $db->select('taxonomy_term__field_commerce_id', 'ttfci');
    $query->addField('ttfci', 'field_commerce_id_value', 'commerce_id');
    $query->groupBy('commerce_id');
    $query->having('count(*) > 1');
    $result = $query->execute()->fetchAllKeyed(0, 0);

    if (empty($result)) {
      $this->output->writeln(dt('No duplicate categories found.'));
      return;
    }

    foreach ($result as $commerce_id) {
      $this->output->writeln(dt('Duplicate categories found for commerce id: @commerce_id.', [
        '@commerce_id' => $commerce_id,
      ]));

      $query = $db->select('taxonomy_term__field_commerce_id', 'ttfci');
      $query->addField('ttfci', 'entity_id', 'tid');
      $query->condition('ttfci.field_commerce_id_value', $commerce_id);
      $query->orderBy('tid', 'DESC');
      $tids = $query->execute()->fetchAllKeyed(0, 0);

      foreach ($tids as $tid) {
        $query = $nodeStorage->getQuery();
        $query->condition('field_category', $tid);
        $nodes = $query->execute();

        if (empty($nodes)) {
          $this->output->writeln(dt('No nodes found for tid: @tid for commerce id: @commerce_id. Deleting', [
            '@commerce_id' => $commerce_id,
            '@tid' => $tid,
          ]));

          $term = $termStorage->load($tid);
          $term->delete();

          unset($tids[$tid]);

          // Break the loop if only one left now, we might not have any products
          // added yet and categories are synced which means there will be no
          // nodes for any term.
          if (count($tids) == 1) {
            break;
          }
        }
        else {
          $this->output->writeln(dt('@count nodes found for tid: @tid for commerce id: @commerce_id. Not Deleting', [
            '@commerce_id' => $commerce_id,
            '@tid' => $tid,
            '@count' => count($nodes),
          ]));
        }
      }
    }

    $this->output->writeln(dt('Done.'));
  }
public function removeCategoryDuplicates ( ) { $ this -> output -> writeln ( dt ( 'Cleaning all commerce categories, please wait...' ) ) ; $ db = $ this -> connection ; /** @var \Drupal\taxonomy\TermStorageInterface $termStorage */ $ termStorage = \ Drupal :: entityTypeManager ( ) -> getStorage ( 'taxonomy_term' ) ; /** @var \Drupal\node\NodeStorageInterface $nodeStorage */ $ nodeStorage = \ Drupal :: entityTypeManager ( ) -> getStorage ( 'node' ) ; $ query = $ db -> select ( 'taxonomy_term__field_commerce_id' , 'ttfci' ) ; $ query -> addField ( 'ttfci' , 'field_commerce_id_value' , 'commerce_id' ) ; $ query -> groupBy ( 'commerce_id' ) ; $ query -> having ( 'count(*) > 1' ) ; $ result = $ query -> execute ( ) -> fetchAllKeyed ( 0 , 0 ) ; if ( empty ( $ result ) ) { $ this -> output -> writeln ( dt ( 'No duplicate categories found.' ) ) ; return ; } foreach ( $ result as $ commerce_id ) { $ this -> output -> writeln ( dt ( 'Duplicate categories found for commerce id: @commerce_id.' , [ '@commerce_id' => $ commerce_id , ] ) ) ; $ query = $ db -> select ( 'taxonomy_term__field_commerce_id' , 'ttfci' ) ; $ query -> addField ( 'ttfci' , 'entity_id' , 'tid' ) ; $ query -> condition ( 'ttfci.field_commerce_id_value' , $ commerce_id ) ; $ query -> orderBy ( 'tid' , 'DESC' ) ; $ tids = $ query -> execute ( ) -> fetchAllKeyed ( 0 , 0 ) ; foreach ( $ tids as $ tid ) { $ query = $ nodeStorage -> getQuery ( ) ; $ query -> condition ( 'field_category' , $ tid ) ; $ nodes = $ query -> execute ( ) ; if ( empty ( $ nodes ) ) { $ this -> output -> writeln ( dt ( 'No nodes found for tid: @tid for commerce id: @commerce_id. Deleting' , [ '@commerce_id' => $ commerce_id , '@tid' => $ tid , ] ) ) ; $ term = $ termStorage -> load ( $ tid ) ; $ term -> delete ( ) ; unset ( $ tids [ $ tid ] ) ; // Break the loop if only one left now, we might not have any products // added yet and categories are synced which means there will be no // nodes for any term. if ( count ( $ tids ) == 1 ) { break ; } } else { $ this -> output -> writeln ( dt ( '@count nodes found for tid: @tid for commerce id: @commerce_id. Not Deleting' , [ '@commerce_id' => $ commerce_id , '@tid' => $ tid , '@count' => count ( $ nodes ) , ] ) ) ; } } } $ this -> output -> writeln ( dt ( 'Done.' ) ) ; }
Remove all duplicate categories available in system.

@command acm_sku:remove-category-duplicates

@validate-module-enabled acm_sku

@aliases acccrd,commerce-cats-remove-duplicates

@usage drush acccrd
Remove all duplicate categories available in system.
Remove all duplicate categories available in system .