How to check uniquenes of color options when adding a color magento 2?Retrieve Product Attribute Label in event catalog_entity_attribute_save_beforeChange Product Description when clicking Color SwatchProduct image according to selected color option on cart page MagentoHow to add color picker to watches?How to change Magento 2 backend color?How color swatches work?how to create two different form to customer registration magento 2Customise the scrip of export Magento 2Override export page under system data transfer export magento 2how to create a dropdown attribute for product with dynamic options M2How to display options of custom attribute product magento 2?

Difference between > and >> when used with a named pipe

Did Milano or Benatar approve or comment on their namesake MCU ships?

Does an ice chest packed full of frozen food need ice?

Why was the Sega Genesis marketed as a 16-bit console?

Were Alexander the Great and Hephaestion lovers?

Someone whose aspirations exceed abilities or means

Taxi Services at Didcot

How did old MS-DOS games utilize various graphic cards?

A curious prime counting approximation or just data overfitting?

Second (easy access) account in case my bank screws up

How to handle self harm scars on the arm in work environment?

Grover algorithm for a database search: where is the quantum advantage?

How to construct an hbox with negative height?

Is counterpoint still used today?

Confusion around using "des" in sentences

Using "subway" as name for London Underground?

How does an ordinary object become radioactive?

PhD - Well known professor or well known school?

What to do when surprise and a high initiative roll conflict with the narrative?

Character descriptions

Should an arbiter claim draw at a K+R vs K+R endgame?

Should I give professor gift at the beginning of my PhD?

Impedance ratio vs. SWR

Share calendar details request from manager's manager



How to check uniquenes of color options when adding a color magento 2?


Retrieve Product Attribute Label in event catalog_entity_attribute_save_beforeChange Product Description when clicking Color SwatchProduct image according to selected color option on cart page MagentoHow to add color picker to watches?How to change Magento 2 backend color?How color swatches work?how to create two different form to customer registration magento 2Customise the scrip of export Magento 2Override export page under system data transfer export magento 2how to create a dropdown attribute for product with dynamic options M2How to display options of custom attribute product magento 2?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















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.










share|improve this question






























    0















    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.










    share|improve this question


























      0












      0








      0








      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.










      share|improve this question
















      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.







      magento2 color-swatches unique-constraint






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 30 at 8:40









      TN98

      8810




      8810










      asked May 30 at 8:26









      Developper Magento Developper Magento

      616212




      616212




















          1 Answer
          1






          active

          oldest

          votes


















          1














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



          protected $_resource;

          public function __construct(
          MagentoFrameworkAppResourceConnection $resource
          )
          $this->_resource = $resource;


          public function checkColorExists($color)
          $connection = $this->_resource->getConnection(MagentoFrameworkAppResourceConnection::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!






          share|improve this answer























          • 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 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 at 9:36











          • ok i will check this immediatly

            – Developper Magento
            May 30 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 at 9:48












          • Please do echo "<pre>"; print_r($result); before and after foreach. You will get idea.

            – Dhara Bhatti
            May 30 at 9:50











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "479"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f276699%2fhow-to-check-uniquenes-of-color-options-when-adding-a-color-magento-2%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














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



          protected $_resource;

          public function __construct(
          MagentoFrameworkAppResourceConnection $resource
          )
          $this->_resource = $resource;


          public function checkColorExists($color)
          $connection = $this->_resource->getConnection(MagentoFrameworkAppResourceConnection::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!






          share|improve this answer























          • 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 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 at 9:36











          • ok i will check this immediatly

            – Developper Magento
            May 30 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 at 9:48












          • Please do echo "<pre>"; print_r($result); before and after foreach. You will get idea.

            – Dhara Bhatti
            May 30 at 9:50















          1














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



          protected $_resource;

          public function __construct(
          MagentoFrameworkAppResourceConnection $resource
          )
          $this->_resource = $resource;


          public function checkColorExists($color)
          $connection = $this->_resource->getConnection(MagentoFrameworkAppResourceConnection::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!






          share|improve this answer























          • 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 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 at 9:36











          • ok i will check this immediatly

            – Developper Magento
            May 30 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 at 9:48












          • Please do echo "<pre>"; print_r($result); before and after foreach. You will get idea.

            – Dhara Bhatti
            May 30 at 9:50













          1












          1








          1







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



          protected $_resource;

          public function __construct(
          MagentoFrameworkAppResourceConnection $resource
          )
          $this->_resource = $resource;


          public function checkColorExists($color)
          $connection = $this->_resource->getConnection(MagentoFrameworkAppResourceConnection::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!






          share|improve this answer













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



          protected $_resource;

          public function __construct(
          MagentoFrameworkAppResourceConnection $resource
          )
          $this->_resource = $resource;


          public function checkColorExists($color)
          $connection = $this->_resource->getConnection(MagentoFrameworkAppResourceConnection::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!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 30 at 9:18









          Dhara BhattiDhara Bhatti

          427112




          427112












          • 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 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 at 9:36











          • ok i will check this immediatly

            – Developper Magento
            May 30 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 at 9:48












          • Please do echo "<pre>"; print_r($result); before and after foreach. You will get idea.

            – Dhara Bhatti
            May 30 at 9:50

















          • 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 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 at 9:36











          • ok i will check this immediatly

            – Developper Magento
            May 30 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 at 9:48












          • Please do echo "<pre>"; print_r($result); before and after foreach. You will get idea.

            – Dhara Bhatti
            May 30 at 9:50
















          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 at 9:33





          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 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 at 9:36





          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 at 9:36













          ok i will check this immediatly

          – Developper Magento
          May 30 at 9:42





          ok i will check this immediatly

          – Developper Magento
          May 30 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 at 9:48






          i can't understand this part of your code foreach ($result as &$value) $value = $value['value']; what that mean

          – Developper Magento
          May 30 at 9:48














          Please do echo "<pre>"; print_r($result); before and after foreach. You will get idea.

          – Dhara Bhatti
          May 30 at 9:50





          Please do echo "<pre>"; print_r($result); before and after foreach. You will get idea.

          – Dhara Bhatti
          May 30 at 9:50

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f276699%2fhow-to-check-uniquenes-of-color-options-when-adding-a-color-magento-2%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form