Move block and set template
-
28-02-2021 - |
Question
I succesfully moved the product related block in my page layout with
<move element="catalog.product.related" destination="product.info.right" after='-'/>
I want now to apply a custom template to this block:
product/list/mytemplate.phtml instead of product/list/items.phtml How can I do?
I tried adding also
<block class="Magento\Catalog\Block\Product\ProductList\Related" name="product.review.related" template="product/list/items.phtml" />
but it don't works.
Solution 3
Just for Knowledge share: I used the following code to load related product collection and show results in a custom way (only "formato" field)
<?php
$relatedProducts = $_product->getRelatedProductCollection()
->addAttributeToSelect('formato');
?>
<?php if ((count($relatedProducts)) != 0): ?>
<div class="altriformati">
<h4 class="categorie">Altri formati disponibili:</h4>
<?php foreach ($relatedProducts as $relatedProduct) {?>
<a href="<?php echo $relatedProduct->getProductUrl(); ?>" title="acquista anche nel formato <?php echo $relatedProduct->getAttributeText('formato'); ?>">
<img src="<?php echo $store->getBaseUrl(); ?>pub/media/formati/<?php echo $relatedProduct->getAttributeText('formato'); ?>.png" alt="disponibile nel formato <?php echo $relatedProduct->getAttributeText('formato'); ?>" class="detail-icon"/>
<?php echo $relatedProduct->getAttributeText('formato'); ?>
</a>
<?php } ?>
</div>
<?php endif;?>
OTHER TIPS
You should try with reference block method.
<referenceBlock name="product.review.related">
<arguments>
<argument name="template" xsi:type="string">[Vendor]_[Module]::product/list/items.phtml</argument>
</arguments>
In above name="" write your block identifier name. and name="" in argument pass tempalte.
Or you can set a template in the block file.
echo $this->getLayout()
-createBlock('Magento\Catalog\Block\Product\ProductList\Related')
->setBlockId('product.review.related')
->toHtml();
Here I am sharing knowledge of how to set a custom template to block. template path and block identifier should be changed as per your requirement.
I hope it helps!
You should indicate the specific Module location of your template:
<block class="Magento\Catalog\Block\Product\ProductList\Related" name="product.review.related" template="Vendor_Module::product/list/items.phtml" />