Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions app/code/Magento/Catalog/Observer/DeleteUrlKeys.php
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]);
Copy link
Contributor

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.

Copy link
Author

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.

}
catch (\Exception $exception)
{
throw new CouldNotDeleteException(__(
'Could not delete the url rewrite(s): %1',
$exception->getMessage()
));
}

return $this;
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/Catalog/etc/adminhtml/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
<event name="cms_wysiwyg_images_static_urls_allowed">
<observer name="catalog_wysiwyg" instance="Magento\Catalog\Observer\CatalogCheckIsUsingStaticUrlsAllowedObserver" />
</event>
<event name="attribute_set_delete_after">
<observer name="delete_url_keys" instance="Magento\Catalog\Observer\DeleteUrlKeys" />
</event>
</config>
20 changes: 20 additions & 0 deletions app/code/Magento/Eav/Model/AttributeSetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
Expand All @@ -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();
}

Expand Down Expand Up @@ -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]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catalog must not be referred in Eav module. New events must not be introduced when it's enough extension points to create a plugin-interceptor.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right! I'll get on this later today 👍

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Author

@patrikpihlstrom patrikpihlstrom Oct 11, 2017

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}

Expand Down