I have used the below code but that is not working on catalog, cms & product page for me. but for compare page, cart page, and checkout page it's working.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$compare = $objectManager->get('\Magento\Catalog\CustomerData\CompareProducts');
$compareCollection = $compare->getSectionData();
foreach($compareCollection['items'] as $items){
echo $items['id']."</br>";
}
Note: I am checking for guest users.
I am in wonder the same code is working for cart & checkout page but I am getting empty collection for cms, catalog and product page.
Please help me out.
Thanks in advance.!!