Zend certified PHP/Magento developer

Magento2.3 Cron job product collection getFinalPrice returns 0

I am making a module that will create a product feed and save it. The module uses cron to create the feed in regular intervals. However, I have run into a couple of problems…

  1. I get this error: Item (MagentoCatalogModelProductInterceptor) with the same ID “<id>” already exists. I searched the database using various queries but I did not find any duplicates.
  2. I used the code from this page to fix the above problem, but now all calls to $product->getFinalPrice() return 0

Stuff that may help:

  1. When running the same helper code using a command, I do not get the duplicate ID error
  2. I am using an iterator to loop through the product collection, since it’s quite large
  3. In the cron job execute() function, I am using the below code to run the helper function that generates the feed:
foreach ($this->storeManager->getStores() as $store) {
    $this->_helper->generateXML($store->getId());
}
  1. To get the product collection, I am using something like this:
private function getProducts(int $storeId)
{
    $this->collection = $objectManager->create('MagentoCatalogModelResourceModelProductCollection');
    ... //Other filters here
    $this->collection->addFinalPrice();
    $this->collection->addStoreFilter($storeId);
    $this->collection->load();
    $this->iterator->walk(
        $this->collection->getSelect(),
        [[$this, 'productCallback']],
        [
            'store' => $storeId,
        ]
    );
}

(Yes, I know object manager is not recommended, but this is a proof of concept module)

  1. In my callback function, I am using MagentoCatalogApiProductRepositoryInterface to load the product data

  2. I am using Magento 2.3.6

Can anyone help me out? I am sure I am missing something, but I am not sure what that is…