Custom Web Applications to Grow Your Business

(256) 429-8145

How to add a Blog into OpenCart E-commerce platform

opencart blog wordpress integrationIn my opinion OpenCart is one of the best FREE e-commerce platforms out there. However, with content being crucial to SEO one needs a blog or a way of integrating rich content into OpenCart websites. If you already have a wordpress blog but will like to integrate the content into OpenCart so that products can be integrated into your content, here is a way to do it. First, OpenCart PHP platform is constructed using the MVC approach: model, view, controller. That means every page with a view (template) must be generated by content in the model which is controlled and rendered by the controller.

1. We will begin with the model

Go to you catalog > model > catalog and create a file and name it listarticles.php. In this file you will add all you model class codes. Create the class and name it according to the name of the folder directory class ModelCatalogListarticles extends Model { public $pageTitle; Set up your your page view count for your wordpress post. Add a view column if your wordpress wp_posts table does not have one public function updateViewed($article_id) { $this->db->query("UPDATE " . DB_PREFIX . "wp_posts SET viewed = (viewed + 1) WHERE ID = '" . (int)$article_id . "'"); } The listarticles function will select the articles in the wp_posts table. This function will be used to create a link to articles if you need to public function listarticles() { $query = $this->db->query("SELECT p.ID AS aid, p.post_content, p.post_title, p.post_status, p.post_name, p.post_parent, c.ID, FROM " . DB_PREFIX . "wp_posts p LEFT JOIN wp_category c ON c.ID = p.post_parent WHERE post_parent != '0' ORDER BY aid DESC LIMIT 5"); return $query->rows; } The getArticle function will get article by ID and will be used to display the entire article content public function getArticle($article_id) { $query = $this->db->query("SELECT *FROM " . DB_PREFIX . "wp_posts WHERE ID = '" . (int)$article_id . "'"); $this->pageTitle = $query->row['post_name']; return $query->rows; } The getOtherArticles function will get other wordpress articles other than the one currently viewed. This can operate just as next and previous post in wordpress public function getOtherArticles() { $query = $this->db->query("SELECT p.ID AS aid, p.post_content, p.post_title, p.post_status, p.post_name, p.post_parent, c.ID, FROM " . DB_PREFIX . "wp_posts p LEFT JOIN wp_category c ON c.ID = p.post_parent WHERE p.ID > '" . $this->request->get['article_id']. "' LIMIT 7"); return $query->rows; } The productsFromArticle function will help you display products that are related to the article title before or after each article content. To effectively used this function you will have to make meta_description column in the OpenCart product_description table FULLTEXT. The function looks for a MATCH between the page title and the product description. public function productsFromArticle() { $articleproduct = $this->db->query("SELECT pd.meta_description,, pd.product_id, pd.description, p.image, p.tax_class_id, p.price FROM " . DB_PREFIX . "product_description pd LEFT JOIN " . DB_PREFIX . "product p ON p.product_id = pd.product_id WHERE MATCH(meta_description) AGAINST ('".$this->pageTitle."' IN BOOLEAN MODE) AND length_class_id = '1' AND status = '1' LIMIT 2"); if ($articleproduct->num_rows) { return $articleproduct->rows; } } Close the class }

2. We Will Create the Controller Class

Go to catalog/controller/common and create a file listarticles.php Create the class and include the listarticles model, the image model (you will need this for image resizing) and the product model for rendering products in your articles load->model('catalog/listarticles'); $this->load->model('tool/image'); $this->language->load('product/product'); Load the update page views model. You can add text to title here to make it more seo frendly $this->model_catalog_listarticles->updateViewed($this->request->get['article_id']); Load the getArticle model $results = $this->model_catalog_listarticles->getArticle($this->request->get['article_id']); foreach($results as $result) { $this->data['posts'][] = array('title' => $result['post_title'],'content' => $result['post_content']); } Set title and description $this->document->setTitle($result['post_title'].': Add any text to title for seo'); $this->document->setDescription($result['post_title']); Load the getOtherArticle method / model to display other articles $articles = $this->model_catalog_listarticles->getOtherArticles(); if (is_array($articles)) { foreach($articles as $article) { $this->data['morearticles'][] = array('title' => $article['post_title'],'url' => $article['post_name'], 'cat' => $article['name'], 'id' => $article['aid']); } } load productsFromArticle method / model to display articles products $aproducts = $this->model_catalog_listarticles->productsFromArticle(); if($aproducts) { foreach($aproducts as $aproduct) { if ($aproduct['image']) { $this->data['thumb'] = $this->model_tool_image->resize($aproduct['image'], 200, 150); } else { $this->data['thumb'] = ''; } if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) { $price = $this->currency->format($this->tax->calculate($aproduct['price'], $aproduct['tax_class_id'], $this->config->get('config_tax'))); } else { $price = false; } $this->data['products'][] = array('name' => $aproduct['name'], 'content' => $aproduct['description'], 'thumb' => $this->data['thumb'], 'price' => $price, 'product_id' => $aproduct['product_id'], 'href' => $this->url->link('product/product', 'product_id=' . $aproduct['product_id']) ); } } Create the buy now button $this->data['button_cart'] = $this->language->get('button_cart'); Assign the view template to display the information if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/articles.tpl')) { $this->template = $this->config->get('config_template') . '/template/common/articles.tpl'; } else { $this->template = 'default/template/common/articles.tpl'; } If you will be using other parts of OpenCart $this->children = array( 'common/column_left', 'common/column_right', 'common/content_top', 'common/content_bottom', 'common/footer', 'common/header' ); Render the pages $this->response->setOutput($this->render()); } }

3. Now Let's Set Up the View

This is the most exciting moment: display the contents of your wordpress into OpenCart Create a listarticles.ptl file in your view/theme/template/common folder and place the following within it. <?php echo $header; ?> <div id="content"><div> <?php foreach($posts as $post): echo "<h1>".$post['title']."</h1>"; $content = $post['content']; $content = preg_replace("%\n%", "<p>", $content); echo "<p>$content</p>"; endforeach; ?> <div style="width:400px; float:left; margin-right:10px;"> <?php if (isset($morearticles)) { echo "<h2>More Related Articles</h2>"; foreach($morearticles as $more): echo "<p>&raquo; <a href=\"".$more['cat']."/".$more['url'].".html\">".$more['title']."</a></p>"; endforeach; } ?> </div> <div style="width:530px; float:left;"> <?php if (isset($products)) { ?> <div> <div> <?php foreach ($products as $product) { ?> <div> <?php if ($product['thumb']) { ?> <div><a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" /></a></div> <?php } ?> <div><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></div> <?php if ($product['price']) { ?> <div> <?php echo $product['price']; ?></div> <?php } ?> </div> <?php } ?> </div> </div> <?php } ?> </div> </div> </div> <?php echo $footer; ?>

4. Last Thing is to make URL friendly

You will now need to update your url_alias OpenCart table where seo url are mapped. Mapping will depend on your wordpress blog URL structure. In this cases all URL's uses a ".html" at the end ID is the id of the wordpress wp_posts table. article_id is the new id $_GET variable of your new content management system INSERT INTO url_alias (query, keyword) SELECT CONCAT('article_id=',ID), CONCAT(post_name,'.html') FROM wp_posts