I am trying to get all the products which are displayed on the current page. The information is used for tracking cookies on the page.ready call.

It was possible to get the product from the product page using the Registry class, on the cart and checkout pages I can use the Cart class. How can I get the currently displayed products on the catalog page?

Both classes are deprecated as I am on magento 2.4.3 .


2 Answers 2


If you don't have access to the $collection you can use a jQuery to parse all ids:

var productIds = [];
    .each(function (i, e) {
        productIds[i] = parseInt(e.id.replace('product-item-info_', ''))



Result in console on category view page

But better way is to take all ids right from the product collection:

<script type="text/javascript">
    var productIds = '<?= $_productCollection->getAllIds();?>'

Magento does not use any ui-components in the standard product listing block, so they cannot be detected using the registry.


You can add own block to the layout:


<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="category.product.list.additional">
            <block class="Vendor\Module\Block\CategoryAdditional" name="additional_products_info_js" template="Vendor_Module::products_additional_info.phtml" />

Block code:



namespace Vendor\Module\Block;

class CategoryAdditional extends \Magento\Framework\View\Element\Template
    public function getCollection() {
        return $this->getLayout()->getBlock('category.products.list')->getLoadedProductCollection();

Template where you can use the product collection to fill your javascript object (you can get a sku, name and other information right from the collection):


/** @var \Vendor\Module\Block\CategoryAdditional $block */
<h3><?= __('Product ids');?></h3>
$productIds = $block->getCollection()->getAllIds();
echo implode(',', $productIds);
// Add relevant script here, it's just a sample code which will render all the ids

Results (tested with standard layered navigation):

11 products:

Products 1

With filter (1 product):

Products 2

  • Hey Siarhey, I think I have access to the ProductCollection in my Block. But I want only the products that are visible to the user on one page. The collection gives me all Products in the shop, no matter on which page I am. Is there some way to filter it to the current page?
    – Max
    Sep 13, 2021 at 14:57
  • Using the jQuery approach, I have a phtml file, which provides my javascript File with information like so: ´ <script type="text/x-magento-init"> { "*": { "Vendor_Module/js/myJavascriptFile": { "currentProducts": "<?= json_encode(array_values((array)$currentProducts), JSON_UNESCAPED_UNICODE) ?>" } ` As I need the name and the sku in my javascript File, how can I proceed after providing the Id's to my javascript file?
    – Max
    Sep 13, 2021 at 15:20
  • Or can I load the Id's with jQuery and then get the name and sku in the same phtml file in the '<script type="text/javascript">' part with inline php??
    – Max
    Sep 13, 2021 at 15:24
  • @Max I've updated the answer: added example with layout and new block. Sep 13, 2021 at 15:50

If you are on the category page, then you can get collection by :

<?php $_productCollection = $block->getLoadedProductCollection(); ?>

Where $block is object of \Magento\Catalog\Block\Product\ListProduct

  • Thanks Dhiren. I created a ListProduct object in my Block. The Block itself is of the Type: Magento\Framework\View\Element\Template My findings were, that it did not matter on which page I was. The collection from getLoadedProductCollection() gave back all Products in the Shop, even though on the single Product site (catalog_product_view), there is only one product. Is it, because my Block is of the wrong Type?
    – Max
    Sep 13, 2021 at 14:48
  • 1
    Yes the block must be on this type, then you will get proper output. Sep 13, 2021 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.