You need to can add this to one of your layout files:
<catalog_product_view>
<reference name="head">
<block type="core/template" name="open-graph" as="open-graph" template="opengraph/product.phtml" />
</reference>
</catalog_product_view>
Now you need to create the file opengraph/product.phtml
inside you theme (app/design/frontend/{package}/{theme}/template/
) with the following content:
<?php $_product = Mage::registry('current_product'); ?>
<?php if ($_product) : ?>
<meta property="og:url" content="<?php echo "http://". $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];?>"/>
<meta property="og:description" content="<?php echo htmlspecialchars($_product->getDescription())?>"/>
<meta property="og:title" content="<?php echo htmlspecialchars($_product->getName())?>"/>
<meta property="og:image" content="<?php echo $this->helper('catalog/image')->init($_product, 'image')?>"/>
<?php endif;?>
If you need to include the sku
or color
in there you can get them like this:
$sku = $_product->getSku();
$color = $_product->getAttributeText('color');
Clear the cache when you are done.
The Meta Description for each page is loaded in the file
[your magento install dir]/app/design/frontend/YOURPACKAGE/YOURTHEME/template/page/html/head.phtml
If your theme does not have its own head.phtml
file then look in the
/default/default/
and /base/default/
directories
By default the code which renders the Meta Description looks like this
<meta name="description" content="<?php echo htmlspecialchars($this->getDescription()) ?>" />
So first check for possible changes in this code.
Now for product pages the getDescription()
function call gets its data from
[your magento install dir]/app/code/core/Mage/Catalog/Block/Product/View.php
By default the _prepareLayout()
function in this file should contain the following code for retreiving the Meta Description
$description = $product->getMetaDescription();
if ($description) {
$headBlock->setDescription( ($description) );
} else {
$headBlock->setDescription(Mage::helper('core/string')->substr($product->getDescription(), 0, 255));
}
So also check this code for any changes. This code could also be overwritten in
[your magento install dir]/app/code/local/Mage/Catalog/Block/Product/View.php
If you do not find any changes then this may be caused by some extenion. A good way to trouble shoot is to disable all extensions (one by one) and/or change to the base/default theme to see what happens.
Best Answer
I see 3 ways to tackle this.
Option 1.
Have a cron that runs every night that builds this meta description for each product. This way you don't interfere with the Magento flow for frontend.
The code can look something like this (untested)
This makes sense if your meta descriptions are going to change a lot and if you don't have a lot of products.
Option 2.
Rewrite the product model
Mage_Catalog_Model_Product
and add a method calledgetMetaDescription()
like this:This will generate the meta description on the fly. It makes sense if you don't have a very complicated discount formula. I would choose this one. I wrote it as the second one (in the middle) to see if you are paying attention. :)
Option 3.
Is very similar to the one above, but involves modifying the block that adds the meta description. You should use this in case you don't want to touch the product model.
You have to rewrite the
Mage_Catalog_Block_Product_View
and in the method_prepareLayout
replace$description = $product->getMetaDescription();
with