VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: duncan12 on October 09, 2015, 23:41:23 PM

Title: Sort by Category is Useless - Bug?
Post by: duncan12 on October 09, 2015, 23:41:23 PM
Why is there no checkbox in the menu settings for Category Layout to show products from subcategories?

I have a menu link to a VirtueMart category layout. I have no products in this category. But there are subcategories and I have products in there. But the page shows no products.

By the way, in the VirtueMart back end under Configuration => Product Order Settings, there is an option to sort by Category Name. But, it doesn't do anything as far as I can tell because the Category Layout view does not even show products from multiple categories.
Title: Re: Sort by Category is Useless - Bug?
Post by: GJC Web Design on October 09, 2015, 23:53:18 PM
Correct - afaik the std. VM doesn't show products from subcats in a cat view

I always use this hack in models/product.php ~ line 330 in the function sortSearchListQuery()

if ($virtuemart_category_id > 0) {
/*GJC add subcat products*/

//$joinCategory = TRUE;
//$where[] = ' `pc`.`virtuemart_category_id` = ' . $virtuemart_category_id;
$catmodel = VmModel::getModel ('category');
$childcats = $catmodel->getChildCategoryList(1, $virtuemart_category_id,null, null, true);
$cats = $virtuemart_category_id;
foreach($childcats as $childcat){
$cats .= ','.$childcat->virtuemart_category_id;
}
$joinCategory = TRUE;
$where[] = ' `pc`.`virtuemart_category_id` IN ('.$cats.') ';
/*GJC add subcat products*/

} else if ($isSite) {
if (!VmConfig::get('show_uncat_parent_products',TRUE)) {
$joinCategory = TRUE;
$where[] = ' ((p.`product_parent_id` = "0" AND `pc`.`virtuemart_category_id` > "0") OR p.`product_parent_id` > "0") ';
}
if (!VmConfig::get('show_uncat_child_products',TRUE)) {
$joinCategory = TRUE;
$where[] = ' ((p.`product_parent_id` > "0" AND `pc`.`virtuemart_category_id` > "0") OR p.`product_parent_id` = "0") ';
}
}


There's probably better ways -- Max once mentioned a vmsearch plugin called at line 632

if ($this->searchplugin !== 0) {
JPluginHelper::importPlugin('vmcustom');
$dispatcher = JDispatcher::getInstance();
$dispatcher->trigger('plgVmBeforeProductSearch', array(&$select, &$joinedTables, &$where, &$groupBy, &$orderBy,&$joinLang));
}


will try one day if I ever find time
Title: Re: Sort by Category is Useless - Bug?
Post by: Mx150 on October 14, 2015, 18:58:25 PM
GREAT  :o  GJC Web Design perfekt!

Exactly what i need... big thanks to duncan12 aswell for sending me the Link to the post.
Title: Re: Sort by Category is Useless - Bug?
Post by: Milbo on October 14, 2015, 22:53:27 PM
Added to the model, but without option, just with if(true), so easier to change it, option will come.
Title: Re: Sort by Category is Useless - Bug?
Post by: wicko on December 04, 2015, 16:54:31 PM
Adding the code by GJC Web Design is great and shows the products in the first level subcategory. But if you have for instance a group category in your store then a sub group within that then the products will not be viewed in the first level store category. 

Quote from: Milbo on October 14, 2015, 22:53:27 PM
Added to the model, but without option, just with if(true), so easier to change it, option will come.


When you look into this option for future releases I guess you might want the option of how many subcategory levels you look into.
Title: Re: Sort by Category is Useless - Bug?
Post by: GJC Web Design on December 04, 2015, 17:28:08 PM
what you would need to do is then run the function $catmodel->getChildCategoryList again within the loop for each returned child cat

could be very heavy on queries
Title: Re: Sort by Category is Useless - Bug?
Post by: wicko on December 04, 2015, 17:51:23 PM
Yes you are right. If too many loops are created then it will only slow things down. Which I guess is why I think you would need to set a level of how far you want to go.
Title: Re: Sort by Category is Useless - Bug?
Post by: raydekker on February 23, 2016, 05:02:01 AM
Thank you so much GJC Webdesign!! Works as a charm!
Just spend 5 hours modifying php's etc.
What I found a great addition to this is a random image for each category!
It uses an image from a product in the category (and subcategory thanks to GJC) as a category image...
So I just use CSVI Pro to import my products + category tree + product images and voila, done!

copy components/com_virtuemart/sublayouts/categories.php to templates/[yourtemplate]/html/com_virtuemart/sublayouts/categories.php
edit the file and replace:echo $category->images[0]->displayMediaThumb("",false); with:
$productModel = VmModel::getModel('product');
$prod_in_category = $productModel->getProductListing(false, 1, false, true, true, true, $category->virtuemart_category_id);
$sel = array_rand($prod_in_category);
$productModel->addImages($prod_in_category[0],1);
if(!empty($prod_in_category[0]->images[0])){
    echo $prod_in_category[0]->images[0]->displayMediaThumb("",false);
} else {
    echo $category->images[0]->displayMediaThumb("",false);
}


Or with the following to just display a product image (not random = less memory usage):

$productModel = VmModel::getModel('product');
$prod_in_category = $productModel->getProductListing(false, 1, false, true, true, true, $category->virtuemart_category_id);
$productModel->addImages($prod_in_category[0],1);
if(!empty($prod_in_category[0]->images[0])){
    echo $prod_in_category[0]->images[0]->displayMediaThumb("",false);
} else {
    echo $category->images[0]->displayMediaThumb("",false);
}


Title: Re: Sort by Category is Useless - Bug?
Post by: wicko on April 06, 2016, 15:11:41 PM
My products are not showing in parent category any more. Has this been replaced in VM update? I have this as template override but are not working anymore.
Title: Re: Sort by Category is Useless - Bug?
Post by: Rune Rasmussen on May 30, 2016, 16:30:23 PM
Quote from: wicko on December 04, 2015, 16:54:31 PM
Adding the code by GJC Web Design is great and shows the products in the first level subcategory.
But only for the first sub category, not from several second level categories ...

T.ex. if having the following structure, it will only display products from "Black" if you are viewing "Coffee":
Coffee (first level)
- Black (second level)
- Espresso (second level)
- Various (second level)

Right? So I prefer the old solution over at http://forum.virtuemart.net/index.php?topic=97874.msg346289#msg346289

And yes @wicko, core hacks are removed on updates as the file is replaced with the original from latest pack. :)
Title: Re: Sort by Category is Useless - Bug?
Post by: doc_denis on June 04, 2016, 10:33:41 AM
hello, this is exactly what I wanted to do.
Thank you for sharing GJC Web Design !

@milbo, I think this should be native with a button in admin (view product of subcategories on category : yes / no)
VM chosen himself as the first category is a category by default, and it is disadvantageous when exporting to Google Shopping or market place.
Since we can not define the default category of a product, this solution is a good alternative, leaving the product in subcategory

the seo url change (its really different)  ...my french example  ;) :
before : /wine/mychampagnebootle.html
after : /wine/champagne/mychampagnebottle.html
and the product mychampagnebottle.html is visible on /wine.html   good !

thank you for everything
Denis, French user.
Title: Re: Sort by Category is Useless - Bug?
Post by: sandomatyas on June 23, 2016, 16:49:24 PM
Maybe in 3.0.18? ;)
Title: Re: Sort by Category is Useless - Bug?
Post by: panoss on January 21, 2017, 14:13:35 PM
Quote from: Milbo on October 14, 2015, 22:53:27 PM
Added to the model, but without option, just with if(true), so easier to change it, option will come.

I have VM 3.0.18.
I want products from children categories of the current category, to be shown.
e.g: current category bikes, with sub categories road bikes and mountain bikes:
bikes
--road bikes (with products: rb1, rb2)
--mountain bikes (with products: mb1, mb2)

I want this as result:
rb1 rb2 mb1 mb2
Is there some option I should check?
Title: Re: Sort by Category is Useless - Bug?
Post by: GJC Web Design on January 21, 2017, 22:47:11 PM
it isn't a config option .. you need to change the true to a false in

if(true){
            $where[] = ' `pc`.`virtuemart_category_id` = ' . $virtuemart_category_id;
         } else {

line 369 ->  administrator\components\com_virtuemart\models\product.php
Title: Re: Sort by Category is Useless - Bug?
Post by: panoss on January 22, 2017, 10:03:53 AM
Thanks it works fine!
Title: Re: Sort by Category is Useless - Bug?
Post by: hegbi on April 04, 2017, 17:14:41 PM
thanks GJC
how to do the same in VM 3.2.1 (J3.6.5)?
Title: Re: Sort by Category is Useless - Bug?
Post by: Rune Rasmussen on April 04, 2017, 17:39:53 PM
By checking the option in the configuration, I assume.
Configuration -> Shopfront -> Product Listing -> Show products of subcategories
Title: Re: Sort by Category is Useless - Bug?
Post by: hegbi on April 04, 2017, 18:11:33 PM
thanks Rune, tried that already but can't get it to work.
could be that my template is preventing this somehow...
Title: Re: Sort by Category is Useless - Bug?
Post by: Rune Rasmussen on April 04, 2017, 18:27:38 PM
Test with default template, and also remember to clear any cache.
So fare I have only tested it in the Zeus template from OlympianThemes, and there it worked.
Title: Re: Sort by Category is Useless - Bug?
Post by: Milbo on April 05, 2017, 09:38:56 AM
Just to make clear. It is template independent.
Title: Re: Sort by Category is Useless - Bug?
Post by: Rune Rasmussen on April 05, 2017, 10:25:14 AM
You're of cource right, it's how it should be. But I've seen quite many bad templates through the years, with original code and structure totaly replaced, so I wouldn't bet on it. T.ex. if I recall right, I've seen some removing product display when there is sub categories. So better be on the safe side and test default.
Title: Re: Sort by Category is Useless - Bug?
Post by: hegbi on April 05, 2017, 17:07:40 PM
works like a charm!!!  ;)
my mistake, had some old product.php file in models folder
after replacing that file from fresh VM download I can confirm that child products are shown in main category as well in VM3.2.1/J3.6.5 (and enabling Configuration -> Shopfront -> Product Listing -> Show products of subcategories)

thank you Rune, Milbo and GJC!!
Title: Re: Sort by Category is Useless - Bug?
Post by: sandomatyas on April 06, 2017, 10:14:27 AM
but it loads the products only from the first level of subcategories.
Like
CatLevel1
--CatLevel2-1
-----CatLevel3-1
-----CatLevel3-2
--CatLevel2-2
-----CatLevel3-3

If I have products in CatLevel3 categories CatLevel1 won't display any of them
Title: Re: Sort by Category is Useless - Bug?
Post by: hegbi on April 11, 2017, 02:22:17 AM
thanks to http://forum.virtuemart.net/index.php?topic=97874.0 (http://forum.virtuemart.net/index.php?topic=97874.0)
backup your product.php in \administrator\components\com_virtuemart\models\ and then replace with my attached version any try in order to show all levels...