Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Eraser.php
Go to the documentation of this file.
1 <?php
9 
11 
12 class Eraser
13 {
18 
22  protected $connection;
23 
27  protected $storeManager;
28 
34  public function __construct(
36  \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper,
38  ) {
39  $this->productIndexerHelper = $productHelper;
40  $this->connection = $resource->getConnection();
41  $this->storeManager = $storeManager;
42  }
43 
51  public function removeDeletedProducts(array &$ids, $storeId)
52  {
53  $select = $this->connection->select()->from(
54  $this->productIndexerHelper->getTable('catalog_product_entity')
55  )->where(
56  'entity_id IN(?)',
57  $ids
58  );
59  $result = $this->connection->query($select);
60 
61  $existentProducts = [];
62  foreach ($result->fetchAll() as $product) {
63  $existentProducts[] = $product['entity_id'];
64  }
65 
66  $productsToDelete = array_diff($ids, $existentProducts);
67  $ids = $existentProducts;
68 
70  }
71 
79  public function deleteProductsFromStore($productId, $storeId = null)
80  {
81  if (!is_array($productId)) {
83  }
84  if (null === $storeId) {
85  foreach ($this->storeManager->getStores() as $store) {
86  $this->connection->delete(
87  $this->productIndexerHelper->getFlatTableName($store->getId()),
88  ['entity_id IN(?)' => $productId]
89  );
90  }
91  } else {
92  $this->connection->delete(
93  $this->productIndexerHelper->getFlatTableName((int)$storeId),
94  ['entity_id IN(?)' => $productId]
95  );
96  }
97  }
98 }
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\App\ResourceConnection $resource, \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper, \Magento\Store\Model\StoreManagerInterface $storeManager)
Definition: Eraser.php:34
deleteProductsFromStore($productId, $storeId=null)
Definition: Eraser.php:79
$productsToDelete