One of our clients had products around 50k because of which the sitemap could not load and always throw 500 errors as Opencart tried to load all the products' sitemap URLs on one page. So, our solution is to show the sitemap URLs in a chunk of 500 and use the sitemap index.
Solution:
Although a single sitemap limit is 50MB or 50k URLs, mostly the hosting providers or servers cannot handle showing all of those 50k URLs on one page so our solution is to create chunks to 500 URLs as one sitemap and list all sitemaps using sitemapindex and submit this one sitemap which includes the sitemapindex.
Here is an example of sitemapindex:
https://webocreation.com.com/index.php?route=extension/feed/google_sitemap&start=0&end=500
2023-02-19T18:23:17+00:00
https://webocreation.com.com/index.php?route=extension/feed/google_sitemap&start=500&end=500
2023-02-19T18:23:17+00:00
https://webocreation.com.com/index.php?route=extension/feed/google_sitemap&start=1000&end=500
2023-02-19T18:23:17+00:00
https://webocreation.com.com/index.php?route=extension/feed/google_sitemap&start=1500&end=500
2023-02-19T18:23:17+00:00
...
...
...
When you see one of the sitemap URLs, for example, this URL https://webocreation.com.com/index.php?route=extension/feed/google_sitemap&start=0&end=500 then you will see the sitemap code like below of 500 products:
https://webocreation.com.com/walnut
daily
2023-02-20T00:00:00+00:00
1.0
https://webocreation.com.com/image/cache/catalog/data/GKOF20SVE-1000x1000.jpg
Walnuts
Walnuts
https://webocreation.com.com/insurances
daily
2023-02-20T00:00:00+00:00
1.0
https://webocreation.com.com/image/cache/catalog/data/GPT2A45410-1000x1000.jpg
Insurances
Insurances
...
...
...
Code changed for the Google sitemap extension
Open file catalog/controller/extension/feed/google_sitemap.php, remove all code, and paste the following:
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 ='';
$this->load->model('catalog/product');
$totalProducts = $this->model_catalog_product->getTotalProducts();
for($i=0; $i<$totalProducts; $i=$i+500 ){
$output .= ''.HTTPS_SERVER.'index.php?route=extension/feed/google_sitemap&start=' . $i . '&end=500 2023-02-19T18:23:17+00:00 ';
}
$output .= ''.HTTPS_SERVER.'index.php?route=extension/feed/google_sitemap&manufacturers=active 2023-02-19T18:23:17+00:00 ';
$output.=' ';
}
$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 .= '';
$output .= ' ' . $this->url->link('product/category', 'path=' . $new_path) . ' ';
$output .= ' daily ';
$output .= ' 0.7 ';
$output .= ' ';
// $this->load->model('catalog/product');
// $products = $this->model_catalog_product->getProducts(array('filter_category_id' => $result['category_id']));
// foreach ($products as $product) {
// $output .= '';
// $output .= ' ' . $this->url->link('product/product', 'path=' . $new_path . '&product_id=' . $product['product_id']) . ' ';
// $output .= ' daily ';
// $output .= ' 1.0 ';
// $output .= ' ';
// }
$output .= $this->getCategories($result['category_id'], $new_path);
}
return $output;
}
protected function getProductsSiteMaps ($start, $end) {
$output = '';
$output .= '';
$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 .= '';
$output .= ' ' . $this->url->link('product/product', 'product_id=' . $product['product_id']) . ' ';
$output .= ' daily ';
$output .= ' ' . date('Y-m-d\TH:i:sP', strtotime($product['date_modified'])) . ' ';
$output .= ' 1.0 ';
$output .= ' ';
$output .= ' ' . $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')) . ' ';
$output .= ' ' . (str_replace('&', 'and', $product['name'])) . ' ';
$output .= ' ' . (str_replace('&', 'and', $product['name'])) . ' ';
$output .= ' ';
$output .= ' ';
}
}
$output .= ' ';
return $output;
}
protected function getManufactureresSiteMaps(){
$output = '';
$output .= '';
$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 .= '';
$output .= ' ' . $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $manufacturer['manufacturer_id']) . ' ';
$output .= ' daily ';
$output .= ' 0.7 ';
$output .= ' ';
//$products = $this->model_catalog_product->getProducts(array('filter_manufacturer_id' => $manufacturer['manufacturer_id']));
// foreach ($products as $product) {
// $output .= '';
// $output .= ' ' . $this->url->link('product/product', 'manufacturer_id=' . $manufacturer['manufacturer_id'] . '&product_id=' . $product['product_id']) . ' ';
// $output .= ' daily ';
// $output .= ' 1.0 ';
// $output .= ' ';
// }
}
$this->load->model('catalog/information');
$informations = $this->model_catalog_information->getInformations();
foreach ($informations as $information) {
$output .= '';
$output .= ' ' . $this->url->link('information/information', 'information_id=' . $information['information_id']) . ' ';
$output .= ' daily ';
$output .= ' 0.5 ';
$output .= ' ';
}
$output .= ' ';
return $output;
}
}
This is the rough way to do it for now but no worries we will soon create an extension for it and provide you guys, for now, this is the quick way to achieve it.
In this way, you can submit the sitemap for a large number of products and submit it to google, bing, or other search engines. Please let us know if you have any kind of projects, you can email us at webocreation.com@gmail.com. Hope you liked this tutorial, please subscribe to our YouTube Channel and get more Opencart free extensions. You can also find us on Twitter and Facebook.