Magento 2.2.3 How to check if attribute exist in current product?












1















I want to check if attribute exist in current product by attribute code.



For Ex: attribute code if color and this attribute is assign to current product. but if i pass something else like abcde and this attribute doesn't exist in current product.



So how can i filter is current value is not attribute?










share|improve this question

























  • I think you can try this $product->hasData('custom_attribute')

    – kunj
    1 hour ago
















1















I want to check if attribute exist in current product by attribute code.



For Ex: attribute code if color and this attribute is assign to current product. but if i pass something else like abcde and this attribute doesn't exist in current product.



So how can i filter is current value is not attribute?










share|improve this question

























  • I think you can try this $product->hasData('custom_attribute')

    – kunj
    1 hour ago














1












1








1








I want to check if attribute exist in current product by attribute code.



For Ex: attribute code if color and this attribute is assign to current product. but if i pass something else like abcde and this attribute doesn't exist in current product.



So how can i filter is current value is not attribute?










share|improve this question
















I want to check if attribute exist in current product by attribute code.



For Ex: attribute code if color and this attribute is assign to current product. but if i pass something else like abcde and this attribute doesn't exist in current product.



So how can i filter is current value is not attribute?







attributes product-attribute frontend magento2.2.3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago









Shoaib Munir

949421




949421










asked 2 hours ago









Chirag PatelChirag Patel

2,094320




2,094320













  • I think you can try this $product->hasData('custom_attribute')

    – kunj
    1 hour ago



















  • I think you can try this $product->hasData('custom_attribute')

    – kunj
    1 hour ago

















I think you can try this $product->hasData('custom_attribute')

– kunj
1 hour ago





I think you can try this $product->hasData('custom_attribute')

– kunj
1 hour ago










3 Answers
3






active

oldest

votes


















1














Try this, hope this is what looking for



if ($product->getCustomAttributes()) {
$attribute_value = $product->getCustomAttribute('attribute_code') ? $product->getCustomAttribute('attribute_code')->getValue() : '';
return $attribute_value;
}


Try it and do let me know.
Peace :)






share|improve this answer


























  • Tnxs for answer. but it is giving me error Array to string conversion any way i found other solution. +1 for your efforts. :)

    – Chirag Patel
    2 hours ago













  • The above example return array which you might have tried echo, the errors denotes that. Check it or if error persist update the answer because it will be useful for other who visit here.

    – G Prathap
    2 hours ago











  • But why attribute return value is an array? how can i get in string?

    – Chirag Patel
    2 hours ago











  • Updated, now check and let me know

    – G Prathap
    2 hours ago



















1














try this



$product->hasData('custom_attribute') OR $product->hasCustomAttribute()






share|improve this answer































    0














    I found solution in below way as per my specific requirement.



    public function getAttributeValue($product, $code){

    $attribute = $product->getResource()->getAttribute('attribute_code');
    if ($attribute)
    {
    if (in_array(
    $product->getResource()->getAttribute($code)->getFrontendInput(),
    ['select', 'text']
    )) {
    $value = $product->getAttributeText($code);
    } else {
    $value = $product->getData($code);
    }
    } else {
    $value = '';
    }
    return $value;
    }


    Where in above $product is current product and $code is attribute code.






    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%2f261908%2fmagento-2-2-3-how-to-check-if-attribute-exist-in-current-product%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Try this, hope this is what looking for



      if ($product->getCustomAttributes()) {
      $attribute_value = $product->getCustomAttribute('attribute_code') ? $product->getCustomAttribute('attribute_code')->getValue() : '';
      return $attribute_value;
      }


      Try it and do let me know.
      Peace :)






      share|improve this answer


























      • Tnxs for answer. but it is giving me error Array to string conversion any way i found other solution. +1 for your efforts. :)

        – Chirag Patel
        2 hours ago













      • The above example return array which you might have tried echo, the errors denotes that. Check it or if error persist update the answer because it will be useful for other who visit here.

        – G Prathap
        2 hours ago











      • But why attribute return value is an array? how can i get in string?

        – Chirag Patel
        2 hours ago











      • Updated, now check and let me know

        – G Prathap
        2 hours ago
















      1














      Try this, hope this is what looking for



      if ($product->getCustomAttributes()) {
      $attribute_value = $product->getCustomAttribute('attribute_code') ? $product->getCustomAttribute('attribute_code')->getValue() : '';
      return $attribute_value;
      }


      Try it and do let me know.
      Peace :)






      share|improve this answer


























      • Tnxs for answer. but it is giving me error Array to string conversion any way i found other solution. +1 for your efforts. :)

        – Chirag Patel
        2 hours ago













      • The above example return array which you might have tried echo, the errors denotes that. Check it or if error persist update the answer because it will be useful for other who visit here.

        – G Prathap
        2 hours ago











      • But why attribute return value is an array? how can i get in string?

        – Chirag Patel
        2 hours ago











      • Updated, now check and let me know

        – G Prathap
        2 hours ago














      1












      1








      1







      Try this, hope this is what looking for



      if ($product->getCustomAttributes()) {
      $attribute_value = $product->getCustomAttribute('attribute_code') ? $product->getCustomAttribute('attribute_code')->getValue() : '';
      return $attribute_value;
      }


      Try it and do let me know.
      Peace :)






      share|improve this answer















      Try this, hope this is what looking for



      if ($product->getCustomAttributes()) {
      $attribute_value = $product->getCustomAttribute('attribute_code') ? $product->getCustomAttribute('attribute_code')->getValue() : '';
      return $attribute_value;
      }


      Try it and do let me know.
      Peace :)







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 2 hours ago

























      answered 2 hours ago









      G PrathapG Prathap

      330113




      330113













      • Tnxs for answer. but it is giving me error Array to string conversion any way i found other solution. +1 for your efforts. :)

        – Chirag Patel
        2 hours ago













      • The above example return array which you might have tried echo, the errors denotes that. Check it or if error persist update the answer because it will be useful for other who visit here.

        – G Prathap
        2 hours ago











      • But why attribute return value is an array? how can i get in string?

        – Chirag Patel
        2 hours ago











      • Updated, now check and let me know

        – G Prathap
        2 hours ago



















      • Tnxs for answer. but it is giving me error Array to string conversion any way i found other solution. +1 for your efforts. :)

        – Chirag Patel
        2 hours ago













      • The above example return array which you might have tried echo, the errors denotes that. Check it or if error persist update the answer because it will be useful for other who visit here.

        – G Prathap
        2 hours ago











      • But why attribute return value is an array? how can i get in string?

        – Chirag Patel
        2 hours ago











      • Updated, now check and let me know

        – G Prathap
        2 hours ago

















      Tnxs for answer. but it is giving me error Array to string conversion any way i found other solution. +1 for your efforts. :)

      – Chirag Patel
      2 hours ago







      Tnxs for answer. but it is giving me error Array to string conversion any way i found other solution. +1 for your efforts. :)

      – Chirag Patel
      2 hours ago















      The above example return array which you might have tried echo, the errors denotes that. Check it or if error persist update the answer because it will be useful for other who visit here.

      – G Prathap
      2 hours ago





      The above example return array which you might have tried echo, the errors denotes that. Check it or if error persist update the answer because it will be useful for other who visit here.

      – G Prathap
      2 hours ago













      But why attribute return value is an array? how can i get in string?

      – Chirag Patel
      2 hours ago





      But why attribute return value is an array? how can i get in string?

      – Chirag Patel
      2 hours ago













      Updated, now check and let me know

      – G Prathap
      2 hours ago





      Updated, now check and let me know

      – G Prathap
      2 hours ago













      1














      try this



      $product->hasData('custom_attribute') OR $product->hasCustomAttribute()






      share|improve this answer




























        1














        try this



        $product->hasData('custom_attribute') OR $product->hasCustomAttribute()






        share|improve this answer


























          1












          1








          1







          try this



          $product->hasData('custom_attribute') OR $product->hasCustomAttribute()






          share|improve this answer













          try this



          $product->hasData('custom_attribute') OR $product->hasCustomAttribute()







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          kunjkunj

          2,6072423




          2,6072423























              0














              I found solution in below way as per my specific requirement.



              public function getAttributeValue($product, $code){

              $attribute = $product->getResource()->getAttribute('attribute_code');
              if ($attribute)
              {
              if (in_array(
              $product->getResource()->getAttribute($code)->getFrontendInput(),
              ['select', 'text']
              )) {
              $value = $product->getAttributeText($code);
              } else {
              $value = $product->getData($code);
              }
              } else {
              $value = '';
              }
              return $value;
              }


              Where in above $product is current product and $code is attribute code.






              share|improve this answer






























                0














                I found solution in below way as per my specific requirement.



                public function getAttributeValue($product, $code){

                $attribute = $product->getResource()->getAttribute('attribute_code');
                if ($attribute)
                {
                if (in_array(
                $product->getResource()->getAttribute($code)->getFrontendInput(),
                ['select', 'text']
                )) {
                $value = $product->getAttributeText($code);
                } else {
                $value = $product->getData($code);
                }
                } else {
                $value = '';
                }
                return $value;
                }


                Where in above $product is current product and $code is attribute code.






                share|improve this answer




























                  0












                  0








                  0







                  I found solution in below way as per my specific requirement.



                  public function getAttributeValue($product, $code){

                  $attribute = $product->getResource()->getAttribute('attribute_code');
                  if ($attribute)
                  {
                  if (in_array(
                  $product->getResource()->getAttribute($code)->getFrontendInput(),
                  ['select', 'text']
                  )) {
                  $value = $product->getAttributeText($code);
                  } else {
                  $value = $product->getData($code);
                  }
                  } else {
                  $value = '';
                  }
                  return $value;
                  }


                  Where in above $product is current product and $code is attribute code.






                  share|improve this answer















                  I found solution in below way as per my specific requirement.



                  public function getAttributeValue($product, $code){

                  $attribute = $product->getResource()->getAttribute('attribute_code');
                  if ($attribute)
                  {
                  if (in_array(
                  $product->getResource()->getAttribute($code)->getFrontendInput(),
                  ['select', 'text']
                  )) {
                  $value = $product->getAttributeText($code);
                  } else {
                  $value = $product->getData($code);
                  }
                  } else {
                  $value = '';
                  }
                  return $value;
                  }


                  Where in above $product is current product and $code is attribute code.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 24 mins ago

























                  answered 43 mins ago









                  Chirag PatelChirag Patel

                  2,094320




                  2,094320






























                      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%2f261908%2fmagento-2-2-3-how-to-check-if-attribute-exist-in-current-product%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

                      Statuo de Libereco

                      Tanganjiko

                      Liste der Baudenkmäler in Enneberg