Adding simple Tweet this for Magento products

Featured Image

There are few different ways one can achieve this kind of functionality. Normally in Magento you would do it like a real, “independent”, module (extension) thus loosing at least two ours to make a few lines of code work.

The simplest way to add this kind of feature to your product view page is to open the view.phtml file under the app/design/frontend/default/custom/template/catalog/product/ folder.

And add the following code where you sish to show your “Tweet this” link:

<a title="Send this page to Twitter!" href="http://twitter.com/home?status=Check out the <?php echo $this->htmlEscape($_product->getName()) ?> at < ?php echo $_product->getProductUrl() ?>" target="_blank">Tweet This!</a>

.

Thats it. Remember, this is truly the simplest solution, aka Quick & Dirty. For more fancy stuff… we do custom deevelopment 🙂

Cheers.


11 comments

  1. Couldn’t get this to work in Magento 1.4.1.1 not sure what the problem is but I can’t seem to get any social links or extensions to work on my site. I’ve tried adding this code in both places base and my theme and nothing it acts like I never added a line of code at all.

  2. So it seems getProductUrl() sometimes also returns a url with full category path info ie: /47/s/balloon/category/5/…

    How do you make getProductUrl() NOT return a url using category paths. ie: http://www.mysite.com/product

    We’d like to stay away from a URL shortener. Our urls are already pretty short and we’d like the original domain / link sent to twitter.

    Thanks!

    Or is there another function floating around for this purpose?

    I’ve been searching for an answer to this, see other threads that have run into the problem, but no answer.

  3. I am not understanding where to put the code for the tiny URL. I pasted that in the top of the phtml file and it didn’t work. Can you help me with detailed instructions? Thanks! great feature by the way!

  4. Thanks for this – its perfect with the shorturl..
    I was looking around for an extension before I found this…

  5. Just to recap what anoop wrote, place this at the top of your phtml file:

    getProduct();
    // Convert URL to Bit.Ly
    $_shorturl = ‘http://bit.ly/api?url=’.$_product->getProductUrl();
    $_shorturl = file_get_contents($_shorturl);
    ?>

    and then use this in the href of your tweet status:

  6. Hi,
    Just to add to it..to shorten your urls use this one…
    $shorturl=”http://bit.ly/api?url=”.$_product->getProductUrl();
    $shorturl=file_get_contents($shorturl);

  7. Hey, in the code above there is a space after the open tag of the code providing the link. I had to take it out to make this code work. It works as advertised. Nice job.
    This is what I used to get it to work.
    getProductUrl() ?>

  8. Hey, in the code above there is a space after the open tag of the code providing the link. I had to take it out to make this code work. It works as advertised. Nice job.
    This is what I used to get it to work.
    getProductUrl() ?>

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>.