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
Solved! Go to Solution.
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.
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
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
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.