Skip to content

getFirstItem is not working on collection while getData returns an array

getFirstItem is not working on a collection while getData returns a row.

$product_obj       = $objectManager->create('MagentoCatalogModelProduct');
$productCollection = $product_obj->getCollection()->addAttributeToFilter('erp_vc_item_code',$value->getVcItemCode());

print_r($productCollection->getSize()); // Prints 1
print_r($productCollection->getData()); // Prints an array
print_r($productCollection->getFirstItem()->getData()); // Does not prints anything
print_r($productCollection->getFirstItem()->getSku()); // Does not prints anything

Output

1

Array
(
  [0] => Array
    (
        [entity_id] => 2087
        [attribute_set_id] => 4
        [type_id] => simple
        [sku] => Mouthwash---Fresh-To-Go-500ml
        [has_options] => 0
        [required_options] => 0
        [created_at] => 2022-03-27 16:31:08
        [updated_at] => 2022-03-27 16:31:09
        [erp_vc_item_code] => PCMO0000258
    )

)

Array
(
)

Sometimes the function works at other places but don’t know why it does’t work every time as expected.