modules/acm_sku/src/Commands/AcmSkuDrushCommands.php
public function syncCategories() {
    $this->output->writeln(dt('Synchronizing all commerce categories, please wait...'));
    $response = $this->acmCategoryManager->synchronizeTree($this->categoryVid);

    // We trigger delete only if there is any term update/create.
    // So if API does not return anything, we don't delete all the categories.
    if (!empty($response['created']) || !empty($response['updated'])) {
      // Get all category terms with commerce id.
      $orphan_categories = $this->acmCategoryManager->getOrphanCategories($response);

      // If there are categories to delete.
      if (!empty($orphan_categories)) {
        // Show `tid + cat name + commerce id` for review.
        $this->io()->table([
          dt('Category Id'),
          dt('Category Name'),
          dt('Category Commerce Id'),
        ], $orphan_categories);

        // Confirmation to delete old categories.
        if ($this->io()->confirm(dt('Are you sure you want to clean these old categories'), FALSE)) {

          // Allow other modules to skipping the deleting of terms.
          $this->moduleHandler->alter('acm_sku_sync_categories_delete', $orphan_categories);

          foreach ($orphan_categories as $tid => $rs) {
            $term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid);
            if ($term instanceof TermInterface) {
              // Delete the term.
              $term->delete();
            }
          }
        }
      }
    }
    else {
      $this->logger->notice(dt('Not cleaning(deleting) old terms as there is no term update/create.'));
    }

    $this->output->writeln(dt('Done.'));
  }
public function syncCategories() {
    $this->output->writeln(dt('Synchronizing all commerce categories, please wait...'));
    $response = $this->acmCategoryManager->synchronizeTree($this->categoryVid);

    // We trigger delete only if there is any term update/create.
    // So if API does not return anything, we don't delete all the categories.
    if (!empty($response['created']) || !empty($response['updated'])) {
      // Get all category terms with commerce id.
      $orphan_categories = $this->acmCategoryManager->getOrphanCategories($response);

      // If there are categories to delete.
      if (!empty($orphan_categories)) {
        // Show `tid + cat name + commerce id` for review.
        $this->io()->table([
          dt('Category Id'),
          dt('Category Name'),
          dt('Category Commerce Id'),
        ], $orphan_categories);

        // Confirmation to delete old categories.
        if ($this->io()->confirm(dt('Are you sure you want to clean these old categories'), FALSE)) {

          // Allow other modules to skipping the deleting of terms.
          $this->moduleHandler->alter('acm_sku_sync_categories_delete', $orphan_categories);

          foreach ($orphan_categories as $tid => $rs) {
            $term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid);
            if ($term instanceof TermInterface) {
              // Delete the term.
              $term->delete();
            }
          }
        }
      }
    }
    else {
      $this->logger->notice(dt('Not cleaning(deleting) old terms as there is no term update/create.'));
    }

    $this->output->writeln(dt('Done.'));
  }
public function syncCategories ( ) { $ this -> output -> writeln ( dt ( 'Synchronizing all commerce categories, please wait...' ) ) ; $ response = $ this -> acmCategoryManager -> synchronizeTree ( $ this -> categoryVid ) ; // We trigger delete only if there is any term update/create. // So if API does not return anything, we don't delete all the categories. if ( ! empty ( $ response [ 'created' ] ) || ! empty ( $ response [ 'updated' ] ) ) { // Get all category terms with commerce id. $ orphan_categories = $ this -> acmCategoryManager -> getOrphanCategories ( $ response ) ; // If there are categories to delete. if ( ! empty ( $ orphan_categories ) ) { // Show `tid + cat name + commerce id` for review. $ this -> io ( ) -> table ( [ dt ( 'Category Id' ) , dt ( 'Category Name' ) , dt ( 'Category Commerce Id' ) , ] , $ orphan_categories ) ; // Confirmation to delete old categories. if ( $ this -> io ( ) -> confirm ( dt ( 'Are you sure you want to clean these old categories' ) , FALSE ) ) { // Allow other modules to skipping the deleting of terms. $ this -> moduleHandler -> alter ( 'acm_sku_sync_categories_delete' , $ orphan_categories ) ; foreach ( $ orphan_categories as $ tid => $ rs ) { $ term = $ this -> entityTypeManager -> getStorage ( 'taxonomy_term' ) -> load ( $ tid ) ; if ( $ term instanceof TermInterface ) { // Delete the term. $ term -> delete ( ) ; } } } } } else { $ this -> logger -> notice ( dt ( 'Not cleaning(deleting) old terms as there is no term update/create.' ) ) ; } $ this -> output -> writeln ( dt ( 'Done.' ) ) ; }
Run a full synchronization of all commerce product category records.

@command acm_sku:sync-categories

@validate-module-enabled acm_sku

@aliases acsc,sync-commerce-cats

@usage drush acsc
Run a full category synchronization of all available categories.
Run a full synchronization of all commerce product category records .