Как отключить минификацию в Google Sitemap для Opencart?

Ссылка скопирована
PHP
9 февраля 2026 1 ответ

Модуль гугла делает sitemap в одну строку, а это очень неудобно, когда Яндекс Вебмастер находит ошибки и указывает "строка 1", где весь sitemap. Настроек никаких у модуля нет, есть просто его код.
spoiler

<?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-dTH: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'); 			$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>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; 	} }

<?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-dTH: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'); $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>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; } }

Дополнительно:

Замените везде $output .= '...'; на $output .= PHP_EOL . '...';

  • Не работает, всё равно код генерируется в одну строку
  • Alexander Kovalenko, а вы через арбузер открывате и смотрите? Код запускаете в каком окружении?
  • Это возможно в ряде случаев:
    • XML генерирует что-то другое и вы не там редактируете.
    • Сгенерированный файл кэшируется и вы получили старый результат.
    • Что-то другое вырезает обратно переносы строк (маловероятно).
  • Алексей Уколов, да, в кеше дело было, почему то он не сразу сбросился. Спасибо за совет!
Нужно решить такую задачу?

Опишите проблему, и специалист поможет с настройкой, исправлением ошибки или доработкой сайта. Подберём понятный план работ без лишней переписки.

Заказать помощь
Лучший ответ
1
Максим Павлов Ответ

Для отключения минификации в Google Sitemap для Opencart, вам потребуется внести небольшие изменения в код. Вам нужно найти файл sitemap.php, который обычно находится в папке catalog/controller/feed/.

Откройте этот файл с помощью любого текстового редактора и найдите следующий участок кода:

```php
$output .= 'daily' . "

Другие ответы (0)

Пока нет других ответов. Будьте первым, кто поможет автору.

Ответить на вопрос

комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Вам также может быть интересно