How to make a simple module in OpenCart? Example g

2019-01-21 10:16发布

问题:

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?

回答1:

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?



回答2:

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.