Resolving Custom Block Issues in Magento 2
You’re on the right track with your implementation of a custom block in Magento 2 to retrieve data for use in a template file. Let’s troubleshoot the issue you’re experiencing and ensure that both your delivery_info.phtml and info.phtml files are being properly rendered.
Understanding Your Current Setup
The Custom Block
Your code creates a custom block named Question, which extends Magento’s abstract block class. This block has access to the customer session, allowing you to retrieve customer data if needed. You’ve defined a public method test() that returns a simple string “OK”.
class Question extends \Magento\Framework\View\Element\AbstractBlock
{
// ... (constructor and test method)
public function test()
{
return "OK";
}
}
The Layout XML
In your layout file, you’re correctly referencing the product.info.details block and adding a new tab named ‘Questions’ using your custom block. You’ve also defined an inner block named question under this tab.
<referenceBlock name="product.info.details">
<block class="Magento\Catalog\Block\Product\View"
name="question.tab"
as="question"
template="Semaine2_TP::product/delivery_info.phtml"
group="detailed_info">
<!-- ... (arguments for the tab) -->
<block name="question"
class="Semaine2\TP\Block\Question"
cacheable="false"
template="Semaine2_TP::question/info.phtml"
group="detailed_info"/>
</block>
</referenceBlock>
The Problem and Solution
Why Only delivery_info.phtml is Rendered
The issue lies in the way you’ve defined your blocks. When you create a block inside another block, Magento looks for the template file relative to the outer block’s namespace (Magento\Catalog\Block\Product\View). This is why only delivery_info.phtml from this namespace is being rendered.
The Correct Approach
To solve this issue, you need to ensure that both your custom block and its associated template file are properly registered with Magento:
- Update your layout XML to reference the full path of your custom block’s class:
<block name="question"
type="Semaine2\TP\Block\Question"
cacheable="false"
template="Semaine2_TP::question/info.phtml"
group="detailed_info"/>
-
In your custom block, you can access the customer session and other data as needed. However, keep in mind that Magento’s object manager is deprecated; consider using a constructor with type-hinted dependencies instead.
-
To use data from your block in
delivery_info.phtml, simply call the appropriate method:
<?php echo $block->getTest(); ?>
Remember to clear your cache after making these changes.
Conclusion
You now have a solid understanding of how to create and use custom blocks in Magento 2. By properly registering your blocks with full class paths and using them within your templates, you can easily retrieve data and enhance the functionality of your product detail pages.
For more advanced techniques and best practices for custom block development in Magento 2, visit our comprehensive guide at https://laravelcompany.com/magento-2-custom-block-development.
Happy coding!