how to assign data to new div in magento2

how to assign data to new div in magento2

September 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
how to assign data to new div in magento2

Assigning Data to New Div in Magento2

In Magento2, you can assign data from one block to another using various methods such as reference blocks and layout updates. Let’s see how we can replicate the review functionality assigned to the reviews.tab referenceBlock and apply it to a new div on the product page.

Understanding the Current Setup

The provided XML code shows a reference block named reviews.tab, which is assigned the class Magento\Review\Block\Product\Review. This block retrieves reviews using the getReviewsCollection() method. The corresponding list.phtml file then iterates through these reviews and displays them in a structured format.

Creating a New Div for Reviews

To create a new div for reviews, you need to update your layout XML file (usually located under app/design/frontend/<Vendor>/<Theme>/Magento_Review/layout/default.xml).

Add the following code within the <page_layout> tag:

<referenceBlock name="product.info.details">
    <block class="Magento\Review\Block\Product\Review" name="custom.reviews.tab"
           as="custom_reviews" template="Magento_Review::review.phtml"
           group="detailed_info"/>
</referenceBlock>

In this code:

  • Replace product.info.details with the name of your specific product details container.
  • The new block is named custom.reviews.tab.
  • The class remains the same as the existing reviews block (Magento\Review\Block\Product\Review).
  • The template is set to Magento_Review::review.phtml, which is the same template used for the original review block.

Assigning Data to the New Div

To assign data from your existing reviews collection to this new div, you need to modify the list.phtml file of the Review module (vendor/magento/module-review/view/frontend/templates/review/list.phtml).

Find the following line:

&lt;?php foreach ($_items as $_review): ?&gt;

Replace it with:

&lt;?php if (count($_items)): ?&gt;
    &lt;div class="block review-list" id="custom-reviews"&gt;
        <!-- Your new div content here -->
    &lt;/div&gt;
&lt;?php endif; ?&gt;

Now, within the <!-- Your new div content here --> comment section, you can copy and paste the entire <ol class="items review-items">...</ol> section from your original list.phtml file. This will display the reviews in the same format as the original block.

Updating the Product Page

To include this new div on your product page, you need to update the main Magento layout XML file (app/design/frontend/<Vendor>/<Theme>/layout/default.xml). Add the following code within the <page_layout> tag:

<referenceBlock name="product.info">
    <block class="Magento\Review\Block\Product\Review" name="custom.product.reviews"
           as="custom_product_reviews" template="Magento_Review::review.phtml"
           before="-"/>
</referenceBlock>

In this code:

  • Replace product.info with the name of your specific product information container.
  • The new block is named custom.product.reviews.
  • Set the before attribute to - to ensure the block appears at the end of the content.

Conclusion

By following these steps, you should now have a new div displaying reviews on your Magento2 product page. Remember that modifying core files directly is not recommended for future upgrades. It’s always better to create custom modules or use theme-specific layout updates when making changes like this.

For more detailed information on customizing blocks and layouts in Magento2, visit the official documentation: https://docs.magento.com/m2/ce/user_guide/architecture.html

Happy coding!

Magento 2 Catalog Themes

Our network