web hosting
build a website

Magento Facebook Open Graph dynamic code

Aug 16, 2015   //   by Anthony Devine   //   Website Development tips  //  No Comments

The below should be added to the head of your Magento site, it specifies what image, description, title etc.. Facebook should use for a static page, a category, product page, checkout page and default page.

<?php /* Open Graph Protocol for Facebook and SEO START */ ?>
<?php if(Mage::registry('current_product')): ?>
 <?php $product = Mage::registry('current_product'); ?>
 <meta property="og:title" content="<?php echo ($product->getName()); ?>" />
 <meta property="og:type" content="product" />
 <meta property="og:image" content="<?php $imageproducturl = Mage::helper('catalog/image')->init($product, 'small_image')->resize(285,285); $newimageproducturl = str_replace('https://', 'http://', $imageproducturl ); echo $newimageproducturl; ?>" />
 <meta property="og:image:secure_url" content="<?php echo Mage::helper('catalog/image')->init($product, 'small_image')->resize(285,285);?>" />
 <meta property="og:image:type" content="image/png">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
 <meta property="og:url" content="<?php echo Mage::registry('product')->getProductUrl(); ?>" />
 <meta property="og:description" content="<?php echo strip_tags(($product->getShortDescription())); ?>" />
 
<?php elseif(Mage::registry('current_category')): ?>
 <meta property="og:title" content="<?php echo $this->getTitle() ?>" />
 <meta property="og:type" content="product.group" />
 <meta property="og:image" content="http://imagelink/facebookshare/header-logofb.jpg" />
 <meta property="og:image:secure_url" content="https://imagelink/facebookshare/header-logofb.jpg" />
 <meta property="og:image:type" content="image/jpg">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
 <meta property="og:url" content="<?php echo $this->helper('core/url')->getCurrentUrl();?>" />
 <meta property="og:description" content="<?php echo strip_tags($this->getDescription()) ?>" />

<?php elseif((Mage::getSingleton('cms/page')->getIdentifier() == 'home' &&
 Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms')) : ?>
 <meta property="og:title" content="<?php echo $this->getTitle() ?>" />
 <meta property="og:type" content="website" />
 <meta property="og:image" content="http://imagelink/facebookshare/header-logofb.jpg" />
 <meta property="og:image:secure_url" content="https://imagelink/facebookshare/header-logofb.jpg" />
 <meta property="og:image:type" content="image/jpg">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
 <meta property="og:url" content="<?php echo $this->helper('core/url')->getCurrentUrl();?>" />
 <meta property="og:description" content="<?php echo strip_tags($this->getDescription()) ?>" />

 <?php elseif(Mage::app()->getFrontController()->getRequest()->getRouteName() == "checkout") : ?>
 <meta property="og:image:secure_url" content="https://imagelink/facebookshare/hawkheads_logofb.jpg" />

<?php else: ?>
 <meta property="og:title" content="<?php echo $this->getTitle() ?>" />
 <meta property="og:type" content="article" />
 <meta property="og:image" content="http://imagelink/facebookshare/header-logofb.jpg" />
 <meta property="og:image:secure_url" content="https://imagelink/facebookshare/header-logofb.jpg" />
 <meta property="og:image:type" content="image/jpg">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
 <meta property="og:url" content="<?php echo $this->helper('core/url')->getCurrentUrl();?>" />
 <meta property="og:description" content="<?php echo strip_tags($this->getDescription()) ?>" />

<?php endif; ?>
<?php /* Open Graph Protocol for Facebook and SEO END */ ?>