Skip to content

Commit 284f966

Browse files
author
Patrik Pihlström
committed
delete url rewrites for products after an attribute set is deleted
1 parent 0e29169 commit 284f966

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: patrikpihlstrom
5+
* Date: 11/10/17
6+
* Time: 22:12
7+
*/
8+
9+
namespace Magento\CatalogUrlRewrite\Plugin\Eav\Model;
10+
11+
12+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
13+
use Magento\Eav\Api\Data\AttributeSetInterface;
14+
use Magento\UrlRewrite\Model\UrlPersistInterface;
15+
16+
class AttributeSetRepository
17+
{
18+
/**
19+
* @var \Magento\UrlRewrite\Model\UrlPersistInterface $urlPersist
20+
*/
21+
private $urlPersist;
22+
23+
/**
24+
* @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection
25+
*/
26+
private $productCollection;
27+
28+
/**
29+
* AttributeSetRepository constructor.
30+
* @param \Magento\UrlRewrite\Model\UrlPersistInterface $urlPersist
31+
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection
32+
*/
33+
public function __construct(UrlPersistInterface $urlPersist, Collection $productCollection)
34+
{
35+
$this->urlPersist = $urlPersist;
36+
$this->productCollection = $productCollection;
37+
}
38+
39+
/**
40+
* Remove product url rewrites when an attribute set is deleted.
41+
*
42+
* @param \Magento\Eav\Model\AttributeSetRepository $subject
43+
* @param callable $proceed
44+
* @param AttributeSetInterface $attributeSet
45+
*/
46+
public function aroundDelete(\Magento\Eav\Model\AttributeSetRepository $subject, callable $proceed,
47+
AttributeSetInterface $attributeSet)
48+
{
49+
// Get the product ids
50+
$ids = $this->productCollection->addFieldToFilter('attribute_set_id', $attributeSet->getAttributeSetId())
51+
->getAllIds();
52+
53+
// Delete the attribute set
54+
$result = $proceed($attributeSet);
55+
56+
// Delete the old product url rewrites
57+
try
58+
{
59+
$this->urlPersist->deleteByData(['entity_id' => $ids, 'entity_type' => 'product']);
60+
}
61+
catch (\Exception $exception)
62+
{
63+
throw new CouldNotDeleteException(__('Could not delete the url rewrite(s): %1', $exception->getMessage()));
64+
}
65+
66+
return $result;
67+
}
68+
}

app/code/Magento/CatalogUrlRewrite/etc/di.xml

+3
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@
3131
</argument>
3232
</arguments>
3333
</type>
34+
<type name="Magento\Eav\Model\AttributeSetRepository">
35+
<plugin name="attribute_set_plugin" type="\Magento\CatalogUrlRewrite\Plugin\Eav\Model\AttributeSetRepository" />
36+
</type>
3437
</config>

0 commit comments

Comments
 (0)