0

I've been looking everywhere but no luck with anything that works. I have Magento ver. 1.9.2.0 and I'm trying to move the related products block to the product view template using the Local.xml. I found several ways described showing how to do it in catalog.xml and a couple using the local.xml but they didn't work for me. Here's what I've got so far:

In my Local.xml

<catalog_product_view>
<reference name="product.info">
<block type="catalog/product_list_related" name="catalog.product.related" after="upsell_products" template="catalog/product/list/related.phtml"/>
</reference>
<reference name="right">
    <remove name="catalog.product.related"/>
</reference>

In my view.phtml

<?php endforeach;?>
    <?php echo $this->getChildHtml('upsell_products') ?>
    <?php echo $this->getChildHtml('product_additional_data') ?>
    <?php echo $this->getChildHtml('related') ?>

I'm wondering if it's even possible to use only the Local.xml to do this. Thanks!

3 Answers 3

1

You almost have it. When you use the remove layout element this is the last element that is compiled before rendering the layout so if you have two elements with the same name, as you have above this will remove both of these. Look at using unset or change the name of the related block:

<action method="unsetChild">
  <name>catalog.product.related</name>
</action>

Also when you are calling this block make sure you call the name, in your example you are using <?php echo $this->getChildHtml('related') ?> when the block's name is catalog.product.related

4
  • actually we should use alias name to refer a block inside a phtml file. Not the the name attribute Aug 13, 2015 at 1:12
  • Here's what I have: <catalog_product_view> <action method="unsetChild"> <name>catalog.product.related</name> </action> <reference name="product.info"> <block type="catalog/product_list_related" name="catalog.product.related" as="related" template="catalog/product/list/related.phtml"/> </reference> </catalog_product_view> and in my view.phtml this:
    – Freejoy
    Aug 13, 2015 at 7:33
  • <?php echo $this->getChildHtml('upsell_products') ?> <?php echo $this->getChildHtml('product_additional_data') ?> <?php echo $this->getChildHtml('catalog.product.related') ?> No errors in system.log When I added the unsetChild it made my related sideblock reappear. But nothing shows up in view.phtml
    – Freejoy
    Aug 13, 2015 at 7:33
  • If you add hello to app/design/frontend/base/default/catalog/product/list/related.phtml does this display? Just remember to remove it afterwards :)
    – rob3000
    Aug 13, 2015 at 8:13
1

You are almost there. Try this code.

<catalog_product_view>
    <remove name="catalog.product.related" />
    <reference name="product.info">
        <block type="catalog/product_list_related" name="catalog.product.related" as="related" template="catalog/product/list/related.phtml"/>
    </reference>
</catalog_product_view>

Here there is two main difference with your code.

  1. I added remove node first. While Magento renders the layout, the ordering of nodes has importance. Since we are removing already existing related block catalog.product.related very first, it will safely remove that block without affecting the declaration of our new related block. Due to this, you can safely re-use catalog.product.related name for our new block.

  2. Note that, our new related block now have an as property. It is also known as alias name. In a phtml template, alias name should be used. Since you have called this block like <?php echo $this->getChildHtml('related') ?>, which implies that related is the alias name of our new related block.

Hope that will help you to sort out this issue.

8
  • Thanks for the reply. I tried it and it's still not showing up. We must be missing something or it might not be possible. I have Debug Toolbar and it shows the layout.xml is working but no related.phtml block.
    – Freejoy
    Aug 13, 2015 at 6:16
  • did you remove the cache ? and you should enclose those codes inside <layout /> node, if that is not present Aug 13, 2015 at 6:22
  • I just noticed I have another module called outofstocksubscription that replaced my view.phtml. It's located here:/template/outofstocksubscription/catalog/product/view.phtml I added the code to this new view.phtml but it didn't work. Will I have to change anything in the Local.xml so it will work?
    – Freejoy
    Aug 13, 2015 at 6:39
  • Here's what in outofstocksubscription.xml <layout version="0.1.0"> <catalog_product_view> <reference name="product.info"> <action method="setTemplate"> <template>outofstocksubscription/catalog/product/view.phtml</template> </action> </reference> </catalog_product_view> </layout>
    – Freejoy
    Aug 13, 2015 at 6:43
  • ok did you try my first comment. you have that code enclosed inside layout node ? did you clear your cache. Aug 13, 2015 at 6:48
0

I finally got it with the clues here and a little more digging. Here's how to do it:

    <catalog_product_view>
    <reference name="right">  
        <action method="unsetChild">
            <name>catalog.product.related</name>
        </action>
    </reference>    
    <reference name="product.info">
        <block type="catalog/product_list_related" name="catalog.product.related" as="related" template="catalog/product/list/related.phtml"/>
    </reference>
</catalog_product_view>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.