Reading list Switch to dark mode

    Get Parent-Child Categories Hierarchical Tree In Joomla VirtueMart

    Updated 8 January 2019

    Obtaining parent-child hierarchical tree for categories in Joomla VirtueMart front-end could be requirement specific. Though Joomla VirtueMart already provides method to get categroy tree listing but it provides different results depending upon the call being made from the back-end or the front-end environemnt. The method it uses is getCategoryTree() of ‘VirtueMart Category’ Model. This method provides tree structure as per parent child hierarchy of categories at the backend and provides sorted data as per categories’ name when called from the frontend.

    This behavior of function arises a requirement of creating your own method to get parent child hierarchy of categories if required at front end. The code presented will allow you to achieve that.

    /**
    * Webkul Software.
    *
    * @category Webkul
    * @author Webkul
    * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com)
    * @license https://store.webkul.com/license.html
    *
    * return object
    */
    class WebkulModelExample extends JModelLegacy
    {
        public $childCategoriesId = array();
        /**
        * Webkul Software.
        * Method to get all available categories
        *
        * @category Webkul
        * @author Webkul
        * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com)
        * @license https://store.webkul.com/license.html
        *
        * return object
        */
        public function getCategories()
        {
            $categoryData = array();
            $categoryModel=VmModel::getModel('category');
            $categories = $categoryModel->getCategoryTree(0, 0, false);
            foreach ($categories as $category) {
                $categoryData[] = $this->getCategoryData($category->virtuemart_category_id);
            }
            $this->childCategoriesId = array_unique($this->childCategoriesId);
            foreach ($this->childCategoriesId as $childCategoryId) {
                foreach ($categoryData as $catKey => $catValue) {
                    if ($catValue['virtuemartCategoryId'] == $childCategoryId) {
                        unset($categoryData[$catKey]);
                    }
                }
            }
            $categoryData = array_values($categoryData);
            return $categoryData;
        }
    
        /**
         * Webkul Software.
         * Get category data
         * This function calls itself recursively to get all related child category data
         *
         * @category Webkul
         * @author Webkul
         * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com)
         * @license https://store.webkul.com/license.html
         *
         * @param int $categoryId category id
         * 
         * @return array
         */
        public function getCategoryData($categoryId) 
        {
            $categoryModel=VmModel::getModel('category');
            $categoryData = array();
            $tempCategory=$categoryModel->getCategory($categoryId);
    
            //fetch all relevant data required
            $categoryData['virtuemartCategoryId']=$categoryId;
            $categoryData['categoryName']= $tempCategory->category_name;
            $categoryData['children'] = array();
            if ($tempCategory->children != null) {
                foreach ($tempCategory->children as $childCatkey => $childCatValue) {
                    $this->childCategoriesId[] = $childCatValue->virtuemart_category_id;
                    $categoryData['children'][] = $this->getCategoryData($childCatValue->virtuemart_category_id);
                }
            }
            $tempCategory = null;
            return $categoryData;
        }
    }

    Result

    The final output will be as –

    • Product 1
      • Child 1
        • Sub child 1
        • Sub child 2
      • Child 2
    • Product 2
      • Child 1
        • Sub child 1
          • Sub-sub child 1
        • Subchild 2

     

    For any query regarding Joomla VirtueMart plug-ins and add-ons, you can communicate with us by creating a ticket at:
    https://webkul.uvdesk.com/en/customer/create-ticket/

    Start your headless eCommerce
    now.
    Find out More

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    Table of Content