-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
delete url rewrites for all products of an attribute set when said at… #11290
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Catalog\Observer; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\Event\ObserverInterface; | ||
|
||
/** | ||
* Delete the url key of a product on product deletion | ||
*/ | ||
class DeleteUrlKeys implements ObserverInterface | ||
{ | ||
/** @var ResourceConnection $resourceConnection */ | ||
private $resourceConnection; | ||
|
||
public function __construct( | ||
ResourceConnection $resourceConnection | ||
) | ||
{ | ||
$this->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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,18 +52,28 @@ 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 | ||
* @param CollectionFactory $collectionFactory | ||
* @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]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right! I'll get on this later today 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that the removal of an attribute set calls any delete function on a per product level, at least as far as I can tell. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Products are dropped due to foreign keys? Maybe similar constraints should be added for product URL Rewrites? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That isn't possible since there are other url rewrites in the table that relate to different entity types. Or am I just not seeing something that's super obvious? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right https://stackoverflow.com/a/28322144/8640867 :) That's our case, needs to be handled on application side, please implement as an interceptor attached to attribute set entity removal. |
||
} 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; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
\Magento\UrlRewrite\Model\UrlPersistInterface::deleteByData
must be used instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was looking for a more elegant way of deleting the entries.