1

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
  • are you talking about Category Page Collection, right? Commented Sep 11, 2021 at 4:44
  • Hey @DhirenVasoya, yes the category page. Similar to this thread, but I don't know how it can be done in magento 2. magento.stackexchange.com/questions/141645/…
    – Max
    Commented Sep 13, 2021 at 12:13

2 Answers 2

1

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

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

console.log(productIds);

Result:

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();?>'
</script>

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

Update

You can add own block to the layout:

Vendor/Module/view/frontend/layout/catalog_category_view.xml

<?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">
    <body>
        <referenceContainer name="category.product.list.additional">
            <block class="Vendor\Module\Block\CategoryAdditional" name="additional_products_info_js" template="Vendor_Module::products_additional_info.phtml" />
        </referenceContainer>
    </body>
</page>

Block code:

app/code/Vendor/Module/Block/CategoryAdditional.php

<?php

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):

app/code/Vendor/Module/view/frontend/templates/products_additional_info.phtml

<?php
/** @var \Vendor\Module\Block\CategoryAdditional $block */
?>
<h3><?= __('Product ids');?></h3>
<?php
$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

4
  • 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
    Commented 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
    Commented 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
    Commented Sep 13, 2021 at 15:24
  • @Max I've updated the answer: added example with layout and new block. Commented Sep 13, 2021 at 15:50
1

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

2
  • 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
    Commented Sep 13, 2021 at 14:48
  • 1
    Yes the block must be on this type, then you will get proper output. Commented Sep 13, 2021 at 15:16

Your Answer

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

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