I want that when i add an existing color an exception will be raised so how to proceed and which file do i have to override and thanks in advance.


You can create a custom function to check color value already exists or not as follows:

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)) {
        // Throw exception
    else {
        return true;

Hope this may helpful!

  • do i have to put this code in observer event catalog_entity_attribute_save_before to check or in other place and thanks for your replay – Developper Magento May 30 '19 at 9:33
  • Thanks for the upvote. I have not tried yet catalog_entity_attribute_save_before event, but yes ofcourse try once. And if works fine, then please accept this answer. – Dhara Bhatti May 30 '19 at 9:36
  • ok i will check this immediatly – Developper Magento May 30 '19 at 9:42
  • i can't understand this part of your code foreach ($result as &$value) { $value = $value['value']; } what that mean – Developper Magento May 30 '19 at 9:48
  • Please do echo "<pre>"; print_r($result); before and after foreach. You will get idea. – Dhara Bhatti May 30 '19 at 9:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.