Facebook “Friends Already Connected” functionality

I had a request to implement Facebook “Friends Already Connected” functionality together with “Facebook Connect” into Magento’s registration form. For Facebook Connect, I used old Ivan Weiler’s extension as base. And now let me guide you through the process. And note at stat. This is strictly developer orientated article, there’s no final solution.

First of all, since Facebook’s API has changed in las year, I had to create some minor modifications to extension I used as base, but I won’t go through details there but I’ll tell you that fix is based on API connection itself.

So, I’ve added couple of methods to Session.php as follows:

public function getUsersAlreadyConnected()
	{
		return $this->_formatUsersAlreadyConnected();
	}
 
private function _formatUsersAlreadyConnected()
	{
		$names = '';
		$return = array();
 
		$users = self::_getAlreadyConnectedData();
		$numberOfUsers = count(self::_getFriendConnectedUids());
 
		if ($numberOfUsers < 5) {
			$totalNumberOfUsers = count($users);
		} else {
			$totalNumberOfUsers = count($this->_getRegisteredUsers());
		}
 
		$storeName = Mage::getStoreConfig(self::$_storeName);
 
		if (empty($storeName)) {
			$storeName = Mage::getBaseUrl();
		}
 
		switch($totalNumberOfUsers){
			case 0:
				$names = 'No one has connected with ' . $storeName . ' yet.';
				break;
			case 1:
				$names = $users[0]['name'] . ' has connected to ' . $storeName;
				break;
			case 2:
				$names = $users[0]['name'] . ' and ' . $users[1]['name'] . ' have connected to ' . $storeName;
				break;
			case 3:
				$names = $users[0]['name'] . ' and ' . $users[1]['name'] . ' and ' .
						(int)($totalNumberOfUsers - 2) . ' other have connected to ' . $storeName;
				break;
			default:
				$names = $users[0]['name'] . ' and ' . $users[1]['name'] . ' and ' .
						(int)($totalNumberOfUsers - 2) . ' others have connected to ' . $storeName;
				break;
		}
 
		$return['data'] = $users;
		$return['names'] = $names;
 
		return $return;
	}
 
	private function _getFriendConnectedUids()
	{
		$return = array();
 
		$params = $this->getData();
		if ($params) {
		$params['client_id'] = 'enter_your_id'; 
		$params['api_key'] = Mage::getSingleton('facebook/config')->getApiKey();
		$params['signature'] = $params['sig'];
 
		$result = $this->_retrieveFacebookResponse('friends.getAppUsers',Zend_Http_Client::POST , $params);
 
		$xml = new SimpleXMLElement($result);
		unset($result);
 
		foreach ($xml->uid as $id) {
			$return[] = (string) $id;
		}
 
		unset($xml);
		} else {
			$return[] = 0;
		}
 
		return $return;
	}
 
	private function _getAlreadyConnectedData()
	{
		$return = array();
 
		$userIds = self::_getFriendConnectedUids();
		$countUserIds = count($userIds);
		if ($countUserIds < 5) {
			$oldUserIds = $this->_getRegisteredUsers()->getData();
			for ($i = $countUserIds; $i < 5; $i++) {
				$oldId = $oldUserIds[$i-$countUserIds]['facebook_uid'];
				if (!is_null($oldId)){
					$userIds[$i] = $oldId;
				}
			}
		}
 
		$params = $this->getData();
		$params['fields'] = implode(',', self::$_connectedFriendsFields);
		$params['uids'] = implode(',', $userIds);
 
		$result = $this->_retrieveFacebookResponse('users.getInfo',Zend_Http_Client::POST , $params);
 
		$xml = new SimpleXMLElement($result);
		unset($result);
 
		$i = 0;
		foreach($xml->user as $friend){
			$return[$i]['uid'] = (string) $friend->uid;
			$return[$i]['name'] = (string) $friend->name;
			$return[$i]['pic_square'] = (string) $friend->pic_square;
			$i++;
		}
 
		unset($xml);
 
		return $return;
	}

And in register.phtml I’ve added this part:

<span id="friends-already-connected">
<?php $usersAlreadyConnected = Mage::getSingleton('facebook/session')->getUsersAlreadyConnected();?>
	<div id="friends-already-connected-images">
		<?php foreach($usersAlreadyConnected['data'] as $fbUser):?>
			<img src="<?php echo $fbUser['pic_square']?>" alt="<?php echo $fbUser['name']?>">
		<?php endforeach;?>
	</div>
	<div id="friends-already-connected-message">
		<?php echo $usersAlreadyConnected['names']?>
	</div>
</span>

Now I know this isn’t going to work if you just copy/paste it into your Magento installation, but it presents a good way to implement a new feature.

And for conculsion, fully functional version looks like this:

Hope I helped someone, cheers!

You made it all the way down here so you must have enjoyed this post! You may also like:

How to increase Magento sales? With one simple extension! Mladen Lotar
Mladen Lotar, | 27

How to increase Magento sales? With one simple extension!

Add “increment field” functionality to Magento’s quantity fields Mladen Lotar
Mladen Lotar, | 32

Add “increment field” functionality to Magento’s quantity fields

Magento PDF invoice with product images extension Mladen Lotar
Mladen Lotar, | 23

Magento PDF invoice with product images extension

3 comments

  1. Hello friend, where are these files to modify

    register.phtml
    session.php

    session.php on to believe this in / app / code / community / Inchoo / Facebook / Model /

    but I find that of register.phtml

    can you help me?
    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.