I would create a custom block that is active only for the category layout (catalog_category_view.xml
).
This block would use the product collection and would filter the product by category id. When the category page is loaded, the registry current_category ($registry->registry('current_category')->getId();
) is set. The code below would give you all the product ids.
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $this->collectionFactory->create();
$collection->addCategoriesFilter([$categoryId]);
$productIds = $collection->getAllIds();
Once done, I would advise to set a cache lifetime to this block so that it does not slow down your category pages.