0

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 DevAll\ExtensionSettingConfig\Setup\Patch\Data;

use Magento\Framework\Exception\StateException;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
use Magento\Catalog\Model\ProductRepository;
use Magento\Framework\Api\SearchCriteriaBuilder;
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 ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.