-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
delete url rewrites for products after an attribute set is deleted
- Loading branch information
Patrik Pihlström
committed
Oct 11, 2017
1 parent
0e29169
commit 284f966
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
app/code/Magento/CatalogUrlRewrite/Plugin/Eav/Model/AttributeSetRepository.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: patrikpihlstrom | ||
* Date: 11/10/17 | ||
* Time: 22:12 | ||
*/ | ||
|
||
namespace Magento\CatalogUrlRewrite\Plugin\Eav\Model; | ||
|
||
|
||
use Magento\Catalog\Model\ResourceModel\Product\Collection; | ||
use Magento\Eav\Api\Data\AttributeSetInterface; | ||
use Magento\UrlRewrite\Model\UrlPersistInterface; | ||
|
||
class AttributeSetRepository | ||
{ | ||
/** | ||
* @var \Magento\UrlRewrite\Model\UrlPersistInterface $urlPersist | ||
*/ | ||
private $urlPersist; | ||
|
||
/** | ||
* @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection | ||
*/ | ||
private $productCollection; | ||
|
||
/** | ||
* AttributeSetRepository constructor. | ||
* @param \Magento\UrlRewrite\Model\UrlPersistInterface $urlPersist | ||
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection | ||
*/ | ||
public function __construct(UrlPersistInterface $urlPersist, Collection $productCollection) | ||
{ | ||
$this->urlPersist = $urlPersist; | ||
$this->productCollection = $productCollection; | ||
} | ||
|
||
/** | ||
* Remove product url rewrites when an attribute set is deleted. | ||
* | ||
* @param \Magento\Eav\Model\AttributeSetRepository $subject | ||
* @param callable $proceed | ||
* @param AttributeSetInterface $attributeSet | ||
*/ | ||
public function aroundDelete(\Magento\Eav\Model\AttributeSetRepository $subject, callable $proceed, | ||
AttributeSetInterface $attributeSet) | ||
{ | ||
// Get the product ids | ||
$ids = $this->productCollection->addFieldToFilter('attribute_set_id', $attributeSet->getAttributeSetId()) | ||
->getAllIds(); | ||
|
||
// Delete the attribute set | ||
$result = $proceed($attributeSet); | ||
|
||
// Delete the old product url rewrites | ||
try | ||
{ | ||
$this->urlPersist->deleteByData(['entity_id' => $ids, 'entity_type' => 'product']); | ||
} | ||
catch (\Exception $exception) | ||
{ | ||
throw new CouldNotDeleteException(__('Could not delete the url rewrite(s): %1', $exception->getMessage())); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters