Настройка ЧПУ в OpenCart

August 18, 2015 02:54    OpenCartчпуphp


1. В корне лежит файл .htaccess.txt, который нужно переименовать в .htaccess

2. В админке, в разделе System > Settings заходим в редактирование настроек магазина. Во вкладке Server будет настройка Use SEO URLs.

3. Системные страницы вроде главной, корзины и др все равно будут урловаться как говно. Необходимо открыть файл \catalog\controller\common\seo_url.php

Найти:

if (isset($data['route'])) {
	if (($data['route'] == 'product/product' && $key == 'product_id') || (($data['route'] == 'product/manufacturer/info' || $data['route'] == 'product/product') && $key == 'manufacturer_id') || ($data['route'] == 'information/information' && $key == 'information_id')) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");
 
		if ($query->num_rows && $query->row['keyword']) {
			$url .= '/' . $query->row['keyword'];
 
			unset($data[$key]);
		}
	} elseif ($key == 'path') {
		$categories = explode('_', $value);
 
		foreach ($categories as $category) {
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_id=" . (int)$category . "'");
 
			if ($query->num_rows && $query->row['keyword']) {
				$url .= '/' . $query->row['keyword'];
			} else {
				$url = '';
 
				break;
			}
		}
 
		unset($data[$key]);
	}
}

И добавить условие, чтобы получился следующий код:

if (isset($data['route'])) {
	if (($data['route'] == 'product/product' && $key == 'product_id') || (($data['route'] == 'product/manufacturer/info' || $data['route'] == 'product/product') && $key == 'manufacturer_id') || ($data['route'] == 'information/information' && $key == 'information_id')) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");
 
		if ($query->num_rows && $query->row['keyword']) {
			$url .= '/' . $query->row['keyword'];
 
			unset($data[$key]);
		}
	} elseif ($key == 'path') {
		$categories = explode('_', $value);
 
		foreach ($categories as $category) {
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_id=" . (int)$category . "'");
 
			if ($query->num_rows && $query->row['keyword']) {
				$url .= '/' . $query->row['keyword'];
			} else {
				$url = '';
 
				break;
			}
		}
 
		unset($data[$key]);
	}else{
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($value) . "'");
		if (($query->num_rows && $query->row['keyword']) or $value == 'common/home') {
			$url .= '/' . $query->row['keyword'];
 
			unset($data[$key]);
		}
 
	}
}

Затем в базе выполняем эти запросы для добавления урлов:

INSERT INTO oc_url_alias (query, keyword) VALUES ('common/home', '');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/wishlist', 'wishlist');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/account', 'my-account');
INSERT INTO oc_url_alias (query, keyword) VALUES ('checkout/cart', 'shopping-cart');
INSERT INTO oc_url_alias (query, keyword) VALUES ('checkout/checkout', 'checkout');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/login', 'login');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/logout', 'logout');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/order', 'order-history');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/newsletter', 'newsletter');
INSERT INTO oc_url_alias (query, keyword) VALUES ('product/special', 'specials');
INSERT INTO oc_url_alias (query, keyword) VALUES ('affiliate/account', 'affiliates');
INSERT INTO oc_url_alias (query, keyword) VALUES ('checkout/voucher', 'gift-vouchers');
INSERT INTO oc_url_alias (query, keyword) VALUES ('product/manufacturer', 'brands');
INSERT INTO oc_url_alias (query, keyword) VALUES ('information/contact', 'contact-us');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/return/insert', 'request-return');
INSERT INTO oc_url_alias (query, keyword) VALUES ('information/sitemap', 'sitemap');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/forgotten', 'forgot-password');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/download', 'downloads');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/return', 'returns');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/transaction', 'transactions');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/register', 'create-account');
INSERT INTO oc_url_alias (query, keyword) VALUES ('product/compare', 'compare-products');
INSERT INTO oc_url_alias (query, keyword) VALUES ('product/search', 'search');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/edit', 'edit-account');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/password', 'change-password');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/address', 'address-book');
INSERT INTO oc_url_alias (query, keyword) VALUES ('account/reward', 'reward-points');
INSERT INTO oc_url_alias (query, keyword) VALUES ('affiliate/edit', 'edit-affiliate-account');
INSERT INTO oc_url_alias (query, keyword) VALUES ('affiliate/password', 'change-affiliate-password');
INSERT INTO oc_url_alias (query, keyword) VALUES ('affiliate/payment', 'affiliate-payment-options');
INSERT INTO oc_url_alias (query, keyword) VALUES ('affiliate/tracking', 'affiliate-tracking-code');
INSERT INTO oc_url_alias (query, keyword) VALUES ('affiliate/transaction', 'affiliate-transactions');
INSERT INTO oc_url_alias (query, keyword) VALUES ('affiliate/logout', 'affiliate-logout');
INSERT INTO oc_url_alias (query, keyword) VALUES ('affiliate/forgotten', 'affiliate-forgot-password');
INSERT INTO oc_url_alias (query, keyword) VALUES ('affiliate/register', 'create-affiliate-account');
INSERT INTO oc_url_alias (query, keyword) VALUES ('affiliate/login', 'affiliate-login');

4. Очистить кеш в OpenCart — system/cache