Tự động tạo file sitemap.xml cho website Opencart

Thay thế địa chỉ sitemap truyền thống (site.com/index.php?route=extension/feed/google_sitemapbằng file sitemap tự động (site.com/sitemap.xml), 1 module khá hay có thể edit file php thủ công nếu phiên bản opencart của bạn không tương thích khi cài upload module.

Tự động tạo file sitemap.xml cho website Opencart


Sitemap của Opencart hoạt động ra sao sau khi thay đổi

Giải thích cơ chế cũ mới của sitemap?

Mặc định site map Opencart như thế nào: 
Bạn cần bật Feed trong module quản trị để sitemap có thế hoạt động và cần RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L] trong .htaccess để có đường link ngắn ( vd: site.com/sitemap.xml

Sử dụng module mới cơ chế sitemap hoạt động sẽ rao sao?

Sẽ vẫn cần bật Feed sitemap cũ lên và cần chỉnh lại code 1 chút theo module dưới. Khi đã mod xong hãy chạy lại đường link sitemap cũ để tạo ra file sitemap.xml trên root nhé, tức là khi chạy lại đường link sitemap cũ thì thấy trắng trang nhưng nó đã tạo ra file sitemap.xml trên root chứa website, và  khi có link sản phẩm mới bạn cần bấm lại để cập nhật lại sitemap.xml mới nhất, quá tiện phải không (nên để link sitemap cũ ở phần quản trị để mỗi khi cập nhật sản phẩm thì bấm lại link sitemap để tạo mới sitemap.xml )!

Tải về và hướng dẫn Cài đặt module sitemap cho Opencart 3x

Tải về module trên Opencart.Com


Nếu không tải được hoặc link die thì xem nội dung file mod sau:

<?xml version="1.0" encoding="utf-8"?>
<modification>
  <name>sitemap.xml from Google Sitemap</name>
  <code>sitemap-xml</code>
  <version>1.0</version>
  <author>Focus Style</author>
  <link>https://focus.style</link>
  <file path="catalog/controller/extension/feed/google_sitemap.php">
    <operation>
      <search><![CDATA[$this->response->setOutput($output);]]></search>
      <add position="after"><![CDATA[
        // sitemap.xml generator
  $rootPath = realpath(DIR_APPLICATION . '..');
  $file_name_and_path = $rootPath.'/sitemap.xml';
  $dirname = dirname($file_name_and_path);
  $handle = FALSE;
  if($file_name_and_path && !file_exists($file_name_and_path)){
  $handle = fopen($file_name_and_path, "w");
  } elseif(file_exists($file_name_and_path)) {
  $handle = fopen($file_name_and_path, "w");
  }  
  if($handle){
  fwrite($handle, $output);
  fclose($handle);
 
  // sitemap.xml generator end
        ]]></add>
    </operation>
  </file> 
</modification>

Hướng dẫn Installation and comfiguration theo tác giả

1. Download the archive (if you reading this - done).
2. In admin panel of your website go to Extensions > Installer and upload the achieve.
3. Go to Extensions > Modifications and click Refresh button.
4. Go to Extensions > Extensions  , choose type Feeds.
5. On current page click Install on the right of Google Sitemap, then click Edit.
6. On Google Sitemap page set Status to Enable, then click Save.
7. On Google Sitemap page field Data Feed Url contains 'https://yoursite.com/index.php?route=extension/feed/google_sitemap'. When you are accessing this url - the feed file sitemap.xml creates/updates.

In .htaccessfile find 
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
and comment it, like
#RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
So you will get access to URL https://yoursite.com/sitemap.xml

Hướng dẫn thủ công khi phiên bản không tương thích. 

Bạn cần sửa file google_sitemap.php và cập nhập lại với phiên bản opencart 3.x
Đường dẫn file: /catalog/controller/extension/feed/google_sitemap.php
Nội dung file:
<?php
class ControllerExtensionFeedGoogleSitemap extends Controller {
public function index() {
if ($this->config->get('feed_google_sitemap_status')) {
$output  = '<?xml version="1.0" encoding="UTF-8"?>';
$output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">';

$this->load->model('catalog/product');
$this->load->model('tool/image');

$products = $this->model_catalog_product->getProducts();

foreach ($products as $product) {
if ($product['image']) {
$output .= '<url>';
$output .= '  <loc>' . $this->url->link('product/product', 'product_id=' . $product['product_id']) . '</loc>';
$output .= '  <changefreq>weekly</changefreq>';
$output .= '  <lastmod>' . date('Y-m-d\TH:i:sP', strtotime($product['date_modified'])) . '</lastmod>';
$output .= '  <priority>1.0</priority>';
$output .= '  <image:image>';
$output .= '  <image:loc>' . $this->model_tool_image->resize($product['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_popup_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_popup_height')) . '</image:loc>';
$output .= '  <image:caption>' . $product['name'] . '</image:caption>';
$output .= '  <image:title>' . $product['name'] . '</image:title>';
$output .= '  </image:image>';
$output .= '</url>';
}
}

$this->load->model('catalog/category');

$output .= $this->getCategories(0);

$this->load->model('catalog/manufacturer');

$manufacturers = $this->model_catalog_manufacturer->getManufacturers();

foreach ($manufacturers as $manufacturer) {
$output .= '<url>';
$output .= '  <loc>' . $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $manufacturer['manufacturer_id']) . '</loc>';
$output .= '  <changefreq>weekly</changefreq>';
$output .= '  <priority>0.7</priority>';
$output .= '</url>';

$products = $this->model_catalog_product->getProducts(array('filter_manufacturer_id' => $manufacturer['manufacturer_id']));

foreach ($products as $product) {
$output .= '<url>';
$output .= '  <loc>' . $this->url->link('product/product', 'manufacturer_id=' . $manufacturer['manufacturer_id'] . '&product_id=' . $product['product_id']) . '</loc>';
$output .= '  <changefreq>weekly</changefreq>';
$output .= '  <priority>1.0</priority>';
$output .= '</url>';
}
}

$this->load->model('catalog/information');

$informations = $this->model_catalog_information->getInformations();

foreach ($informations as $information) {
$output .= '<url>';
$output .= '  <loc>' . $this->url->link('information/information', 'information_id=' . $information['information_id']) . '</loc>';
$output .= '  <changefreq>weekly</changefreq>';
$output .= '  <priority>0.5</priority>';
$output .= '</url>';
}

$output .= '</urlset>';

$this->response->addHeader('Content-Type: application/xml');
        // sitemap.xml generator
  $rootPath = realpath(DIR_APPLICATION . '..');
  $file_name_and_path = $rootPath.'/sitemap_index.xml';
  $dirname = dirname($file_name_and_path);
  $handle = FALSE;
  if($file_name_and_path && !file_exists($file_name_and_path)){
  $handle = fopen($file_name_and_path, "w");
  } elseif(file_exists($file_name_and_path)) {
  $handle = fopen($file_name_and_path, "w");
  }  
  if($handle){
  fwrite($handle, $output);
  fclose($handle);
 
  // sitemap.xml generator end 
}
}

protected function getCategories($parent_id, $current_path = '') {
$output = '';

$results = $this->model_catalog_category->getCategories($parent_id);

foreach ($results as $result) {
if (!$current_path) {
$new_path = $result['category_id'];
} else {
$new_path = $current_path . '_' . $result['category_id'];
}

$output .= '<url>';
$output .= '  <loc>' . $this->url->link('product/category', 'path=' . $new_path) . '</loc>';
$output .= '  <changefreq>weekly</changefreq>';
$output .= '  <priority>0.7</priority>';
$output .= '</url>';

$products = $this->model_catalog_product->getProducts(array('filter_category_id' => $result['category_id']));

foreach ($products as $product) {
$output .= '<url>';
$output .= '  <loc>' . $this->url->link('product/product', 'path=' . $new_path . '&product_id=' . $product['product_id']) . '</loc>';
$output .= '  <changefreq>weekly</changefreq>';
$output .= '  <priority>1.0</priority>';
$output .= '</url>';
}

$output .= $this->getCategories($result['category_id'], $new_path);
}

return $output;
}
}

Ở đây mình có đặt là sitemap_index.xml (tên này đặt giống bên wp cho google không làm khó khi index😀)
Nếu có bất kỳ thắc mắc nào, bạn hãy để lại bình luận để cùng giải đáp và chia sẻ nhé!
Tôi đi viết blog dạo...

Đăng nhận xét

f>