5

I'm looking for some help with limiting the review list on the product page to 5 reviews at a time. I'm using Magento 2.0.7 and Porto 2.2.1

I found some suggestions on here but they were for Magento 1.x and didn't help me. What I tried so far:

{theme}\Magento_Review\templates\product\view\list.phtml

changed:

$_items = $block->getReviewsCollection()->getItems();

to

$_items = $block->getReviewsCollection()->getItems()->setPageSize(5);

Result: No reviews shown at all. Payed around but got either no reviews or 10 as usual.

{theme}\Magento_Review\layout\review_product_lists.xml

changed:

<block class="Magento\Theme\Block\Html\Pager" name="product_review_list.toolbar"/>

to

<block class="Magento\Theme\Block\Html\Pager" name="product_review_list.toolbar">
    <argument name="setLimit" xsi:type="number">5</argument>
</block>

I also played around in the pager.phtml and even pager.php with similar changes but got no results. What did I miss?

edit: Thanks, so far. Tried both changes, unfortunately I still get 10 reviews. Any chance that I need to change something somewhere else that I haven't noticed yet? Yes, cache was flushed, other changes were visible immediately.

edit: I experimented with application\magento\vendor\magento\module-theme\Block\Html\Pager.php and found out that changing $_availableLimit or $_limit brings the desired results BUT that's not the clean way to work on this. Is there anything I missed? Is the class correct? Is the argument correct at all?

3

Your idea was good the order was wrong though.

Basically you have to change your first code:

$_items = $block->getReviewsCollection()->getItems()->setPageSize(5);

By:

$_items = $block->getReviewsCollection()->setPageSize(5)->getItems();

Regarding the pager, I think your syntax is wrong and instead of:

<block class="Magento\Theme\Block\Html\Pager" name="product_review_list.toolbar">
    <argument name="setLimit" xsi:type="number">5</argument>
</block>

It should be:

<block class="Magento\Theme\Block\Html\Pager" name="product_review_list.toolbar">
    <action method="setLimit">
        <argument name="limit" xsi:type="number">5</argument>
    </action>
</block>
  • 1
    Get out of my head :D – Fabian Schmengler Jun 16 '16 at 15:44
  • 1
    @fschmengler hahaha my cover is blown – Raphael at Digital Pianism Jun 16 '16 at 15:46
  • I actually tried both before - and now again - yet the page always loads 10 reviews. And yes, I flushed the cache, other changes are shown. – Torben Jun 16 '16 at 15:54
  • I am trying to add pagination on my custom page , but the count is not working as expected , $pager->setLimit(36), how can I set it as dynamic which we are filling in admin?? – supriya mishra Jan 23 '17 at 7:37
  • I have created custom module and removes the review tab ,and Have displayed collection of customer reviews in new section in product page. Can you tell me where i need to put above code to limit the reviews – Jaisa Dec 21 '17 at 4:59
3
$_items = $block->getReviewsCollection()->getItems()->setPageSize(5);

cannot work because you first load the collection, then set the page size. You have to set the page size before it's loaded:

$_items = $block->getReviewsCollection()->setPageSize(5)->getItems();

But this will not affect the pagination block and changing parameters using layout XML is the better approach anyway.

The block might not understand your argument definition. Try <action> instead:

<block class="Magento\Theme\Block\Html\Pager" name="product_review_list.toolbar">
    <action method="setLimit">
        <argument name="limit" xsi:type="number">5</argument>
    </action>
</block>

Note, that action is deprecated and should only be used if argument is not possible:

The <action> instruction is deprecated. If the method implementation allows, use the <argument> for <block> or <referenceBlock> to access block public API.

Source: http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/xml-instructions.html#fedg_layout_xml-instruc_ex_act

  • Get out my head :D – Raphael at Digital Pianism Jun 16 '16 at 15:56
  • 1
    Thanks, the hint on devdocs about <action> being deprecated was the reason for me to believe <argument> alone would work. I had tried out <action> before and it didn't work. Unfortunately, I tried both your changes (even before) and it still loads 10 reviews. – Torben Jun 16 '16 at 16:06
  • Didn't work for me either. – Shahzaib Hayat Khan Nov 11 '17 at 22:37
  • @Torben, how can i add this to admin categories->design->layout xml update ... ? – Sarfaraj Sipai Dec 12 '17 at 12:53
2

So far above solution didn't worked for me. What worked for me is to change this in core file vendor/magento/module-theme/Block/Html/Pager.php

protected $_availableLimit = [10 => 10, 20 => 20, 50 => 50];
protected $_availableLimit = [5 => 5, 10 => 10, 20 => 20, 50 => 50];

Obviously that isn't solution and not recommended to change in CORE FILE as it will be lost in update. Even extending this file in custom module will set this pager setting for whole site i believe.

NOTE: Just sharing it as it will help you go in right direction further.

1

The way I got it to work in tandem with the toolbar/pager block (Magento 2.1.7):

$REVIEWS_PAGE_LIMIT = 4;
$reviewListToolbarBlock = $block->getChildBlock('toolbar');
$requested_page = $reviewListToolbarBlock->getRequest()->getParam($reviewListToolbarBlock->getPageVarName(), 1);
$reviewListToolbarBlock->setAvailableLimit([$REVIEWS_PAGE_LIMIT => $REVIEWS_PAGE_LIMIT]);
$reviewListToolbarBlock->setShowPerPage(false)->setShowAmounts(false)->setLimit($REVIEWS_PAGE_LIMIT)->setCollection($block->getReviewsCollection()->clear());
$_collection = $reviewListToolbarBlock->getCollection()->setCurPage($requested_page)->load()->addRateVotes();
$_items = $_collection->getItems();

In my experience I always have to clear() the collection prior to changing the page size and load() again afterwards.

  • clear() and load() worked – localhost May 2 '19 at 19:52
0

Unfortunately I wasn't able to get the above solutions working either, but I did manage to get this working based on some of the information given so credit to the asker and other answers.

Firstly my template was actually missing the pager itself, so make sure you add this into the product view layout;

<block class="Magento\Theme\Block\Html\Pager" name="product_review_list.toolbar">

The name is significant as well as it's mentioned explicitly in the _prepareLayout function Magento\Review\Block\Product\View\ListView where the collection is also passed.

This created the toolbar but I wasn't able to change the limit in the xml. In order to do that I created a block which extends Magento\Theme\Block\Html\Pager, set the limit using $this->setLimit() in a _construct function and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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