VirtueMart Forum

VirtueMart 2 + 3 + 4 => Product pricing => Topic started by: mogaka on October 28, 2016, 18:12:14 PM

Title: Shows all products from subcategories in parent categories too
Post by: mogaka on October 28, 2016, 18:12:14 PM
Virtuemart: 3.0.16
I would like all products in a sub category to be displayed in the parent category too using php. I know that there is the extension 'Virtuemart autorelations' that does this but I have disabled it because it causes an error on vmvendor.
.
Example:
General [Parent Category]
-- Food [Subcategory]
---- Fruits [Sub-subcategory]
---- Vegetables [Sub-subcategory]
-- Furniture [Subcategory]
---- Tables [Sub-subcategory]
---- Chairs [Sub-subcategory]

I would like all products under the sub-subcategories 'Fruits' and 'vegetables' to also appear under the sub-category 'Food' and similarly all products under the sub-subcategories 'Tables' and 'Chairs' to also appear under the sub-category 'Furniture' and then all the products form all the sub-subcategories to appear under the parent category 'General'
Title: Re: Shows all products from subcategories in parent categories too
Post by: valekichia on December 06, 2016, 16:07:16 PM
I need the same thing and didn't find anything about it.  :'(
To me it seems a quite logical behavior. I don't know why there isn't a native option for this...
I know that you can assign the product to both the parent and the sub category, but I want to avoid this.
Nobody has some hints for us?

Thank you!
Title: Re: Shows all products from subcategories in parent categories too
Post by: rage76 on December 06, 2016, 18:06:15 PM
Hi, if I am getting you correctly, please try VM Auto Parent Categories Plugin

Title: Re: Shows all products from subcategories in parent categories too
Post by: GJC Web Design on December 06, 2016, 19:31:41 PM
this has been available in VM3 since I think 3.0.9 but it requires a small code change.. I think the intention was to make a config for it

but look around line 370 of  \administrator\components\com_virtuemart\models\product.php


if ($virtuemart_category_id > 0) {
$joinCategory = TRUE;
if(true){
$where[] = ' `pc`.`virtuemart_category_id` = ' . $virtuemart_category_id;
} else {
/*GJC add subcat products*/
$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.') ';
}
} else


if u change   

if(true){

to

if(false){

then all the products in the parent and subcats will be returned
Title: Re: Shows all products from subcategories in parent categories too
Post by: valekichia on December 07, 2016, 11:41:08 AM
Thank you to both of you! :)

---

@rage76
Thank you for the suggestion. I've had already tried VM Auto Parent Categories Plugin, but basically what it does is to assign the product to both the main category and the subcategory.
This can be enough, but I want to avoid it for 2 reasons.

1) It creates problems with the breadcrumbs.

Let's say you have "Product A" that belongs to "Subcategory1" inside "CATEGORY1".
If you click on it from the products list in the subcategory page, the product's detail page will have the correct path, that is:
(root) > CATEGORY 1 > Subcategory 1 > Product A
But if you click on the product from the main category view, then you will get an incomplete path that doesn't include the subcategory. It will loook like this:
(root) > CATEGORY 1 > Product A

I think that for the user this will be more confusing than helpful.

2) It means that each product will have 2 URLs, which I think it is bad for SEO.

In the two cases above, you will have these URLs:
(siteroot)/category1/producta-detail.html
(siteroot)/category1/subcategory1/producta-detail.html

I think these are sufficient reasons for deciding not to use it, but please correct if I'm wrong!

---

@GJC Web Design

I tested your suggestion and it works. Great!

But this is a core hack, so it will be lost with the next update, right?

Another question. Is there a way to achieve the same thing in Virtuemart Products Module?
It should work in the same way, otherwise if it is set to recall products from the main category it will be empty!
Title: Re: Shows all products from subcategories in parent categories too
Post by: GJC Web Design on December 07, 2016, 11:54:28 AM
QuoteBut this is a core hack, so it will be lost with the next update, right?

this code has been there for some time now .. I assume Max's intention was/is to have a config for this ..

although changing one string on an update shouldn't be too much hassle

re prods mod.. you need to check what it's querying (switch off SEF) - I would have assumed it is the same query
Title: Re: Shows all products from subcategories in parent categories too
Post by: rage76 on December 07, 2016, 14:42:15 PM
@valekichia

wow, you are very correct about the VM Auto Parent Categories Plugin

I did not notice (read bothered) this earlier. can we change something in the plugin itself to avoid the different URLs?

on the other hand, now that I have used this plugin, can I revert the changes made by this plugin so that I opt for the core hack ??
Title: Re: Shows all products from subcategories in parent categories too
Post by: GJC Web Design on December 07, 2016, 23:32:13 PM
Quotecan I revert the changes made by this plugin

I assume it only makes new entries to the #__virtuemart_product_categories table.....

prune the most recent entries till your back where u started?
Title: Re: Shows all products from subcategories in parent categories too
Post by: Studio 42 on December 08, 2016, 09:57:52 AM
Quote from: rage76 on December 07, 2016, 14:42:15 PM
@valekichia

wow, you are very correct about the VM Auto Parent Categories Plugin

I did not notice (read bothered) this earlier. can we change something in the plugin itself to avoid the different URLs?

on the other hand, now that I have used this plugin, can I revert the changes made by this plugin so that I opt for the core hack ??
To unset products categories, you can use my component http://shop.st42.fr/en/products/vm-be-pro-batch-product-edit.htm.
You can not only remove or add directly categories but update and correct many other problems, without needs to go in each product.
Title: Re: Shows all products from subcategories in parent categories too
Post by: valekichia on December 12, 2016, 11:11:46 AM
@GJC Web Design

Quotealthough changing one string on an update shouldn't be too much hassle

Of course it wouldn't. I was only double checking my knowledge. Since I'm building it on my own, talking to more experienced people it's always a good opportunity to consolidate it. :)

Quotere prods mod.. you need to check what it's querying (switch off SEF) - I would have assumed it is the same query

I followed your suggestion and found that the query is the same, except for the "Itemid" for VM homepage.

In my case, this is virtuemart component's query:
(siteroot)/index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id=8&virtuemart_category_id=4&Itemid=187

And this is virtuemart products module's query:
(siteroot)/index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id=8&virtuemart_category_id=4

I think that, to obtain the same result in the module's behaviour, I should make some changes to modules/mod_virtuemart_product/mod_virtuemart_product.php file.
Is it correct?
Unfortunately I didn't find a block of code similar to the one you stated above for VM component and my coding skills are not enough to solve the problem.
Could you please give me some help?

This analysis leaves me with another question:
VM products module doesn't add the Itemid to the query. This means that all the modules set to show on this Itemid (and consequently - in my case - also on product details pages) won't show!
Then, a user that reaches the product from VM component will see all the related modules. On the other hand, a user that reaches the product from VM prods mod won't see those modules, which is not so consistent.
Do you have some advice on this too?

Thank you again!
Title: Re: Shows all products from subcategories in parent categories too
Post by: Studio 42 on December 12, 2016, 12:00:57 PM
If you need optimised site, use SEF urls.
&Itemid=187 is curent selected menu, but for a module, this itemid can be different, but on using the SEF urls, this itemId get hidden and recalculated, so you have always same URLS for all links and don't need to fight with this problem(and of course Google is more happy).
Title: Re: Shows all products from subcategories in parent categories too
Post by: valekichia on December 12, 2016, 16:46:04 PM
QuoteIf you need optimised site, use SEF urls.

Silly me! I disabled SEF urls for the testing purposes above but didn't focus on the fact that before everything was working correclty! Reactivated SEF urls and everything is ok again!
Thank you @Studio 42 for pointing me in the right direction!

Nevertheless the previous question is still open.

How can I let VM products module show products assigned exclusively to the subcategories of the selected category?
Title: Re: Shows all products from subcategories in parent categories too
Post by: GJC Web Design on December 12, 2016, 17:16:39 PM
Quoteexclusively

??

Then you need to have a if/else statement in your products.php  sortSearchListQuery() function to only deep search if the catid is/is-not = [an array of cat ids]

but all requests for products are returned by sortSearchListQuery() -- doesn't matter if it is the comp or modules etc
Title: Re: Shows all products from subcategories in parent categories too
Post by: valekichia on December 12, 2016, 18:13:07 PM
Quoteexclusively

= my products are assigned only to subcategories and not to both the subcategory and the main category. Or at least I'd like to, for the reasons stated above.
Sorry if I used a wrong term to describe this.

Following your suggestion I managed to let VM main category pages show the products from subcategories.

I'd like to achieve the same result with VM prods mod.
By now, with no products assigned to main categories, the module is empty if I set it to display products from a main category (obviously), but I don't want to switch to a specific subcategory. I wish it can display products from all the subcategories.

I think I can't follow you...

QuoteThen you need to have a if/else statement in your products.php

where do I find products.php?

QuotesortSearchListQuery() function to only deep search if the catid is/is-not = [an array of cat ids]

can you provide the specific chunk of code?

Thanks


Title: Re: Shows all products from subcategories in parent categories too
Post by: Studio 42 on December 12, 2016, 21:50:35 PM
GJC Web Design explained to change false to true in the code, so the products lists are always get from subcaetgories.
This should work in the virtuemart core modules too.
Title: Re: Shows all products from subcategories in parent categories too
Post by: valekichia on December 13, 2016, 14:11:33 PM
@Studio 42 Thank you for the explanation!

Yesterday I felt I was missing something because, even though I had cleared both browser and joomla cache after the change, the module wasn't working properly.
Today I opened it and there it is, working as you and GJC Web Design explained.  :o

May I ask something else related to the categories and subcategories behavior?

This is my current situation:
- I have several main categories, which one has multiple subcategories
- my products are only assigned to subcategories
- I have an extra main category called "Discounted products", to which I assign the products on sale (in addiction to the original subcategory)

Now, when I click on a discounted product (both from VM comp and VM prods mod) it will open under the "Discounted products" category.
I'd prefer it to open under its subcategory instead.
I read in the forum that the "Discounted products" category is a suggested way to gather the products on sale, but maybe there are other solutions.
I think that a "Discounted products" should act like a filter instead of being an actual category.
I don't know if it's clear enough.

Please, could you give me your opinions/suggestions about this?

Thank you as always for your patience!
Title: Re: Shows all products from subcategories in parent categories too
Post by: Abatap on December 15, 2016, 16:12:44 PM
Hello, everyone. First of all, thank you guys for providing the easiest solution solve this problem. And now I have a small question: Is there a way to make VM category pages show child category products separated by child category titles they belong to?

What I need should look something like this:

Child category title 1
--------------------
Child product | Child product | Child product
...
Child product | Child product | Child product


Child category title 2
--------------------
Child product | Child product | Child product
...
Child product | Child product | Child product

...

Child category title n
--------------------
Child product | Child product | Child product
...
Child product | Child product | Child product

I don't really expect someone here to write out a fully working solution, just give me a hint where to look at in the VM code. I'm not that good with it's architecture at the moment, so a hint would be nice.
Title: Re: Shows all products from subcategories in parent categories too
Post by: Studio 42 on December 15, 2016, 16:44:48 PM
Abatap,
for your case the proposed code do not work, because this use a global request for all your shop view and modules.
If you want this, you have to not change the code, but change your layout and get products from child categories only, someone in the forum(perhaps me) sended the code, of you search a little, i think you find it.
Title: Re: Shows all products from subcategories in parent categories too
Post by: Abatap on December 15, 2016, 17:04:33 PM
Uh, the provided solution solves my problem exactly, it's just that in my exact case it would be a lot nicer if all the child products weren't just glued into a single list but separated in blocks by their respective category name with the category name being displayed on top of each block. I do understand though that the solution for my case might require some non-trivial changes to existing views.
Title: Re: Shows all products from subcategories in parent categories too
Post by: Studio 42 on December 15, 2016, 23:43:43 PM
Quote from: Abatap on December 15, 2016, 17:04:33 PM
Uh, the provided solution solves my problem exactly, it's just that in my exact case it would be a lot nicer if all the child products weren't just glued into a single list but separated in blocks by their respective category name with the category name being displayed on top of each block. I do understand though that the solution for my case might require some non-trivial changes to existing views.
The querie do not check for the sub categories Levels, because it get all subcategories in one query.
If you know a little mysql the query is a IN and this not group the product in the categories correctly.
So the real working solution, is to add each product in the parent categories so you can then us a request by child level category and order all properly. If you want group it.
You can use the proposed code changes but this can be very slower if you have many caetgories, but in all case you need to use a sub query for product in child cateogries.

Title: Re: Shows all products from subcategories in parent categories too
Post by: Abatap on December 16, 2016, 06:22:17 AM
Thanks for all the details.
Title: Re: Shows all products from subcategories in parent categories too
Post by: heslikk on May 29, 2017, 10:30:57 AM
Quote from: GJC Web Design on December 06, 2016, 19:31:41 PM
this has been available in VM3 since I think 3.0.9 but it requires a small code change.. I think the intention was to make a config for it

but look around line 370 of  \administrator\components\com_virtuemart\models\product.php


if ($virtuemart_category_id > 0) {
$joinCategory = TRUE;
if(true){
$where[] = ' `pc`.`virtuemart_category_id` = ' . $virtuemart_category_id;
} else {
/*GJC add subcat products*/
$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.') ';
}
} else


if u change   

if(true){

to

if(false){

then all the products in the parent and subcats will be returned
Everything works, but does not work filter by manufacturer...
Title: Re: Shows all products from subcategories in parent categories too
Post by: heslikk on May 29, 2017, 14:59:08 PM
There are ideas how to make a working filter by the manufacturers?
Title: Re: Shows all products from subcategories in parent categories too
Post by: Studio 42 on May 29, 2017, 21:30:33 PM
You can use custom filter pro component or you can develop your own module.
For eg. This use Virtuemart https://ongallery.com/en/themes and a self made filter module.
Title: Re: Shows all products from subcategories in parent categories too
Post by: EIF on March 02, 2022, 22:52:48 PM
I am trying to get this to work in Virtuemart 3.8.8 10472.

Displaying all products from the subcategories works, but totally. For example:

Category A
  - Sub A1
       Subsub A11
       Subsub A12
  - Sub A2
  - Sub A3

Category B
Category C

With the current options and conditions, I get it to work when I display Sub a1 category that all items will be displayed there. So all products from subsub A11 and subsub A12 are shown in Sub A1. That works.

But... when I display Category A, I want all products from al the sub categories and the subsub categories (the sub of the sub category). Both in the menu items setting and in the category setting, there is an option to display the products. But it only displays 3 products. It should be hundreds.

What is going wrong?


EDIT: I made a new own topic for this issue --> https://forum.virtuemart.net/index.php?topic=147957.0 (https://forum.virtuemart.net/index.php?topic=147957.0)