cancel
Showing results for 
Search instead for 
Did you mean: 

Getting error while getting custom attribute value on product detail page

SOLVED
   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Getting error while getting custom attribute value on product detail page

Hi,
As i created several attributes for products and their is an error occur when i am getting the value of attributes which value i not set in product detail page.
0 [internal function]: Magento\Framework\App\ErrorHandler->handler(2, 'strpos() expect...', '/var/www/vhosts...', 148, Array)

 i am using this code 

$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {

$var = $attribute->getAttributeCode();
echo $attribute->getFrontendLabel();
echo $product->getAttributeText($var); echo '<br />';

}
Thank you

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Getting error while getting custom attribute value on product detail page

I solve this using 

$attributes = $_product->getAttributes();
foreach ($attributes as $attribute) {
$var = $attribute->getAttributeCode();
$_product->offsetExists($var);

}

where offsetExist() check that attribute have value or not it return boolean value.
Thank you @shikha1193  for your reply.

View solution in original post

3 REPLIES 3

Re: Getting error while getting custom attribute value on product detail page

hi @piyush_khandelw 

 

it seems there is no check for empty data in `getAttributeText()` function

try to get values of only your custom attributes with check of empty data and get its values from sources by calling below code instead of `$product->getAttributeText($var); `
`$product->getResource()->getAttribute($var)->getSource()->getOptionText(
$this->getData($product->getData($var)));`

 

If my answer is useful Click Kudos and Accept as Answer

Re: Getting error while getting custom attribute value on product detail page

Hi @shikha1193 
As i try your solution and now it give me this error 
Exception #0 (Exception): Warning: strpos() expects parameter 1 to be string, array given in /httpdocs/vendor/magento/framework/DataObject.php on line 126

Re: Getting error while getting custom attribute value on product detail page

I solve this using 

$attributes = $_product->getAttributes();
foreach ($attributes as $attribute) {
$var = $attribute->getAttributeCode();
$_product->offsetExists($var);

}

where offsetExist() check that attribute have value or not it return boolean value.
Thank you @shikha1193  for your reply.