Magento Themes and Magento Extensions

Magento – Share compare product page on facebook to get review from your friends

Share

Here I want to address one thing to you all it’s regarding to the compare product in magento. we all knows magento provides us an compare product functionality as a customer point of view while comparing some products we always have some questions, doubts about that products now it could be better to ask the review to your friends on facebook by sharing your compare product page.

As per developer point of view how to do that? Very Simple

Step 1: You have to override one function which is present in app/code/core/Mage/Catalog/controllers/Product/CompateController.php i.e. function indexAction()
and add the below line of code after $items = $this->getRequest()->getParam(‘items’);

<?php

 if($items){
            Mage::getSingleton('catalog/session')
                ->setCompareUrlParams($items);
      }

Now Your function will look like below

<?php

public function indexAction()
    {
        $items = $this->getRequest()->getParam('items');

        // Added code for Facebook share
        if($items){
            Mage::getSingleton('catalog/session')
                ->setCompareUrlParams($items);
        }

        if ($beforeUrl = $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED))
       {        Mage::getSingleton('catalog/session')
                ->setBeforeCompareUrl(Mage::helper('core')->urlDecode($beforeUrl));
        }

        if ($items) {
            $items = explode(',', $items);
            $list = Mage::getSingleton('catalog/product_compare_list');
            $list->addProducts($items);
            $this->_redirect('*/*/*');
            return;
        }

        $this->loadLayout();
        $this->renderLayout();
    }


Step 2: Now go to app/design/frontend/default/your_template/template/catalog/product/compare/list.phtml and add below code where you want to show the facebook share button

<div data-href="<?php echo Mage::getBaseUrl().'catalog/product_compare/index/'.Mage::getSingleton('catalog/session')->getCompareUrlParams();?>" data-type="button_count"></div>


Step 3: Now last step is to add javascript method which require to work share button add below step at the bottom of the list.phtml page.

<div></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

Comments are closed.