3

I have set the attribute color as unque value but still can add same value and i have tried with an event catalog_entity_attribute_save_before but now he delete all the label and values please any help here is my code :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_entity_attribute_save_before">
        <observer name="color_swatches" instance="Description\Set\Observer\ChangeColorOptions" />
    </event>
</config>

and here is my helper that i called then in the observer :

<?php

namespace Description\Set\Helper;
use \Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{
    protected $_resource;

    public function __construct(
        \Magento\Framework\App\ResourceConnection $resource
    )
    {
        $this->_resource = $resource;
    }

    public function checkColorExists($color)
    {
        $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);

        $result = $connection->fetchAll("SELECT  DISTINCT value FROM `eav_attribute_option_value` WHERE option_id IN (SELECT option_id FROM `eav_attribute_option` WHERE `attribute_id` = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'color'))");
        foreach ($result as &$value) {
            $value = $value['value'];
        }
        if (!in_array($color, $result)) {

            return true;
        }
    }
}

and this is the observer:

<?php

namespace Description\Set\Observer;
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;




class ChangeColorOptions implements \Magento\Framework\Event\ObserverInterface
{
    protected $dataHelper;
    protected $_productAttributeRepository;
    protected $eavSetupFactory;
    protected $setup;
    protected $_resource;
    protected $messageManager;



    public function __construct(

        \Description\Set\Helper\Data $dataHelper,
        \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository,
        EavSetupFactory $eavSetupFactory,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        ModuleDataSetupInterface $setup,
        \Magento\Framework\App\ResourceConnection $resource


    )
    {

        $this->dataHelper = $dataHelper;
        $this->_productAttributeRepository = $productAttributeRepository;
        $this->eavSetupFactory = $eavSetupFactory;
        $this->setup = $setup;
        $this->_resource = $resource;
        $this->messageManager = $messageManager;


    }

    public function execute(\Magento\Framework\Event\Observer $observer)

    {


        //$options = $this->_productAttributeRepository->get('color')->getOptions();


        $attributeCode = 'color';
        $attributeId = 93;
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $attributeModel = $objectManager->create('Magento\Customer\Model\Attribute')->load($attributeId);
        $options = $attributeModel->getSource()->getAllOptions(); //get all options
        $resource = $objectManager->create('Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        $myTable = $this->_resource->getTableName('eav_attribute_option_value');

        foreach ($options as $value) {


            $result = $this->dataHelper->checkColorExists($value['label']);

            if (!$result) {
                $this->messageManager->addError(__("This color exists yet."));
                $test = $value['value'];
                $connection->delete($myTable,['option_id = ?' => $test]);




            }


        }



    }
}

now i even the new color does not exist in the options it show me error and delete all the values any idea please and thanks in advance

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.