I have created datapatch to delete products with specific value on attribute manufacturer.
I want to delete all products whose manufacturer is LG , my datapatch looks like that
< ?php
declare(strict_types=1);
namespace DevAllExtensionSettingConfigSetupPatchData;
use MagentoFrameworkExceptionStateException;
use MagentoFrameworkSetupPatchDataPatchInterface;
use MagentoEavSetupEavSetup;
use MagentoEavApiAttributeRepositoryInterface;
use MagentoCatalogModelResourceModelProductAttributeCollectionFactory;
use MagentoCatalogModelProductRepository;
use MagentoFrameworkApiSearchCriteriaBuilder;
class RemoveLGManufacturer implements DataPatchInterface
{
private $eavSetupFactory;
private $attributeRepository;
private $collectionFactory;
private $productRepository;
private $searchCriteriaBuilder;
public function __construct(
SearchCriteriaBuilder $searchCriteriaBuilder,
AttributeRepositoryInterface $attributeRepository,
ProductRepository $productRepository,
array $data = []
) {
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->productRepository = $productRepository;
$this->attributeRepository = $attributeRepository;
}
/**
* @inheritDoc
*/
public static function getDependencies()
{
return [];
}
/**
* @inheritDoc
*/
public function getAliases()
{
return [];
}
public function apply()
{
$searchCriteria = $this->searchCriteriaBuilder->addFilter('manufacturer','LG')->create();
$products = $this->productRepository->getList($searchCriteria)->getItems();
foreach ($products as $product) {
try {
$this->productRepository->delete($product);
} catch (StateException $e) {
}
}
}
}
For me it seems kind a correct but doesn’t do the job.
debugged it and it takes wrong products ( 3 products when in database there are a lot ).
What can be the problem ?