News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Display subcategory products, full depth - VM 3.2.2

Started by bcohen0, August 17, 2017, 22:24:02 PM

Previous topic - Next topic

el02154

Debug Line 461 show_all_subcat_products <pre></pre><br />

GJC Web Design

so u haven't set up your config correctly

search on here how to do it

show_all_subcat_products=1
GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

el02154

I have included already this in /administrator/components/com_virtuemart/virtuemart.cfg (check attached).
I should do something more?

jenkinhill

Did you then save VM configuration twice to ensure the setting is applied?
Kelvyn
Lowestoft, Suffolk, UK

Retired from forum life November 2023

Please mention your VirtueMart, Joomla and PHP versions when asking a question in this forum

el02154

 :) :) :) :) :)

I did right now and all seem ok!!! I did not know I should save twice. Thank you all for helping!

jenkinhill

Kelvyn
Lowestoft, Suffolk, UK

Retired from forum life November 2023

Please mention your VirtueMart, Joomla and PHP versions when asking a question in this forum

wadou3a

if anyone needs an updated version for latest virtuemart, I have modified it to work


/administrator/components/com_virtuemart/models/category.php

     static public function categoryFlatList(&$cats, $catId) {
         $catmodel = VmModel::getModel ('category');
      $childcats = $catmodel->getChildCategoryList(1, $catId,null, null, true);
         foreach($childcats as $k=>$childcat){
            if(!empty($childcat->virtuemart_category_id)){
            $cats .= $childcat->virtuemart_category_id .',';
            self::categoryFlatList($cats, $childcat->virtuemart_category_id);
                  }
               }
    }
   

/administrator/components/com_virtuemart/models/product.php

         if(VmConfig::get('show_subcat_products',false)){
            /*GJC add subcat products*/
            $catmodel = VmModel::getModel ('category');
            $cats = '';
            foreach($virtuemart_category_id as $catId){
               $childcats = $catmodel->getChildCategoryList(1, $catId,null, null, true);
               foreach($childcats as $k=>$childcat){
                  if(!empty($childcat->virtuemart_category_id)){
                     $cats .= $childcat->virtuemart_category_id .',';
                  }
               }
               $cats .= $catId;
            }
                if(!empty($cats)){
                    $joinCategory = TRUE;
                    $where[] = ' `pc`.`virtuemart_category_id` IN ('.$cats.') ';
                }
         } else if (VmConfig::get('show_all_subcat_products',false)) {
            $cats = '';
            foreach($virtuemart_category_id as $catId){
               VmModel::getModel('category')->categoryFlatList($cats, $catId);
               $this->filter_order = '';
               $orderBy='';
               $cats .= $catId;
            }
                if(!empty($cats)){
                    $joinCategory = TRUE;
                    $where[] = ' `pc`.`virtuemart_category_id` IN ('.$cats.') ';
                }
         } else {
            $where[] = ' `pc`.`virtuemart_category_id` IN ('.implode(',',$virtuemart_category_id).') ';
         }




infogate

I can confirm that the above code (from wadou3a)  works normally but you have to be sure that the following option in VM Configuration  and shopfront tab is disabled.

Quoteshow products in subcategories = false

I wonder why this code is not still implemented to official release.

Milbo

Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

razor7

Quote from: Milbo on April 30, 2022, 23:04:46 PM
It is imho part of the core. Please try vm4

Hi Milbo! I'm using VM 4.0.6 and J 3.10.11 but after enabling show_subcat_products I'm still only getting level 2 category products. IE:

Cat Parent <-- clicked this cat link and
  Cat Child 1 <-- got products from here
  Cat Child 2 <-- got products from here
    Cat Grandchild 1 <-- didn't got products
    Cat Grandchild 2 <-- didn't got products
    Cat Grandchild 3 <-- didn't got products
  Cat Child 3 <-- got products from here

Is this feature implemented in VM?

Thanks!
MGS Creativa - VirtueMart Payment Plugin Experts
http://www.mgscreativa.com

Take a look at our downloads section for VirtueMart payment plugins and mouch more!
http://www.mgscreativa.com/en/online-store

sandomatyas

Hello guys. What do you think, could VM work with multi-depth situation?

sandomatyas

I'm not sure if it helps, but if there is a performance concern about fetching categories recursively you might consider some PHP based approach.
I build something like this for a project of mine:

   public static function buildCategoryTree($categories) {
        // Create a mapping of categories by their IDs
        $categoryMap = [];
        foreach ($categories as $category) {
            $categoryId = $category->virtuemart_category_id;
            $categoryMap[$categoryId] = [
                'virtuemart_category_id' => $categoryId,
                'category_parent_id' => $category->category_parent_id,
                'firstLevelChildren' => [],
                'allChildren' => [],
                'isLeaf' => true
            ];
        }

        // Build the tree structure
        foreach ($categoryMap as $categoryId => &$category) {
            $parentId = $category['category_parent_id'];
            if ($parentId !== 0 && isset($categoryMap[$parentId])) {
                $categoryMap[$parentId]['firstLevelChildren'][] = $categoryId;
                $categoryMap[$parentId]['isLeaf'] = false;
            }
        }

        // Populate allChildren array recursively
        foreach ($categoryMap as $categoryId => &$category) {
            $category['allChildren'] = self::getAllChildren($categoryMap, $categoryId);
        }

        // Return the result as array with virtuemart_category_id as keys
        return $categoryMap;
    }

    private static function getAllChildren(&$categoryMap, $categoryId) {
        $children = [];
        foreach ($categoryMap as $id => &$category) {
            if ($category['category_parent_id'] === $categoryId) {
                $children[] = $id;
                $children = array_merge($children, self::getAllChildren($categoryMap, $id));
            }
        }
        return $children;
    }

$db = JFactory::getDBO();

$query = $db->getQuery(true);
$query->select('*')
      ->from('#__virtuemart_categories');
$db->setQuery($query);
$categories = $db->loadObjectList();
$categoriesWithChildren = self::buildCategoryTree($categories);

print_r($categoriesWithChildren);

It fetches all of the categories with a single query, and builds all of it's subcategories using only PHP.
In my test shop $categoriesWithChildren[$virtuemart_category_id] gives something like this:
    [68] => Array
        (
            [virtuemart_category_id] => 68
            [category_parent_id] => 36
            [firstLevelChildren] => Array
                (
                    [0] => 221
                    [1] => 222
                )

            [allChildren] => Array
                (
                    [0] => 221
                    [1] => 222
                    [2] => 442
                    [3] => 1109
                    [4] => 1110
                    [5] => 1281
                    [6] => 1299
                )

            [isLeaf] => false
        )

Where firstLevelChildren is the ids of the child categories, allChildren is the ids of all children recursively. Also it's cacheable.
I think it might be implemented to sortSearchListQuery using a simple implode to allChildren

sandomatyas

I created a dev site for that, it does work with a modified sortSearchListQuery function in a shop which uses >1000 categories in 4 level. For a top level category it collects 650 child category ids from multiple levels in few milliseconds