0

in the attribute Attribute "Manufacturer" we've several entries of the brand "LG", seems it was duplicated over and over during migration or import. I have to drop all instances of "LG" from the attribute, if products are associated with "LG" they can be deleted. I tried several ways to create data patch but could not reach success . my code looks something like that

<?php

declare(strict_types=1);

namespace DevAll\ExtensionSettingConfig\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;

class RemoveLGManufacturer implements DataPatchInterface
{
    private $eavSetupFactory;

    private $eavAttributeRepository;

    private $_productVisibility;

    private $collectionFactory;

    public function __construct(
        EavSetup $eavSetupFactory,
        AttributeRepositoryInterface $eavAttributeRepositoryInterface,
        \Magento\Catalog\Model\Product\Visibility $productVisibility,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory,
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = []
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavAttributeRepository = $eavAttributeRepositoryInterface;
    $this->collectionFactory = $collectionFactory;
        $this->_productVisibility = $productVisibility;
    }

    /**
     * @inheritDoc
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * @inheritDoc
     */
    public function getAliases()
    {
        return [];
    }

    /**
     * @return DataPatchInterface|void
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function apply()
    {
        $collection =$this->collectionFactory->create();

        $collection->addAttributeToSelect('manufacturer');
        $collection->addFieldToFilter(array(
            array('attribute' => 'manufacturer', 'eq' => 'LG'),
        ));
        $collection->setDataToAll('');

    }
}

How can I solve the problem ? thanks.

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.