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
    33 mins 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
    33 mins 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 1 hour ago









Shoaib Munir

939421




939421










asked 1 hour ago









Chirag PatelChirag Patel

2,095320




2,095320













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

    – kunj
    33 mins ago



















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

    – kunj
    33 mins ago

















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

– kunj
33 mins ago





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

– kunj
33 mins ago










2 Answers
2






active

oldest

votes


















3














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
    1 hour 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
    1 hour ago











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

    – Chirag Patel
    1 hour ago











  • Updated, now check and let me know

    – G Prathap
    1 hour ago



















1














try this



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






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

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    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
      1 hour 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
      1 hour ago











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

      – Chirag Patel
      1 hour ago











    • Updated, now check and let me know

      – G Prathap
      1 hour ago
















    3














    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
      1 hour 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
      1 hour ago











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

      – Chirag Patel
      1 hour ago











    • Updated, now check and let me know

      – G Prathap
      1 hour ago














    3












    3








    3







    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 1 hour ago

























    answered 1 hour ago









    G PrathapG Prathap

    342113




    342113













    • 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
      1 hour 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
      1 hour ago











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

      – Chirag Patel
      1 hour ago











    • Updated, now check and let me know

      – G Prathap
      1 hour 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
      1 hour 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
      1 hour ago











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

      – Chirag Patel
      1 hour ago











    • Updated, now check and let me know

      – G Prathap
      1 hour 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
    1 hour 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
    1 hour 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
    1 hour 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
    1 hour ago













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

    – Chirag Patel
    1 hour ago





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

    – Chirag Patel
    1 hour ago













    Updated, now check and let me know

    – G Prathap
    1 hour ago





    Updated, now check and let me know

    – G Prathap
    1 hour 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 48 mins ago









        kunjkunj

        2,6072423




        2,6072423






























            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

            Bundesstraße 106

            Verónica Boquete

            Ida-Boy-Ed-Garten