From 2304faf6249ed0ee3f71474b3b9733af3fce3fe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20Pihlstr=C3=B6m?= Date: Sat, 7 Oct 2017 18:13:46 +0200 Subject: [PATCH] delete url rewrites for all products of an attribute set when said attribute set is deleted --- .../Catalog/Observer/DeleteUrlKeys.php | 52 +++++++++++++++++++ .../Magento/Catalog/etc/adminhtml/events.xml | 3 ++ .../Eav/Model/AttributeSetRepository.php | 20 +++++++ 3 files changed, 75 insertions(+) create mode 100644 app/code/Magento/Catalog/Observer/DeleteUrlKeys.php diff --git a/app/code/Magento/Catalog/Observer/DeleteUrlKeys.php b/app/code/Magento/Catalog/Observer/DeleteUrlKeys.php new file mode 100644 index 0000000000000..c50f9f1648a41 --- /dev/null +++ b/app/code/Magento/Catalog/Observer/DeleteUrlKeys.php @@ -0,0 +1,52 @@ +resourceConnection = $resourceConnection; + } + + /** + * Delete the product url key + * + * @param \Magento\Framework\Event\Observer $observer + * @return $this + */ + public function execute(\Magento\Framework\Event\Observer $observer) + { + $ids = $observer->getEvent()->getProducts(); + $connection = $this->resourceConnection->getConnection(); + + try + { + $connection->delete('url_rewrite', ['entity_type = ?' => 'product', 'entity_id IN (?)' => $ids]); + } + catch (\Exception $exception) + { + throw new CouldNotDeleteException(__( + 'Could not delete the url rewrite(s): %1', + $exception->getMessage() + )); + } + + return $this; + } +} diff --git a/app/code/Magento/Catalog/etc/adminhtml/events.xml b/app/code/Magento/Catalog/etc/adminhtml/events.xml index f4fd7fc30398c..5e546810985b0 100644 --- a/app/code/Magento/Catalog/etc/adminhtml/events.xml +++ b/app/code/Magento/Catalog/etc/adminhtml/events.xml @@ -9,4 +9,7 @@ + + + diff --git a/app/code/Magento/Eav/Model/AttributeSetRepository.php b/app/code/Magento/Eav/Model/AttributeSetRepository.php index d0b7ed3ccaa28..c9d4f42105266 100644 --- a/app/code/Magento/Eav/Model/AttributeSetRepository.php +++ b/app/code/Magento/Eav/Model/AttributeSetRepository.php @@ -52,11 +52,19 @@ class AttributeSetRepository implements AttributeSetRepositoryInterface */ protected $joinProcessor; + /** + * @var \Magento\Framework\Event\ManagerInterface + */ + private $eventManager; + /** * @var CollectionProcessorInterface */ private $collectionProcessor; + /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection */ + private $productCollection; + /** * @param AttributeSetResource $attributeSetResource * @param AttributeSetFactory $attributeSetFactory @@ -64,6 +72,8 @@ class AttributeSetRepository implements AttributeSetRepositoryInterface * @param Config $eavConfig * @param \Magento\Eav\Api\Data\AttributeSetSearchResultsInterfaceFactory $searchResultFactory * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor + * @param \Magento\Framework\Event\ManagerInterface $eventManager + * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection * @param CollectionProcessorInterface $collectionProcessor * @codeCoverageIgnore */ @@ -74,6 +84,8 @@ public function __construct( EavConfig $eavConfig, \Magento\Eav\Api\Data\AttributeSetSearchResultsInterfaceFactory $searchResultFactory, \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor, + \Magento\Framework\Event\ManagerInterface $eventManager, + \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection, CollectionProcessorInterface $collectionProcessor = null ) { $this->attributeSetResource = $attributeSetResource; @@ -82,6 +94,8 @@ public function __construct( $this->eavConfig = $eavConfig; $this->searchResultsFactory = $searchResultFactory; $this->joinProcessor = $joinProcessor; + $this->eventManager = $eventManager; + $this->productCollection = $productCollection; $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor(); } @@ -156,13 +170,19 @@ public function get($attributeSetId) */ public function delete(AttributeSetInterface $attributeSet) { + // Get the affected product ids + $productIds = $this->productCollection + ->addFieldToFilter('attribute_set_id', $attributeSet->getAttributeSetId())->getAllIds(); + try { $this->attributeSetResource->delete($attributeSet); + $this->eventManager->dispatch('attribute_set_delete_after', ['products' => $productIds]); } catch (\Magento\Framework\Exception\StateException $exception) { throw new CouldNotDeleteException(__('Default attribute set can not be deleted')); } catch (\Exception $exception) { throw new CouldNotDeleteException(__('There was an error deleting attribute set.')); } + return true; }