Skip to content

How to dynamically limit products per page?

Basically I have some static blocks called promo blocks that are put in specific positions (example positions 1,3,5) and the default limit is 12 products per page, so on the 1st page instead of showing 12 products we should now show 9 products and the 3 promo blocks, and products 10-12 should get pushed to the next page.

I have a promolist.phtml file that’s used as a template for catalog_category_view.xml and here’s the xml code:

    <body>
        <referenceBlock name="category.products.list" template="Sgd_Promo::promolist.phtml">
            <block class="SgdPromoBlockPromoBlock" name="promo.block"/>
        </referenceBlock>
    </body>
</page>

promolist.phtml is a big file but here’s the most important part:

<?php
 * @var $block MagentoCatalogBlockProductListProduct
$_productCollection = $block->getLoadedProductCollection();

Basically the issue is that getLoadedProductCollection is already initialized with 12 products for page 1 and 2 products for page 2, how can I change it to have for example 9 products on page 1 and 8 products on page 2 AFTER I load the collection initially, as I need to load the collection first to calculate the limit that I want to set and then have that limit be in effect on the same page? I tested around with plugins and interceptors but didn’t manage to make it work, I’d appreciate a detailed answer as I’m still relatively new to Magento.