Commit 395d8cfb authored by Daniel Pepin's avatar Daniel Pepin
Browse files

Issue #2536510: Setup deleting rogue collections during sync.

parent 41294355
Loading
Loading
Loading
Loading
+40 −7
Changes for src/Batch/ShopifyCollectionBatch.php: 40 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -61,13 +61,14 @@ class ShopifyCollectionBatch {
      ];
    }

//    if (!$params['delete_collections']) {
//      // Last operation to delete orphaned collections (deleted in Shopify).
//      $operations[] = array(
//        'shopify_sync_collections_deleted_batch_op',
//        array(),
//      );
//    }
    if (!$settings['delete_collections_first']) {
      $this->operations[] = [
        [__CLASS__, 'cleanUpCollections'],
        [
          t('(Deleting left over collections.)'),
        ],
      ];
    }

    $this->batch = array(
      'operations' => $this->operations,
@@ -93,6 +94,38 @@ class ShopifyCollectionBatch {
    $context['message'] = $operation_details;
  }

  /**
   * Deletes collections on the site that don't exist on Shopify anymore.
   */
  public static function cleanUpCollections($operation_details, &$context) {
    $collections = shopify_api_get_collections(['query' => ['fields' => 'id']]);
    $collection_ids = [];

    // Build up array of all existing collection_ids.
    foreach ($collections as $col) {
      $collection_ids[] = $col->id;
    }

    // Get collections that are not on Shopify anymore.
    $query = \Drupal::entityQuery('taxonomy_term');
    $query->condition('vid', 'shopify_collections');
    $query->condition('field_shopify_collection_id', $collection_ids, 'NOT IN');
    $result = $query->execute();

    // Delete these collections.
    if ($result) {
      $manager = \Drupal::entityManager()
        ->getStorage('taxonomy_term');
      $collection_entities = $manager->loadMultiple($result);
      $manager->delete($collection_entities);
      drupal_set_message(t('Deleted @collections.', [
        '@collections' => \Drupal::translation()
          ->formatPlural(count($collection_entities), '@count collection', '@count collections'),
      ]));
      $context['message'] = $operation_details;
    }
  }

  /**
   * Product sync operation.
   */
+2 −1
Changes for src/Batch/ShopifyProductBatch.php: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -106,7 +106,7 @@ class ShopifyProductBatch {
  /**
   * Deletes products on the site that don't exist on Shopify anymore.
   */
  public function cleanUpProducts($operation_details, &$context) {
  public static function cleanUpProducts($operation_details, &$context) {
    // Get all Shopify product_ids and variant_ids.
    $client = shopify_api_client();
    $products = $client->getProducts(['query' => ['fields' => 'id,variants']]);
@@ -140,6 +140,7 @@ class ShopifyProductBatch {
        '@products' => \Drupal::translation()
          ->formatPlural(count($product_entities), '@count product', '@count products'),
      ]));
      $context['message'] = $operation_details;
    }

    // Go ahead and delete all rogue variants.