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
+ }
0 commit comments