Author Topic: Shows all products from subcategories in parent categories too  (Read 3712 times)

mogaka

  • Beginner
  • *
  • Posts: 4
  • A beginner
Shows all products from subcategories in parent categories too
« 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'

valekichia

  • Beginner
  • *
  • Posts: 26
Re: Shows all products from subcategories in parent categories too
« Reply #1 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!

rage76

  • Jr. Member
  • **
  • Posts: 91
Re: Shows all products from subcategories in parent categories too
« Reply #2 on: December 06, 2016, 18:06:15 pm »
Hi, if I am getting you correctly, please try VM Auto Parent Categories Plugin


GJC Web Design

  • 3rd party VirtueMart Developer
  • Hero Member
  • *
  • Posts: 7485
  • Virtuemart, Joomla & php developer
    • GJC Web Design
  • VirtueMart Version: 2.6.22 & 3.2.2
Re: Shows all products from subcategories in parent categories too
« Reply #3 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


Code: [Select]
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
GJC Web Design
VirtueMart and Joomla Developers - php developers http://www.gjcwebdesign.com
VM3 AusPost Shipping Plugin - e-go Shipping Plugin - VM3 Postcode Shipping Plugin - Radius Shipping Plugin - VM3 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
http://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

valekichia

  • Beginner
  • *
  • Posts: 26
Re: Shows all products from subcategories in parent categories too
« Reply #4 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!

GJC Web Design

  • 3rd party VirtueMart Developer
  • Hero Member
  • *
  • Posts: 7485
  • Virtuemart, Joomla & php developer
    • GJC Web Design
  • VirtueMart Version: 2.6.22 & 3.2.2
Re: Shows all products from subcategories in parent categories too
« Reply #5 on: December 07, 2016, 11:54:28 am »
Quote
But 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
GJC Web Design
VirtueMart and Joomla Developers - php developers http://www.gjcwebdesign.com
VM3 AusPost Shipping Plugin - e-go Shipping Plugin - VM3 Postcode Shipping Plugin - Radius Shipping Plugin - VM3 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
http://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

rage76

  • Jr. Member
  • **
  • Posts: 91
Re: Shows all products from subcategories in parent categories too
« Reply #6 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 ??

GJC Web Design

  • 3rd party VirtueMart Developer
  • Hero Member
  • *
  • Posts: 7485
  • Virtuemart, Joomla & php developer
    • GJC Web Design
  • VirtueMart Version: 2.6.22 & 3.2.2
Re: Shows all products from subcategories in parent categories too
« Reply #7 on: December 07, 2016, 23:32:13 pm »
Quote
can 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?
GJC Web Design
VirtueMart and Joomla Developers - php developers http://www.gjcwebdesign.com
VM3 AusPost Shipping Plugin - e-go Shipping Plugin - VM3 Postcode Shipping Plugin - Radius Shipping Plugin - VM3 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
http://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

Studio 42

  • Contributing Developer
  • Full Member
  • *
  • Posts: 2128
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3.0.x.y
Re: Shows all products from subcategories in parent categories too
« Reply #8 on: December 08, 2016, 09:57:52 am »
@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.

valekichia

  • Beginner
  • *
  • Posts: 26
Re: Shows all products from subcategories in parent categories too
« Reply #9 on: December 12, 2016, 11:11:46 am »
@GJC Web Design

Quote
although 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. :)

Quote
re 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!

Studio 42

  • Contributing Developer
  • Full Member
  • *
  • Posts: 2128
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3.0.x.y
Re: Shows all products from subcategories in parent categories too
« Reply #10 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).

valekichia

  • Beginner
  • *
  • Posts: 26
Re: Shows all products from subcategories in parent categories too
« Reply #11 on: December 12, 2016, 16:46:04 pm »
Quote
If 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?

GJC Web Design

  • 3rd party VirtueMart Developer
  • Hero Member
  • *
  • Posts: 7485
  • Virtuemart, Joomla & php developer
    • GJC Web Design
  • VirtueMart Version: 2.6.22 & 3.2.2
Re: Shows all products from subcategories in parent categories too
« Reply #12 on: December 12, 2016, 17:16:39 pm »
Quote
exclusively

??

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
GJC Web Design
VirtueMart and Joomla Developers - php developers http://www.gjcwebdesign.com
VM3 AusPost Shipping Plugin - e-go Shipping Plugin - VM3 Postcode Shipping Plugin - Radius Shipping Plugin - VM3 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
http://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

valekichia

  • Beginner
  • *
  • Posts: 26
Re: Shows all products from subcategories in parent categories too
« Reply #13 on: December 12, 2016, 18:13:07 pm »
Quote
exclusively

= 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...

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

where do I find products.php?

Quote
sortSearchListQuery() 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



Studio 42

  • Contributing Developer
  • Full Member
  • *
  • Posts: 2128
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3.0.x.y
Re: Shows all products from subcategories in parent categories too
« Reply #14 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.