Need extra help with your Joomla site? Consider paid Joomla support by the developer of Chameleon and MetaMod.
Virtuemart Recipe + Parent Categories
Virtuemart Recipe + Parent Categories
Hi, The site i am working on is a Virtuemart Shop split into "departments", each department has its own top level category in Virtuemart. In each department I want to specify its own menu module, but to do this I havent found a easierly way other than inputing each individual categoryID in each department in the recipe. Is there a recipe where metamod can take the Toplevel category from virtuemart and apply the same module to its parent/lower level categories below it?
Re: Virtuemart Recipe + Parent Categories
Hi tekfixcomputingsolutions,
that's a great question, and I can see how useful it will be.
I think that the biggest problem here is simply efficiency, as it's going to take several database queries to go from product to parent category to grandparent category to... etc.
Can you tell me how many levels deep your hierarchy is?
I'll start working on the concept, but it would also be useful to have an idea of the potential depth if you can provide an estimate of that.
Cheers,
Stephen
Re: Virtuemart Recipe + Parent Categories
Ok, here's a recipe.
A couple of things to bear in mind:
1 - customise the array of top-level categories to module id's at the top of the recipe. You should not need to customise any more than that.
2 - the recipe tries to find the string "category_id" in the URL, and uses the category id it finds there to search for the top-level parent category. If the product belongs to more than 1 category, then still only 1 category_id is given in the URL, so the particular category id shown will probably depend on the path that it took to get there.
I also included another kind of category lookup, which actually searches the database for ALL categories that the product is a part of. This lookup is only triggered when there's no category_id in the URL, but if you really want that one by default then you'll need to modify the recipe very slightly.
3 - the recipe only operates on the shop.product_details page. I hope that's the one you are wanting. It's easy enough to change to include other pages if required.
Code:
//mapping top level categories to modules
$map = array(
"2"=>100,
"3"=>101,
"4"=>102
);
if (! function_exists("mm_find_vm_parent") ) {
function mm_find_vm_parent($id, $cat_map) {
$ret = @$cat_map[ $id ];
if ($ret == $id) return null; // stop infinite recursion
if ($ret === null) return null;
if ($ret == 0) return $id;
return mm_find_vm_parent($ret, $cat_map);
}
}
if ($option == "com_virtuemart" and JRequest::getVar("page") == "shop.product_details") {
// get category id
$category_id = JRequest::getInt( 'category_id', 0 );
if ( $category_id > 0 ) {
$categories = array( $category_id );
} else {
$pid = JRequest::getInt("product_id",0);
if ($pid == 0) return;
$sql = "select category_id from #__vm_product_category_xref where product_id = $pid";
$db->setQuery( $sql );
$categories = $db->loadResultArray();
}
if (! is_array($categories) or count($categories) == 0) return;
// now we have an array of category_id's
if (@$map[$parent_id]) return $map[$parent_id];
$sql = "select category_parent_id, category_child_id from #__vm_category_xref";
$db->setQuery( $sql );
$rows = $db->loadObjectList();
$cat_map = array();
foreach ($rows as $row) {
$cat_map[$row->category_child_id] = $row->category_parent_id;
}
$cats = array();
foreach ($categories as $c) {
$top_parent = mm_find_vm_parent( $c, $cat_map );
if ($top_parent) $cats[] = $top_parent;
}
$cats = array_unique( $cats );
$ret = array();
foreach ($cats as $cat) {
if ( array_key_exists($cat, $map) ) $ret[] = $map[$cat];
}
return $ret;
}
Re: Virtuemart Recipe + Parent Categories
Stephen,
Great module!
I have also exactly the same problem as the OP. I have tried implementing your code as above, but it does not work.
I presume this maybe because of the amount of levels and categories we have on the site - they are 5-6 levels deep and some products are in multiple categories.
Also I want the module to display on both category pages & product pages.
Do you have any other pointers that may be able to hope me?
Many thanks in anticipation,
ash
Re: Virtuemart Recipe + Parent Categories
Hi ashp,
Actually, things have moved on a lot since this thread was started. There's now a much easier way to detect if the current product/category "descends" from any given category.
Try it this way:
$vm = JomGenius("virtuemart");
if ($vm->check("ancestor_category_ids = 2")) return XXX;
// replace XXX with module id(s) to display for all products
// and categories that have category "2" as an ancestor
The ancestor_category_ids method checks up to 8 levels of ancestors (for efficiency). I hope that's enough!
Is this what you're after?
Cheers,
Stephen
Re: Virtuemart Recipe + Parent Categories
Hi Stephen,
Yes this is what I am after. - Basically i am using the module to display banners, so different banners will be displayed in different VM categories.
I have tried using your code but it does not work (nothing is displayed) at any level.
I am using SEF Urls, but not sure if this would make a difference?
thanks for your help
Re: Virtuemart Recipe + Parent Categories
Hi,
if nothing is being displayed, can you please check the following setup things:
1 - is the MetaMod enabled/published?
2 - is the MetaMod set to a module position that's actually on the page?
3 - is the MetaMod set to the right menu items, or "all" menu items? If it's not then it's not going to get run in order to process the PHP and include the other module.
A basic test you can do is to turn on MetaMod debug mode. If you see some basic debug output then at least points 1-3 above are working ok, then you can start looking at the PHP code to make sure it's correct. I'm pretty sure the code is ok as long as you replace "2" with the category id that you are checking for, and "XXX" with the module id that you want to include.
SEF URLs should not make a difference. MetaMod works happily with or without them.
Cheers,
Stephen
Re: Virtuemart Recipe + Parent Categories
Apologies,
What I mean is that it does not work when using the code you provided above, where you only need to provide the category id for the parent category. When using this code the module does not work at all.
The module does works fine when I use the code you provided in the recipes section, the only problem is that the banner is not displayed in subcategories when using this code, only the parent category.
Hope this makes sense, please let me know if it needs any more clarification,
thanks
Re: Virtuemart Recipe + Parent Categories
Hi ashp,
Can you post the code that you are using at the moment (from the recipes section), and explain when it works and when it doesn't work? Then I can suggest alterations to it.
Thanks,
Stephen
Board Info
- Board Stats:
- Total Topics:
- 1689
- Total Polls:
- 6
- Total Posts:
- 5941
- Posts this week:
- 1
- User Info:
- Total Users:
- 7618
- Newest User:
- goure1dhowen
- Members Online:
- 0
- Guests Online:
- 109
- Online:
- There are no members online
Forum Legend:
Topic
New
Locked
Sticky
Active
New/Active
New/Locked
New Sticky
Locked/Active
Active/Sticky
Sticky/Locked
Sticky/Active/Locked