Skip to content

Commit

Permalink
delete url rewrites for products after an attribute set is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrik Pihlström committed Oct 11, 2017
1 parent 0e29169 commit 284f966
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
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;
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/CatalogUrlRewrite/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@
</argument>
</arguments>
</type>
<type name="Magento\Eav\Model\AttributeSetRepository">
<plugin name="attribute_set_plugin" type="\Magento\CatalogUrlRewrite\Plugin\Eav\Model\AttributeSetRepository" />
</type>
</config>

0 comments on commit 284f966

Please sign in to comment.