Ad

Open Cart SEO URL For Category/manufacturer

- 1 answer

OC 1.5.6

I've installed a module to filter products in categories based on manufacturer.

I'm able to reach every "search result page" at an url like:

example.com/index.php?route=product/category&path=X&manufacturer=Y

I'd like to create a SEO url like:

example.com/alias-for-category/alias-for-manufacturer

(ex. /bikes/honda or /cars/bmw)

but I can't figure out how to do it.

I've tried for testing and simplifying to convert just a single url:

product/category&path=1&manufacturer=1

so I added to url_alias a record with this query and keyword

cat1-man1

then I modified catalog/common/seo_url.php

function Index

if (isset($this->request->get['product_id'])) {
            $this->request->get['route'] = 'product/product';
        } elseif (isset($this->request->get['path'])) {
            $this->request->get['route'] = 'product/category';
        } elseif (isset($this->request->get['manufacturer_id'])) {
            $this->request->get['route'] = 'product/manufacturer/info';
        } elseif (isset($this->request->get['information_id'])) {
            $this->request->get['route'] = 'information/information';
        } 
        //ADDED BELOW CODE TO ROUTE ALL URL
        else {
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($this->request->get['_route_']) . "'");
            if ($query->num_rows) {
                $this->request->get['route'] = $query->row['query'];
            }
        }

and function rewrite

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) {
                    $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) {
                        $url .= '/' . $query->row['keyword'];
                    }                           
                }

                unset($data[$key]);
            }  
            //ADDED CODE BELOW TO CONVERT ALL URL
            else {
                    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($data['route']) . "'");

                    if ($query->num_rows) {
                        $url .= '/' . $query->row['keyword'];

                        unset($data[$key]);
                    }
            }

but also this simplified version doesn't work at all, page not found is shown.

What I'm doing wrong? Any tip will be really appreciated

Ad

Answer

First of all the custom module I've installed expects the parameter for manufacturer as "manufacturer" instead of "manufacturer_id" so a converted URL like :

example.com/index.php?route=product/category&path=X&manufacturer_id=Y

isn't handled correctly by the module.

So the answer to my question is : OC does already convert alias-cat/alias-manufacturer to a correct internal URL

On the other hand OC does not convert URL of the type

example.com/index.php?route=product/category&path=X&manufacturer_id=Y

into SEO friendly URL like:

example.com/alias-for-category/alias-for-manufacturer

in the menus or links, to do this, modify catalog/common/seo_url.php like this:

public function rewrite($link) {
    $url_info = parse_url(str_replace('&', '&', $link));

    $url = ''; 

    $data = array();

    parse_str($url_info['query'], $data);

    foreach ($data as $key => $value) {
        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) {
                    $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) {
                        $url .= '/' . $query->row['keyword'];
                    }                           
                }

                unset($data[$key]);
            }  
            // ADD THIS CODE
            elseif ($key == 'manufacturer_id') {

                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'manufacturer_id=" . (int)$value . "'");

                if ($query->num_rows) {
                    $url .= '/' . $query->row['keyword'];
                }                           

                unset($data[$key]);
            } 
            // END ADDED CODE----------------
            else {
                   .....
Ad
source: stackoverflow.com
Ad