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












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 43 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

                              What other Star Trek series did the main TNG cast show up in?

                              Berlina muro

                              Berlina aerponto