24

I am new to this forum and as well as to OpenCart.

I need help on creating a module in OpenCart. In my case it will get the latest 5 posts from each category of my WordPress installation and display it in my home page of my OpenCart store.

I have already installed OpenCart and WordPress in same database on the same host.

Can someone advice me on this?

TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209
Developer
  • 477
  • 1
  • 4
  • 5
  • 3
    http://wordpress.stackexchange.com/ is for wordpress questions ... – Eugen Nov 03 '12 at 10:59
  • 6
    @Eugen, that's true but this is more of an Opencart question IMO. Developer, welcome to SO - you are going to need to put a lot more effort into your question than that to get any help here. Add what you've tried to your question, add the code etc. 'Please do all my work for me' type questions just get downvoted or closed here. – McNab Nov 03 '12 at 12:54

2 Answers2

32

This can be very easy depending on your skills. I expect a downvote on your question but I will briefly run through the steps since this is not the way SO works. The first thing is to edit our THEMES files. Since OpenCart is MVC, we edit our Theme and then our PHP... or PHP and then the THEME files.. this is vice versa..

Guide

1 - Open /catalog/view/theme/default/template/common/home.tpl

After this line:

<h1 style="display: none;"><?php echo $heading_title; ?></h1>

Add this:

<?php MyWordPressFunction() ?>

or this:

<div>
    <h2>Latest posts from our blog</h2>
    <?php MyWordPressFunction() ?>
</div>

2 - Open our PHP code which is now the code for the home.tpl page, this is /catalog/controller/common/home.php

At the bottom of the code after the main class and the ending ?> PHP tag add this:

// WORDPRESS LATEST POSTS
//#customPHP
// The tag above is so that when you upgrade OpenCart
// Before doing so you need to make sure of all the core
// core changes you made - a unique global comment tag
// is easy to find.

function MyWordPressFunction(){

    // DB
        // GET THE POSTS
        // LIMIT BY 5
        // ORDER BY LATEST POSTS
    $sql=mysql_query("SELECT * FROM `wordpress_db`.`wp_posts` ORDER BY `wp_posts`.`post_date` DESC LIMIT 5");
    while($row = mysql_fetch_array($sql)){

        // VARS (easy to play with in the echo)

        $id=$row["ID"];
        $author=$row["post_author"];
        $date=$row["post_date"];
        $post=$row["post_content"];
        $title=$row["post_title"];

        echo '

            <div id="postID_'.$id.'>

                <h3>'.$title.'</h3>

                <p>'.$post.'</p>

                <p>Posted by '.$author.' on '.$date.'</p>

            </div>

        ';

    }

    // END DB

}

That should give you an idea of some basic PHP function calls. It is a direction to get you started. You can further expand to link categories, author links, etc..

By the way, all these variables can be used as you can see in the WP_Posts table:

/*

All these can be used

ID
post_author
post_date
post_date_gmt
post_content 
post_title
post_excerpt
post_status
comment_status
ping_status
post_password
post_name
to_ping
pinged
post_modified
post_modified_gmt
post_content_filtered
post_parent
guid
menu_order
post_type
post_mime_type
comment_count

*/

Tips

Generally look through the entire OpenCart filter on SO - there are many articles on writing mods, modifying how it works and creating custom pages - these will really help in your long time tweaking. The above code is not got styling or further tweaks, this is a guide.

Further reading and better module type posts

How to add new module to opencart administration?

How to add new module to opencart administration?

How to create a custom admin page in opencart?

How to create a custom admin page in opencart?

How do I get an external page coupon/voucher form to work in OpenCart?

How do I get an external page coupon/voucher form to work in OpenCart?

Opencart - How I can execute a custom code on product page? Without mods on controller product

Opencart - How I can execute a custom code on product page? Without mods on controller product

How can I display the SubTotal on OpenCart on any page?

How can I display the SubTotal on OpenCart on any page?

Community
  • 1
  • 1
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209
  • 1
    @Developer Why this answer is still not accepted? Please, accept the answer so it is clear Your problem has been solved. Thank You! – shadyyx May 15 '13 at 15:37
  • @TheBlackBenzKid How do you actually connect to the database in this situation? Since you are adding code to OC but need to query the DB in WP. I am trying to accomplish the the same thing and the blog is just a subdomain. blog.url.com (WP) and url.com (OC) – Travis Dec 18 '13 at 00:55
  • @Travis in that way. Make the MySQL connection variables above the loop - not a clean way but a fast solution – TheBlackBenzKid Jul 30 '16 at 07:52
1

Thank you TheBlankBenzKid for a very helpful answer, but I think there is one small thing worth adding here. If you do wish to display your wordpress blogs on your opencart shop, make sure you give your wordpress databases the correct user permissions to the opencart database user, done im my case via cpanel.

Gavin Simpson
  • 2,766
  • 3
  • 30
  • 39
  • Thanks for the answer. Not everyone has cPanel and I think it would be unwise to do that. It depends on the situation of the database and blog setup. We for example have a blog hosted on a separate server. – TheBlackBenzKid Apr 09 '13 at 14:09
  • I don't think you would actually have a choice? You are accessing the WP database, and it does not have the same login credentials as the OC database. For me the answer was as I stated, as I just got a 'access denied' error message without it. – Gavin Simpson Apr 09 '13 at 18:34
  • I stand corrected, maybe. Did another installation and no access errors this time, but the latest install is on my apache localhost. – Gavin Simpson Apr 11 '13 at 16:04