Так получилось, что sitemap.xml у OpenCart это слабое место. Его расширение по умолчанию, при каждом запросе генерирует эту карту сайта дергая базу данных, при том что xml это просто статика, для которой дергать php и не нужно, ну а если у вас тысячи товаров, как на моем демо магазине (>2500), то карта сайта даже никогда не отработает.
Я сильно не стал выдумывать решение, все достаточно просто, нужно просто карту сайта сгенерировать в корень и чтобы nginx её отдавал как обычный статический файл. Для этого я скопировал старое расширение с новым именем и добавил просто вывод в файл в корень сайта.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
<?php // catalog/controller/extension/feed/google_sitemap_generate.php class ControllerExtensionFeedGoogleSitemapGenerate 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>' . htmlentities($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>' . htmlentities($product['name']) . '</image:caption>'; $output .= ' <image:title>' . htmlentities($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>' . htmlentities($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>' . htmlentities($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>' . htmlentities($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); $xml = simplexml_load_string($output); $xml->asXML(DIR_APPLICATION . '../sitemap.xml'); } } 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; } } |
Так же обратите внимание на строки
1 2 |
$output .= ' <image:caption>' . htmlentities($product['name']) . '</image:caption>'; $output .= ' <image:title>' . htmlentities($product['name']) . '</image:title>'; |
Изначально они не экранируются, карта сайта собирается строкой а не объектом, и если в названии есть амперсанды, то ваша карта сайта станет сразу невалидной.
Дальше я добавил консольный скриптик и поставил его на крон, для запуска контроллера и генерации карты сайта раз в сутки, чего вполне на мой взгляд достаточно.
PS. Не забываем с .htaccess убрать строку, которая по адресу /sitemap.xml запускает выполнение контроллера.
Здравствуйте! Я совсем новичок в opencart
Подскажите пожалуйста
«»»»»Дальше я добавил консольный скриптик и поставил его на крон, для запуска контроллера и генерации карты сайта раз в сутки, чего вполне на мой взгляд достаточно.
PS. Не забываем с .htaccess убрать строку, которая по адресу /sitemap.xml запускает выполнение контроллера.»»»»
как добавить консольный скрипт и как его поставит на крон?
Спасибо!
В опенкарт простого механизма запуска с консоли нет к сожалению, по этому генерацию надо по умолчанию запустить как то по другому, например вызова с браузера данного контроллера.
Могу расписать как работать с консолью в следующей статье
Подскажите решение сохранения к примеру такой комбинации
Сайт мультиязычный:
https://site.com/index.php?route=extension/feed/google_sitemap — сохраняет в корень — sitemap.xml это есть..
а как добавить
https://site.com/ru/index.php?route=extension/feed/google_sitemap — сохраняем — sitemap_ru.xml
https://site.com/en/index.php?route=extension/feed/google_sitemap — сохраняем — sitemap_en.xml
Как добиться понимания языка…
Он же сохраняет все товары и категории не зависимо от языка. А так дописать что то свое, которое будет генерировать разные файлы с какой то логикой попадания туда линков. Все зависит от того, что вы пытаетесь решить, пока мне кажется что вы делаете что то не то.