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

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

          Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

          Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?