5
<meta property="og:url" content="<?php echo "http://". $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];?>"/>
<meta property="og:description" content=""/> 
<meta property="og:title" content=""/>
<meta property="og:image" content=""/>

I've tested with the head.phtml file,but I don't know how to get product attributes such like sku, color and images.

I tested with $this->helper('catalog/image')->init($_product, 'image') found in media.phtml on meta tag and get a blank page,but images on product view are working fine.

That confuses me a lot.

12

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.

  • is there any other way to add social meta on the fly – MageLerner Jan 28 at 10:15
  • 1
    I offered a quick and easy way? What other way do you have in mind? – Marius Jan 28 at 10:25

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.