Overriding Magento\Review\Block\Product\View: A Comprehensive Guide
Many developers face challenges when trying to override core blocks in Magento 2. The Magento\Review\Block\Product\View block is one of the most common targets for customization, as it handles product reviews on the product view page.
Understanding the Problem
The developer mentioned in the question has correctly configured their module and placed the override file in the correct location (app/code/[Package]/CombineReviews/Block/Magento/Review/Product/View.php). However, the modification to the getReviewsCollection function is not reflected on the frontend.
The issue lies in how Magento 2 resolves dependencies. While the preference configuration in di.xml tells Magento which class to instantiate for a given interface, it doesn’t necessarily mean that the override will be used in all contexts where the interface is called.
The Core Issue: Contextual Dependency Resolution
In this case, the getReviewsCollection method is being called from multiple places within the core codebase. Each context might have its own resolution strategy for the dependency.
The key problem is that Magento 2 doesn’t guarantee that the override will be used in every single context where the interface is invoked. This is a known limitation of the dependency injection system used by Magento 2, which can lead to surprises when creating overrides.
Analyzing the Override
Let’s look at the provided override code:
public function getReviewsCollection()
{
if (null === $this->_reviewsCollection) {
$this->_reviewsCollection = $this->_reviewsColFactory->create()->addStoreFilter(
$this->_storeManager->getStore()->getId()
)->addStatusFilter(
\Magento\Review\Model\Review::STATUS_APPROVED
)->addEntityFilter(
'product',
3
//$this->getProduct()->getId()
)->setDateOrder();
}
return $this->_reviewsCollection;
}
The modification made by the developer is a small change to the addEntityFilter method. The second parameter is set to a hardcoded value of 3 instead of calling $this->getProduct()->getId(). This change should theoretically affect the collection when it’s being filtered based on the product ID.
However, this doesn’t seem to be working as expected in practice.
Potential Solutions
Solution 1: Use Plugin Instead of Override
Instead of using a preference override, which can have unpredictable behavior due to Magento 2’s contextual dependency resolution, consider using an around plugin.
An around plugin allows you to wrap the original method with your own logic. This approach gives you more control over when and how the core method is executed.
Here’s a basic example:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Review\Block\Product\View">
<plugin name="override_getReviewsCollection" type="[Package]\CombineReviews\Plugin\GetReviewsCollection"/>
</type>
</config>
And the plugin class:
public function aroundGetReviewsCollection(View $subject, callable $proceed)
{
$reviews = $proceed();
// Add your custom logic here
return $reviews;
}
Solution 2: Use a Different Approach for Collection Filtering
Instead of trying to modify the core collection filtering logic, consider creating a new method that handles the specific filter you need. This approach allows you to create a clear separation between the core and custom functionality.
For example, you could create a public method called getApprovedReviewsForProduct in your override class:
public function getApprovedReviewsForProduct(\Magento\Catalog\Model\Product $product)
{
return $this->_reviewsColFactory->create()->addStoreFilter(
$this->_storeManager->getStore()->getId()
)->addStatusFilter(
\Magento\Review\Model\Review::STATUS_APPROVED
)->addEntityFilter(
'product',
$product->getId()
)->setDateOrder();
}
You can then call this method wherever you need to get the reviews collection for a specific product.
Conclusion
Overriding core blocks in Magento 2 requires a deep understanding of the dependency injection system and the context in which the dependencies are resolved. While the standard preference override approach works most of the time, it can lead to unpredictable behavior due to Magento 2’s contextual resolution strategy.
In cases where the standard override doesn’t work as expected, consider using around plugins or creating separate methods for custom logic. These approaches provide more control over when and how your code interacts with the core functionality.
For more detailed information on advanced Magento development practices, I recommend visiting Laravel Company. They offer a wealth of resources and articles on Magento 2 development best practices.