How to show latest tweet on magento site

Home / Magento / How to show latest tweet on magento site


Display latest tweet on magento site without any extension.

Twitter is most popular social networking site in a day.Using this code publish their Twitter account posts directly onto any section of their Magento store.This is a fast and effective way to keep your website up to date with relevant information. Once integrated, messages are able to appear directly into a designated section of your website and will appear in real-time as soon as the messages are posted.

Benefit of twitter integrate into magento store

  • Keep your Magento store live with fresh information via Twitter data feeds
  • Fast and effective way to integrate your Magento store with Twitter
  • Keep your website up to date with relevant information posts

Step 1: Create magento_tweet.phtml from catalog/product/magento_tweet.phtml

<?php
function getTime($time){
 $tweetdate = $time;
 $tweet = $tweettag["content"];
 $timedate = explode(" ",$tweetdate);
 $date = $timedate[0];
 $time = substr($timedate[1],0, -1);
 $tweettime = (strtotime($date." ".$time))+3600; // This is the value of the time difference - UK + 1 hours (3600 seconds)
 $nowtime = time();
 $timeago = ($nowtime-$tweettime);
 $thehours = floor($timeago/3600);
 $theminutes = floor($timeago/60);
 $thedays = floor($timeago/86400);
 /********************* Checking the times and returning correct value */
 if($theminutes < 60){
 if($theminutes < 1){
 $timemessage =  "Less than 1 minute ago";
 } else if($theminutes == 1) {
 $timemessage = $theminutes." minute ago.";
 } else {
 $timemessage = $theminutes." minutes ago.";
 }
 } else if($theminutes > 60 && $thedays < 1){
 if($thehours == 1){
 $timemessage = $thehours." hour ago.";
 } else {
 $timemessage = $thehours." hours ago.";
 }
 } else {
 if($thedays == 1){
 $timemessage = $thedays." day ago.";
 } else {
 $timemessage = $thedays." days ago.";
 }
 }
 return $timemessage;
}

?>
<div id="twitter_div">
	<ul id="twitter_update_list">

	 <?php
	 $channel = new Zend_Feed_Rss('http://twitter.com/statuses/user_timeline/justwebdevelop.rss'); // Here you can add your twitter account link
	 Or
	 $channel = new Zend_Feed_Rss('https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=username'); // get from your twitter account
	 
	 $i = 0;
	 foreach ($channel as $item):
	 if ($i<3){ // limit to 3 ?>
	 <?php
	 $date = $item->pubDate('j F Y');
	 $date = substr($date, 0, -15);
	 $text = $item->description;
	 utf8_encode($text);
	 $timemessage = strtotime($item->pubDate());
	 $timemessage = date("Y-m-d H:i:s",$timemessage);
	 ?>
	 <li><?php echo $item->title; ?><br/>
	 <a href="<?php echo $item->link; ?>" title="<?php echo $item->title; ?>">Read Tweet &raquo;</a>
	 <span><?php echo getTime($timemessage); ?></span></li>
	 <?php
	 } // end the loop
	 $i++;
	 endforeach;
	 ?>
</ul>
</div>

Step 2: Call magento_tweet.phtml file from catalog.xml file.
Add Below code into your catalog.xml file in left column.

<block type="core/template" name="latest.tweet" as="magento_tweet" template="catalog/product/magento_tweet.phtml"/>

So now you catalog data look like below:

<reference name="left">
	<block type="core/template" name="latest.tweet" as="get_tweet" template="catalog/product/get_tweet.phtml"/>
	<block type="core/template" name="left.permanent.callout" template="callouts/left_col.phtml">
		<action method="setImgSrc"><src>images/media/col_left_callout.jpg</src></action>
		<action method="setImgAlt" translate="alt" module="catalog"><alt>Our customer service is available 24/7. Call us at (555) 555-0123.</alt></action>
		<action method="setLinkUrl"><url>checkout/cart</url></action>
	</block>
</reference>

So now your magento site look like below

Please Look at CSS Style Sheet

/********* Start Twitter CSS **************/
#twitter_div {
	background-image: url('../images/twitter-bg.png');
	background-repeat: no-repeat;
	border-bottom-style: solid;
	border-bottom-width: 1px;
	border-bottom-color: #5AA5BC;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 0.9em;
	margin-top:10px;
	padding-top: 30px;
	padding-right: 5px;
	padding-left: 5px;
	padding-bottom:15px;
}
 
#twitter_div ul li {
	color: #0C93BA;
	border-bottom-style: solid;
	border-bottom-width: 1px;
	border-bottom-color: #A1E8F7;
}
 
#twitter_div ul li a {
	text-decoration: none;
	color: #DDA84E;
}
 
#twitter_div ul li a:hover {
	text-decoration: none;
	color: #D78E42;
}
 
#twitter_div p {
	text-align: right;
	padding-right: 6px;
	padding-bottom: 10px;
}
/********* End Twitter CSS **************/

Related Posts

Leave a Reply

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