Big Sitemap in OpenCart 3x

If you have many pages of the product on the OpenCart, then the standard module of google sitmap may not handle the information, so below will be given the code for the partition of the site into several parts of 500 references.

We go to file catalog/controller/extension/feed/google_sitemap.php and change its image to the next code

<?php
class ControllerExtensionFeedGoogleSitemap extends Controller {
    public function index() {
        if ($this->config->get('feed_google_sitemap_status')) {
            if(isset($_GET['manufacturers']) && $_GET['manufacturers']=='active'){
                $output = $this->getManufactureresSiteMaps();
            }elseif (isset($_GET['start'])) {
                $output = $this->getProductsSiteMaps($_GET['start'], $_GET['end']);
            }else{
                
                $output ='<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

                $this->load->model('catalog/product');
                $totalProducts = $this->model_catalog_product->getTotalProducts();
                for($i=0; $i<$totalProducts; $i=$i+500 ){
                    $output .= '<sitemap><loc>'.HTTPS_SERVER.'index.php?route=extension/feed/google_sitemap&amp;start=' . $i . '&amp;end=500</loc><lastmod>2023-02-19T18:23:17+00:00</lastmod></sitemap>';
                }
                $output .= '<sitemap><loc>'.HTTPS_SERVER.'index.php?route=extension/feed/google_sitemap&amp;manufacturers=active</loc><lastmod>2023-02-19T18:23:17+00:00</lastmod></sitemap>';
                $output.='</sitemapindex>';
            }

            $this->response->addHeader('Content-Type: application/xml');
            $this->response->setOutput($output);
        }
    }

    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>daily</changefreq>';
            $output .= '  <priority>0.7</priority>';
            $output .= '</url>';

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

            // $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>daily</changefreq>';
            //     $output .= '  <priority>1.0</priority>';
            //     $output .= '</url>';
            // }

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

        return $output;
    }

    protected function getProductsSiteMaps ($start, $end) {
        $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');

        $filter_data = array(
            'start' => $start,
            'limit' => 500,
        );
        $products = $this->model_catalog_product->getProducts($filter_data);
        foreach ($products as $product) {
            if ($product['image']) {
                $output .= '<url>';
                $output .= '  <loc>' . $this->url->link('product/product', 'product_id=' . $product['product_id']) . '</loc>';
                $output .= '  <changefreq>daily</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>' . (str_replace('&', 'and', $product['name'])) . '</image:caption>';
                $output .= '  <image:title>' . (str_replace('&', 'and', $product['name'])) . '</image:title>';
                $output .= '  </image:image>';
                $output .= '</url>';
            }
        }
        $output .= '</urlset>';

        return $output;
    }

    protected function getManufactureresSiteMaps(){
        $output = '<?xml version="1.0" encoding="UTF-8"?>';
        $output .= '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

        $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>daily</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>daily</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>daily</changefreq>';
            $output .= '  <priority>0.5</priority>';
            $output .= '</url>';
        }
        $output .= '</urlset>';
        return $output;
    }
}

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
The comment language code.