What is the right way to get both, the admin translation and the frontend value of a multiselect...

Are small insurances worth it?

Shifting between bemols (flats) and diesis (sharps)in the key signature

Is divide-by-zero a security vulnerability?

Is it safe to abruptly remove Arduino power?

Expressing logarithmic equations without logs

Was it really inappropriate to write a pull request for the company I interviewed with?

Should I take out a loan for a friend to invest on my behalf?

What would be the most expensive material to an intergalactic society?

Determining optimum shared school bus route to a single destination from multiple origin points

Haman going to the second feast dirty

How do we create new idioms and use them in a novel?

Rationale to prefer local variables over instance variables?

Why does Central Limit Theorem break down in my simulation?

What's the 'present simple' form of the word "нашла́" in 3rd person singular female?

What will happen if my luggage gets delayed?

Do items de-spawn?

What stops an assembly program from crashing the operating system?

Outlet with 3 sets of wires

Can one live in the U.S. and not use a credit card?

Street obstacles in New Zealand

After `ssh` without `-X` to a machine, is it possible to change `$DISPLAY` to make it work like `ssh -X`?

Is it a Cyclops number? "Nobody" knows!

What is Earthy controling in the ISS cupola?

Why is a very small peak with larger m/z not considered to be the molecular ion?



What is the right way to get both, the admin translation and the frontend value of a multiselect attribute?


How to get the ID of a custom attribute when having the custom attribute text value?EAV (Entity, Attribute and Value) confusion ..?What is the best way to check if an EAV attribute exists in Magento?Get custom category attribute valueGet attribute option value from option_IDNon default store_view's product attribute value is not visible on frontend (product list) if store_view 0 EAV value is missingWhat is the difference between a field and attribute?How to get the product id based on custom attribute value?Get attribute value of custom eav model in magento 2Magento 1.9.2. - Get Admin value of attribute option in frontend













5















I am using the following code to fetch the admin option value as well as the frontend label from an ID list of vales of a multiselect.



    $attribute = $this->eavConfig->getAttribute('catalog_product', self::ATTRIBUTE_CODE);
$adminAttribute = clone $attribute;
$adminAttribute->setStoreId(0);

$result = [];
foreach(array_unique($allIds) as $id) {
$description = $attribute->getSource()->getOptionText($id);
$fileName = $adminAttribute->getSource()->getOptionText($id);
$result[$fileName] = $description;
}


In a block on the product list this seems to work, while it does not look clean for me. Anyways, on a product detail page it does not (I get for $fileName still the frontend value, not the admin value).



Is there a proper way to do this?



EDIT I was thinking about loading the product in Admin store, still I get the non-admin-store value:



$adminProduct = $this->_productRepository->getById($product->getId(), false, 0);
$adminProduct->getAttributeText(self::ATTRIBUTE_CODE);









share|improve this question





























    5















    I am using the following code to fetch the admin option value as well as the frontend label from an ID list of vales of a multiselect.



        $attribute = $this->eavConfig->getAttribute('catalog_product', self::ATTRIBUTE_CODE);
    $adminAttribute = clone $attribute;
    $adminAttribute->setStoreId(0);

    $result = [];
    foreach(array_unique($allIds) as $id) {
    $description = $attribute->getSource()->getOptionText($id);
    $fileName = $adminAttribute->getSource()->getOptionText($id);
    $result[$fileName] = $description;
    }


    In a block on the product list this seems to work, while it does not look clean for me. Anyways, on a product detail page it does not (I get for $fileName still the frontend value, not the admin value).



    Is there a proper way to do this?



    EDIT I was thinking about loading the product in Admin store, still I get the non-admin-store value:



    $adminProduct = $this->_productRepository->getById($product->getId(), false, 0);
    $adminProduct->getAttributeText(self::ATTRIBUTE_CODE);









    share|improve this question



























      5












      5








      5








      I am using the following code to fetch the admin option value as well as the frontend label from an ID list of vales of a multiselect.



          $attribute = $this->eavConfig->getAttribute('catalog_product', self::ATTRIBUTE_CODE);
      $adminAttribute = clone $attribute;
      $adminAttribute->setStoreId(0);

      $result = [];
      foreach(array_unique($allIds) as $id) {
      $description = $attribute->getSource()->getOptionText($id);
      $fileName = $adminAttribute->getSource()->getOptionText($id);
      $result[$fileName] = $description;
      }


      In a block on the product list this seems to work, while it does not look clean for me. Anyways, on a product detail page it does not (I get for $fileName still the frontend value, not the admin value).



      Is there a proper way to do this?



      EDIT I was thinking about loading the product in Admin store, still I get the non-admin-store value:



      $adminProduct = $this->_productRepository->getById($product->getId(), false, 0);
      $adminProduct->getAttributeText(self::ATTRIBUTE_CODE);









      share|improve this question
















      I am using the following code to fetch the admin option value as well as the frontend label from an ID list of vales of a multiselect.



          $attribute = $this->eavConfig->getAttribute('catalog_product', self::ATTRIBUTE_CODE);
      $adminAttribute = clone $attribute;
      $adminAttribute->setStoreId(0);

      $result = [];
      foreach(array_unique($allIds) as $id) {
      $description = $attribute->getSource()->getOptionText($id);
      $fileName = $adminAttribute->getSource()->getOptionText($id);
      $result[$fileName] = $description;
      }


      In a block on the product list this seems to work, while it does not look clean for me. Anyways, on a product detail page it does not (I get for $fileName still the frontend value, not the admin value).



      Is there a proper way to do this?



      EDIT I was thinking about loading the product in Admin store, still I get the non-admin-store value:



      $adminProduct = $this->_productRepository->getById($product->getId(), false, 0);
      $adminProduct->getAttributeText(self::ATTRIBUTE_CODE);






      eav magento2.2.2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 16 '18 at 9:00







      Alex

















      asked Mar 9 '18 at 17:06









      AlexAlex

      9,6461754114




      9,6461754114






















          2 Answers
          2






          active

          oldest

          votes


















          6





          +250









          Assuming your attribute options are coming from a table, and I assume they are because you reference admin and store values, we should be able to call MagentoEavModelEntityAttributeSourceTable::getAllOptions which depending on the arguments will return the store values or the default (admin) values.



          With that you could try something like this:



          protected function getAttributeOptions($attributeCode) {
          $merged = [];

          $attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);
          $source = $attribute->getSource();

          if($source instanceof MagentoEavModelEntityAttributeSourceTable) {
          $options = $this->optionArrayToHashmap($source->getAllOptions(false, false));
          $adminOptions = $this->optionArrayToHashmap($source->getAllOptions(false, true));

          foreach($adminOptions as $k => $v) {
          $merged[$v] = isset($options[$k]) ? $options[$k] : $v;
          }
          }

          return $merged;
          }

          protected function optionArrayToHashmap($array) {
          $hashMap = [];

          foreach($array as $option) {
          $hashMap[$option['value']] = (string) $option['label'];
          }

          return $hashMap;
          }


          I can't confirm if this works where you are trying it but it's worth a shot. When I run it against demo store data on the "color" attribute with some values specific to the store view ("Store " prepended) I get the following results:



          Array (
          [Black] => Store Black
          [Blue] => Store Blue
          [Brown] => Store Brown
          [Gray] => Store Gray
          [Green] => Green
          [Lavender] => Lavender
          [Multi] => Multi
          [Orange] => Orange
          [Purple] => Purple
          [Red] => Red
          [White] => White
          [Yellow] => Yellow
          )





          share|improve this answer































            0














            I have just tried this for particular one attribute & it worked for me so I am posting this over here May be this can lead you to something you want to achieve.



            <?php

            namespace AbhishekAttributeControllerIndex;

            class Index extends MagentoFrameworkAppActionAction{

            public function __construct(
            MagentoFrameworkAppActionContext $context,
            MagentoEavModelConfig $eavConfig
            ){
            $this->eavConfig = $eavConfig;
            parent::__construct($context);
            }

            public function execute(){
            $attrib = $this->getAttributes();
            $attributeId = 4;
            $back = $attrib->setStoreId(0)->getSource()->getOptionText($attributeId);
            $front = $attrib->setStoreId(1)->getSource()->getOptionText($attributeId);
            echo "Frontend (User) Value = ".$front." & Backend (Admin) Value = ".$back;
            }

            public function getAttributes(){
            $attributeCode = 'color';
            $attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);
            return $attribute;
            }
            }



            We also can achieve this for the all attributes of particular one
            product via product Id.







            share|improve this answer

























              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%2f216779%2fwhat-is-the-right-way-to-get-both-the-admin-translation-and-the-frontend-value%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              6





              +250









              Assuming your attribute options are coming from a table, and I assume they are because you reference admin and store values, we should be able to call MagentoEavModelEntityAttributeSourceTable::getAllOptions which depending on the arguments will return the store values or the default (admin) values.



              With that you could try something like this:



              protected function getAttributeOptions($attributeCode) {
              $merged = [];

              $attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);
              $source = $attribute->getSource();

              if($source instanceof MagentoEavModelEntityAttributeSourceTable) {
              $options = $this->optionArrayToHashmap($source->getAllOptions(false, false));
              $adminOptions = $this->optionArrayToHashmap($source->getAllOptions(false, true));

              foreach($adminOptions as $k => $v) {
              $merged[$v] = isset($options[$k]) ? $options[$k] : $v;
              }
              }

              return $merged;
              }

              protected function optionArrayToHashmap($array) {
              $hashMap = [];

              foreach($array as $option) {
              $hashMap[$option['value']] = (string) $option['label'];
              }

              return $hashMap;
              }


              I can't confirm if this works where you are trying it but it's worth a shot. When I run it against demo store data on the "color" attribute with some values specific to the store view ("Store " prepended) I get the following results:



              Array (
              [Black] => Store Black
              [Blue] => Store Blue
              [Brown] => Store Brown
              [Gray] => Store Gray
              [Green] => Green
              [Lavender] => Lavender
              [Multi] => Multi
              [Orange] => Orange
              [Purple] => Purple
              [Red] => Red
              [White] => White
              [Yellow] => Yellow
              )





              share|improve this answer




























                6





                +250









                Assuming your attribute options are coming from a table, and I assume they are because you reference admin and store values, we should be able to call MagentoEavModelEntityAttributeSourceTable::getAllOptions which depending on the arguments will return the store values or the default (admin) values.



                With that you could try something like this:



                protected function getAttributeOptions($attributeCode) {
                $merged = [];

                $attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);
                $source = $attribute->getSource();

                if($source instanceof MagentoEavModelEntityAttributeSourceTable) {
                $options = $this->optionArrayToHashmap($source->getAllOptions(false, false));
                $adminOptions = $this->optionArrayToHashmap($source->getAllOptions(false, true));

                foreach($adminOptions as $k => $v) {
                $merged[$v] = isset($options[$k]) ? $options[$k] : $v;
                }
                }

                return $merged;
                }

                protected function optionArrayToHashmap($array) {
                $hashMap = [];

                foreach($array as $option) {
                $hashMap[$option['value']] = (string) $option['label'];
                }

                return $hashMap;
                }


                I can't confirm if this works where you are trying it but it's worth a shot. When I run it against demo store data on the "color" attribute with some values specific to the store view ("Store " prepended) I get the following results:



                Array (
                [Black] => Store Black
                [Blue] => Store Blue
                [Brown] => Store Brown
                [Gray] => Store Gray
                [Green] => Green
                [Lavender] => Lavender
                [Multi] => Multi
                [Orange] => Orange
                [Purple] => Purple
                [Red] => Red
                [White] => White
                [Yellow] => Yellow
                )





                share|improve this answer


























                  6





                  +250







                  6





                  +250



                  6




                  +250





                  Assuming your attribute options are coming from a table, and I assume they are because you reference admin and store values, we should be able to call MagentoEavModelEntityAttributeSourceTable::getAllOptions which depending on the arguments will return the store values or the default (admin) values.



                  With that you could try something like this:



                  protected function getAttributeOptions($attributeCode) {
                  $merged = [];

                  $attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);
                  $source = $attribute->getSource();

                  if($source instanceof MagentoEavModelEntityAttributeSourceTable) {
                  $options = $this->optionArrayToHashmap($source->getAllOptions(false, false));
                  $adminOptions = $this->optionArrayToHashmap($source->getAllOptions(false, true));

                  foreach($adminOptions as $k => $v) {
                  $merged[$v] = isset($options[$k]) ? $options[$k] : $v;
                  }
                  }

                  return $merged;
                  }

                  protected function optionArrayToHashmap($array) {
                  $hashMap = [];

                  foreach($array as $option) {
                  $hashMap[$option['value']] = (string) $option['label'];
                  }

                  return $hashMap;
                  }


                  I can't confirm if this works where you are trying it but it's worth a shot. When I run it against demo store data on the "color" attribute with some values specific to the store view ("Store " prepended) I get the following results:



                  Array (
                  [Black] => Store Black
                  [Blue] => Store Blue
                  [Brown] => Store Brown
                  [Gray] => Store Gray
                  [Green] => Green
                  [Lavender] => Lavender
                  [Multi] => Multi
                  [Orange] => Orange
                  [Purple] => Purple
                  [Red] => Red
                  [White] => White
                  [Yellow] => Yellow
                  )





                  share|improve this answer













                  Assuming your attribute options are coming from a table, and I assume they are because you reference admin and store values, we should be able to call MagentoEavModelEntityAttributeSourceTable::getAllOptions which depending on the arguments will return the store values or the default (admin) values.



                  With that you could try something like this:



                  protected function getAttributeOptions($attributeCode) {
                  $merged = [];

                  $attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);
                  $source = $attribute->getSource();

                  if($source instanceof MagentoEavModelEntityAttributeSourceTable) {
                  $options = $this->optionArrayToHashmap($source->getAllOptions(false, false));
                  $adminOptions = $this->optionArrayToHashmap($source->getAllOptions(false, true));

                  foreach($adminOptions as $k => $v) {
                  $merged[$v] = isset($options[$k]) ? $options[$k] : $v;
                  }
                  }

                  return $merged;
                  }

                  protected function optionArrayToHashmap($array) {
                  $hashMap = [];

                  foreach($array as $option) {
                  $hashMap[$option['value']] = (string) $option['label'];
                  }

                  return $hashMap;
                  }


                  I can't confirm if this works where you are trying it but it's worth a shot. When I run it against demo store data on the "color" attribute with some values specific to the store view ("Store " prepended) I get the following results:



                  Array (
                  [Black] => Store Black
                  [Blue] => Store Blue
                  [Brown] => Store Brown
                  [Gray] => Store Gray
                  [Green] => Green
                  [Lavender] => Lavender
                  [Multi] => Multi
                  [Orange] => Orange
                  [Purple] => Purple
                  [Red] => Red
                  [White] => White
                  [Yellow] => Yellow
                  )






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 16 '18 at 22:19









                  Richard KlintRichard Klint

                  55126




                  55126

























                      0














                      I have just tried this for particular one attribute & it worked for me so I am posting this over here May be this can lead you to something you want to achieve.



                      <?php

                      namespace AbhishekAttributeControllerIndex;

                      class Index extends MagentoFrameworkAppActionAction{

                      public function __construct(
                      MagentoFrameworkAppActionContext $context,
                      MagentoEavModelConfig $eavConfig
                      ){
                      $this->eavConfig = $eavConfig;
                      parent::__construct($context);
                      }

                      public function execute(){
                      $attrib = $this->getAttributes();
                      $attributeId = 4;
                      $back = $attrib->setStoreId(0)->getSource()->getOptionText($attributeId);
                      $front = $attrib->setStoreId(1)->getSource()->getOptionText($attributeId);
                      echo "Frontend (User) Value = ".$front." & Backend (Admin) Value = ".$back;
                      }

                      public function getAttributes(){
                      $attributeCode = 'color';
                      $attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);
                      return $attribute;
                      }
                      }



                      We also can achieve this for the all attributes of particular one
                      product via product Id.







                      share|improve this answer






























                        0














                        I have just tried this for particular one attribute & it worked for me so I am posting this over here May be this can lead you to something you want to achieve.



                        <?php

                        namespace AbhishekAttributeControllerIndex;

                        class Index extends MagentoFrameworkAppActionAction{

                        public function __construct(
                        MagentoFrameworkAppActionContext $context,
                        MagentoEavModelConfig $eavConfig
                        ){
                        $this->eavConfig = $eavConfig;
                        parent::__construct($context);
                        }

                        public function execute(){
                        $attrib = $this->getAttributes();
                        $attributeId = 4;
                        $back = $attrib->setStoreId(0)->getSource()->getOptionText($attributeId);
                        $front = $attrib->setStoreId(1)->getSource()->getOptionText($attributeId);
                        echo "Frontend (User) Value = ".$front." & Backend (Admin) Value = ".$back;
                        }

                        public function getAttributes(){
                        $attributeCode = 'color';
                        $attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);
                        return $attribute;
                        }
                        }



                        We also can achieve this for the all attributes of particular one
                        product via product Id.







                        share|improve this answer




























                          0












                          0








                          0







                          I have just tried this for particular one attribute & it worked for me so I am posting this over here May be this can lead you to something you want to achieve.



                          <?php

                          namespace AbhishekAttributeControllerIndex;

                          class Index extends MagentoFrameworkAppActionAction{

                          public function __construct(
                          MagentoFrameworkAppActionContext $context,
                          MagentoEavModelConfig $eavConfig
                          ){
                          $this->eavConfig = $eavConfig;
                          parent::__construct($context);
                          }

                          public function execute(){
                          $attrib = $this->getAttributes();
                          $attributeId = 4;
                          $back = $attrib->setStoreId(0)->getSource()->getOptionText($attributeId);
                          $front = $attrib->setStoreId(1)->getSource()->getOptionText($attributeId);
                          echo "Frontend (User) Value = ".$front." & Backend (Admin) Value = ".$back;
                          }

                          public function getAttributes(){
                          $attributeCode = 'color';
                          $attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);
                          return $attribute;
                          }
                          }



                          We also can achieve this for the all attributes of particular one
                          product via product Id.







                          share|improve this answer















                          I have just tried this for particular one attribute & it worked for me so I am posting this over here May be this can lead you to something you want to achieve.



                          <?php

                          namespace AbhishekAttributeControllerIndex;

                          class Index extends MagentoFrameworkAppActionAction{

                          public function __construct(
                          MagentoFrameworkAppActionContext $context,
                          MagentoEavModelConfig $eavConfig
                          ){
                          $this->eavConfig = $eavConfig;
                          parent::__construct($context);
                          }

                          public function execute(){
                          $attrib = $this->getAttributes();
                          $attributeId = 4;
                          $back = $attrib->setStoreId(0)->getSource()->getOptionText($attributeId);
                          $front = $attrib->setStoreId(1)->getSource()->getOptionText($attributeId);
                          echo "Frontend (User) Value = ".$front." & Backend (Admin) Value = ".$back;
                          }

                          public function getAttributes(){
                          $attributeCode = 'color';
                          $attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);
                          return $attribute;
                          }
                          }



                          We also can achieve this for the all attributes of particular one
                          product via product Id.








                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 15 mins ago









                          Teja Bhagavan Kollepara

                          2,98641947




                          2,98641947










                          answered Mar 23 '18 at 4:52









                          ABHISHEK TRIPATHIABHISHEK TRIPATHI

                          1,8101726




                          1,8101726






























                              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%2f216779%2fwhat-is-the-right-way-to-get-both-the-admin-translation-and-the-frontend-value%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

                              “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

                              How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

                              變成蝙蝠會怎樣? 參考資料 外部連結 导航菜单Thomas Nagel, "What is it like to be a...