2

For my custom module i have added few more meta info to products.Its added to the product meta information tab and getting saved for each product. I dont know how to make it display in app\design\frontend\base\default\template\page\html\head.phtml along with other meta tags. Please help

1

There is no need to rewrite magento blocks for this and it's not a good practice to rewrite core magento blocks, models, etc.

Better way to do this is to create your own block in your custom module and after that include it to layout via xml. Also you should have a layout update for your module

<catalog_product_view>
    <reference name="head">
        <block type="custommodule/product_head" name="product_head" template="custommodule/product/head.phtml" />
    </reference>
</catalog_product_view>

In page/hhtml/head.phtml file you just need to add this:

<?php echo $this->getChildHtml('product_head'); ?>

This give you a possibility to put whatever you want in your template file (custommodule/product/head.phtml) and methods you want in your block class.

Other way is to use events, but i dont have time to explain it now so you can google it :)

  • Nice approach. Just one comment. You don't need to add ?php echo $this->getChildHtml('product_head'); ?> to head.phtml. The template already has <?php echo $this->getChildHtml() ? that will print all it's children. If you add that line you will get duplicate meta tags. Other than that, nice and clean. +1. – Marius Apr 30 '14 at 7:11
  • Yes, i forgot that. Thanks for the correction. – Miroslav Petroff Apr 30 '14 at 7:17
  • Its working fine.Thanks.But am facing one more problem with select box i have added to the attribute.Option value is having attribute id.How to change this to have my own values for options ? – Ann Mary Apr 30 '14 at 7:36
  • Can you be more specific what you want to do because i can't understand what are trying to do? Show some code if you don't mind. – Miroslav Petroff Apr 30 '14 at 7:51
  • I added one select box with add attribute function to product.Its showing <option value="129">INDEX</option> like this. but i wish to have <option value="INDEX">INDEX</option>. Is it possible? – Ann Mary Apr 30 '14 at 12:25
0

Rewrite class Mage_Page_Block_Html_Head in a your custom local module. Add to method _separateOtherHtmlHeadElements:

case 'meta_property':
    $lines[$itemIf]['other'][] = sprintf('<meta%s content="%s" />', $params, $href);
    break;

Rewrite class Mage_Catalog_Block_Product_View in same or other local module. Add to method _prepareLayout:

$headBlock->addItem('meta_property', $product->getData('custom_attribute'), 'my_html_attribute="value_of_attribute_html"');

An example:

$product = $this->getProduct(); // which is Mage::registry('product')
$headBlock->addItem('meta_property', $product->getName(), 'property="og:title"');

will output:

<meta property="og:title" content="Lorem Ipsum Product Name" />

Note, the values ($product->getData()) are already fetched previously by core blocks/controllers or it assures that $this->getProduct() is fetched from db only once globally. The result is no additional queries. If you add more custom code that executes SQL queries, is good to know that _prepareLayout() isn't cached.

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.