0

How to change the template in a custom module.

Original path = vendor/magento/module-review/view/frontend/templates/helper/summary.phtml

I want to change this template to my own when my module is enabled ONLY

I have looked at a few tests that I could gather from Online and nothing worked.

the way Magento show this summary.phtml is by this way

protected $_availableTemplates = [
    self::FULL_VIEW => 'Magento_Review::helper/summary.phtml',
    self::SHORT_VIEW => 'Magento_Review::helper/summary_short.phtml',
];

Can anyone give me some guide or tips to change this template in a custom module.

The core file is coming from magento/module-review/Block/Product/ReviewRenderer.php

/**
 * Get review summary html
 *
 * @param Product $product
 * @param string $templateType
 * @param bool $displayIfNoReviews
 *
 * @return string
 * @throws \Magento\Framework\Exception\LocalizedException
 * @throws \Magento\Framework\Exception\NoSuchEntityException
 */
public function getReviewsSummaryHtml(
    \Magento\Catalog\Model\Product $product,
    $templateType = self::DEFAULT_VIEW,
    $displayIfNoReviews = false
) {
    if ($product->getRatingSummary() === null) {
        $this->reviewSummaryFactory->create()->appendSummaryDataToObject(
            $product,
            $this->_storeManager->getStore()->getId()
        );
    }

    if (null === $product->getRatingSummary() && !$displayIfNoReviews) {
        return '';
    }
    // pick template among available
    if (empty($this->_availableTemplates[$templateType])) {
        $templateType = self::DEFAULT_VIEW;
    }
    $this->setTemplate($this->_availableTemplates[$templateType]);

    $this->setDisplayIfEmpty($displayIfNoReviews);

    $this->setProduct($product);

    return $this->toHtml();
}
9
  • where the above template code return? Oct 8, 2021 at 10:48
  • Try to customize usage of $_availableTemplates variable in your module via plugin(s).
    – Slava
    Oct 8, 2021 at 10:54
  • @DhirenVasoya i have added the core file path
    – Tj1997
    Oct 8, 2021 at 11:10
  • 1
    I mean that there is a point somewhere when $_availableTemplates variable is used, some method gets its value. So in that place you can try to use after plugin
    – Slava
    Oct 8, 2021 at 11:13
  • 1
    You can try to override ReviewRenderer class via preferences in your module and override $_availableTemplates variable in new class. All other methods will be used from parent, so not a big problem for upgradability.
    – Slava
    Oct 8, 2021 at 11:22

1 Answer 1

1

You need to create the plugin of this function.

public function getReviewsSummaryHtml

Where above variable is use to return the template.

5
  • how can i get a plugin for this function i have been trying beforePlugin and i am not able to do it
    – Tj1997
    Oct 8, 2021 at 11:20
  • you need to create arround plugin. Oct 8, 2021 at 11:27
  • is this is the correct way public function aroundGetReviewsSummaryHtml(\Magento\Review\Block\Product\ReviewRenderer $subject, callable $proceed) or do i need to do public function aroundGetReviewsSummaryHtml(\Magento\Catalog\Model\Product $product, $templateType = self::DEFAULT_VIEW, $displayIfNoReviews = false, \Magento\Review\Block\Product\ReviewRenderer $subject, callable $proceed)
    – Tj1997
    Oct 8, 2021 at 11:31
  • if you have new question please ask it separately so other community member can also help you into that one. Oct 8, 2021 at 11:37
  • 1
    If above answer help you into current asking question, please upvote and accept as solutions so it will help other in community. For how to create plugin, you can check this one : mageplaza.com/magento-2-module-development/… Oct 9, 2021 at 5:59

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.