[Mini-Tutorial] Magento + Fishpig: Give Your eCommerce Blog a Face

So, you have a Magento site and a WordPress blog page. At one time it was woeful that these did not work together, then Fishpig came along and saved the day! Now your WordPress posts and author information appear on your page as you wish – hooray for Fishpig!

You may have noticed, however, that in order to have an avatar for your authors you must use Gravatar. What happens if you do not want to use Gravatar? You want to cut-out the middle man and use one of those great WordPress plugins. For instance, you may want to use the top rated WordPress user avatar plugin WordPress User Avatar. This is unfortunate as this WordPress plugin is not supported by Fishpig Integration. Fear not! There is a way to get this plugin to work via Fishpig.

If you’re looking for more Magento resources download our free eBook contain all of Magento’s Transactional Emails by clicking below:





Download ebook: Magento Transactional Emails




Get FishPig to Work with Custom Plug-ins

A good way to start trying to customize Fishpig into working with custom plugins is to see what the plugin is trying to achieve. In this example, we’ll play through the scenario of a user avatar that does not link through Gravatar. Once you have established this you will have a good idea of where you need to look in the WordPress Database in order to find the information you are looking for. Whatever the plugin may be trying to achieve, it is almost certain that key information and files are located in the WordPress database somewhere.

In our case we are looking for the avatar file that must, in some way, be linked to the Author in the database. It turns out that WordPress User Avatar stores the avatar as a post in the wp_posts table (these posts will not be visible in the WordPress backend) as post-type “attachment”. This post will be linked to the author you assigned the avatar to. “How?”- you may ask – “do I get that specific image if the post is just one of many?”. A great question imagined questioner! The answer is that the plug-in actually stores the post_id under the wp_usermeta table under the new meta-key wp_user_avatar. The image link itself is located in the aforementioned wp_posts table under the “Guid” column.

Related: Mini Tutorial: Custom WordPress Shortcode in Magento with Fishpig

Create a New Method to Get Your Image

Now that we know where all the data is kept we can dive into Fishpig and create a new method to get that image for you! In this particular case we are using the Fishpig WordPress User Model. This is because we are getting data from the wp_usermeta table which references the Users (authors). We could simply just write a new Method into the WordPress/Model/User.php and be done with it, but I find its neater and easier to narrow in on potential problems if I extend the model and create a method there. So as you can see below I extended the Fisphig_Wordpress_Model_User class and created the method getUserAvatar().

class Demac_Fishpig_Model_Wordpress_User extends Fishpig_Wordpress_Model_User {
    public function getUserAvatar(){
        $imageSource = “”;
        $avatarPostId = $this->getMetaValue(‘wp_user_avatar’);
        $collection = Mage::getResourceModel(‘wordpress/post_collection’)->addPostTypeFilter(‘attachment’)->addFieldToFilter(‘ID’, $avatarPostId);
        foreach($collection as $post){
            if ($post->getId() == $avatarPostId){
                $imageSource =  $post->getData(‘guid’);
                return $imageSource;
            }
        }
        return $imageSource;
    }
}

As you can see the method is extremely simple. For our purposes $this is the User model and we are calling $this->getMetaValue(‘wp_user_avatar’) which, if the user has one will return a post id number. If it doesn’t then it will return just a null value. After that we are simply just looping through the posts to find the one that matches the value and getting the ‘Guid’ value of the post which is the image source which is returned. If there is nothing it returns an empty string.

Where to use this method?

There are only two places where I found it useful to use this method and it’s in: app\design\frontend\enterprise\terra20\template\wordpress\homepage\author\view.phtml

and

app\design\frontend\enterprise\terra20\template\wordpress\author\view.phtml

In both cases I used a variation of this:

<div class="author-box">
        <div class="author-headshot">
            <?php $avatarImage = $author->getUserAvatar()?>
            <?php if($avatarImage!=""):?>
                <img class = "headshot" src ="<?php echo $avatarImage?>" alt="author"/>
            <?php else:?>
                <img class = "headshot" src="<?php echo $author->getGravatarUrl(139); ?>" alt="author" />
            <?php endif;?>
         </div>
</div>

As you can see you get the avatar image source (the method returns an empty string if there isn’t one). This allows you to use Gravatar as well if you don’t have an avatar uploaded for that user yet.

Related: Mini Tutorial: Adding WordPress Posts & Categories to Magento’s Google Sitemap

The WordPress Database and Fishpig

General Note on getting custom tables and data from the WordPress Database through Fishpig: this post may not be helpful to you if you are trying to use separate widgets similar to this one, but store their data differently, or you may be using a plugin that doesn’t do this kind of thing at all. On that note there is a great example file you should look to at see how Fishpig gets its data so that you may better understand how you could go about it.

Please check out: Fishpig_Wordpress_Model_Resource_Post and the Method getParentCategoryIdsByPostIds. This is a good example of how Fishpig grabs its data. If you look at it you will notice that it wraps a resource model around an SQL select statement to access the specific table. Using this as a guideline you can write your own resource model and SQL statement in order to get any custom data you need from whichever table you might need.

Related: [Mini-Tutorial] Magento and WordPress Multisite Integration

Interest in becoming a Magento Certified Solution Specialist? We created a study guide to help you in your journey, download it for free below:





Download Study Guide