2

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.

  • Than you, but not completely, see my new comment – domfitty May 1 at 9:44
1

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!

  • I tried the following code ` <referenceBlock name="catalog.product.related"> <arguments> <argument name="template" xsi:type="string">Magento_Catalog::product/list/altriformati.phtml</argument> </arguments> </referenceBlock> ` but template is always Magento_Catalog::product/list/items.phtml Here devtools results name catalog.product.related type block class Magento\Catalog\Block\Product\ProductList\Related template Magento_Catalog::product/list/items.phtml module Magento_Catalog – domfitty May 1 at 9:40
  • Okay, nop. as per my answer I mentioned, I am sharing just knowledge. not an exact solution. :) – Chirag Patel May 1 at 9:53
0

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" />
0

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;?>

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.