Page 1 of 1

How to get category path for a product

Posted: Sat Mar 23, 2019 12:27 am
by psiktis
Hi all!

I am trying to get the full category path for each product but I am having a hard time getting it to work.

The following code returns the cattegory id path correctly (from what I have seen) but can't seem to be able to modify it so it returns the name of the category instead of the id.

Can anyone help?

Code: Select all

    public function getProductCategoryPath($product_id) {//nick
            $query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "' LIMIT 1");
            if($query->row['category_id']){
                // Parent category
                $path2 = $query->row['category_id'];
                // Category parent
                $query = $this->db->query("SELECT parent_id FROM " . DB_PREFIX . "category WHERE category_id = '" . $query->row['category_id'] . "' AND parent_id != 0");
                while($query->num_rows){
                    $path2 = $query->row['parent_id'] . "_" . $path2;
                    // Sub-category parent
                    $query = $this->db->query("SELECT parent_id FROM " . DB_PREFIX . "category WHERE category_id = '" . $query->row['parent_id'] . "' AND parent_id != 0");
                }
                return $path2;
            }
            return false;
        } 


Re: How to get category path for a product

Posted: Sat Mar 23, 2019 10:58 am
by IP_CAM
Well, you seem to be digging in the wrong Mine, better get one of
the popular SEO Extensions, to make such work. Like, by example:
(autogenerated random product titles without any meaning!)

Code: Select all

../Tablets/Canon/1596-splendide-understand-p322c57.html
../MP3-Players/test-20/test-25/Sony/Erant-time-saperet-detraxit-p278c34c52c58.html
../Components/Web-Cameras-c25c32.html
../Components/Web-Cameras/HTC/Alus-1963-p42c25c32.html
as you can see here in action: (Likely, if I did not screw up momentarely...)
http://www.velomech.ch/shop/
Ernie

Re: How to get category path for a product

Posted: Sat Mar 23, 2019 11:11 pm
by psiktis
IP_CAM wrote:
Sat Mar 23, 2019 10:58 am
Well, you seem to be digging in the wrong Mine, better get one of
the popular SEO Extensions, to make such work.
Sorry if I didn't myself clear but this is not what I am trying to do.

The goal is to get the category path as a string which has absolutely nothing to do with SEO.

For example, for an AMD Ryzen CPU I want the resulting string to be "PC -> Components -> CPUs -> AMD".

The code above does the exact thing I want but it uses the category_id from the database instead of the category's name.