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?

  • I think you can try this $product->hasData('custom_attribute')
    – kunj
    Commented Feb 15, 2019 at 5:51

3 Answers 3


try this

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


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(
            ['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.


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 :)

  • Tnxs for answer. but it is giving me error Array to string conversion any way i found other solution. +1 for your efforts. :) Commented Feb 15, 2019 at 5:12
  • 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. Commented Feb 15, 2019 at 5:15
  • But why attribute return value is an array? how can i get in string? Commented Feb 15, 2019 at 5:18
  • Updated, now check and let me know Commented Feb 15, 2019 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.