Magento Displays the number of subcategories and categories of products in the specified category

Source: Internet
Author: User
Tags arrays

A situation that is often encountered in two of times of development is to determine whether a classification belongs to a large classification, and the large classification includes many subcategories into a tree structure, the basic method is to get all the subcategories under the large classification of information, and then compare each to see if there is an ID and the undetermined ID equal. A recursive method is used to get all the IDs of the large classification, and the second one is to get all the sub classification IDs by the method of queue and so on, and the second kind of performance is better.

This article tests the success of a function in the Magento template file: Enter the ID of a category, and return the ID of all subcategories (recursively fetched) under that category, and form an array. The method used is a recursive method implemented by queues:

The code is as follows Copy Code

/**
* Input: ID number of a category
* Return: An array of IDs of all subcategories under this category
* Can be used for: the template file can be used directly, can also be used for action and other file classes inside
* Implementation of the idea: the use of the queue method to achieve non-recursion, the tree from the top down traverse
**/
function Getallchildrenofcategory ($cateid) {
$RESARR = Array ();//Result arrays
$QUEUEARR = Array ();//Queue arrays
Array_push ($QUEUEARR, $cateid);

while ($currentcid = Array_pop ($QUEUEARR)) {
Array_push ($RESARR, $currentcid);
Handle the child node of the current node
$_category = Mage::getmodel (' catalog/category ')->load ($CURRENTCID);
$subcats = $_category->getchildren ();

$idarrs = Explode (', ', $subcats);
foreach ($idarrs as $subCatid) {
if (! $subCatid) continue;
$_subcategory = Mage::getmodel (' catalog/category ')->load ($subCatid);
if ($_subcategory->getisactive ()) {
Array_push ($QUEUEARR, $subCatid);
}
}
Reset ($QUEUEARR);
}
return $RESARR;
}
Test
$allProducerIds = Getallchildrenofcategory (19);
$allDesignedIds = getallchildrenofcategory (18);


PHP does not have stacks and queues of data structure, you can use arrays to simulate the implementation of the array of Array_push and Array_pop is just so two methods, which because the Array_pop will change the array of pointers each time, so you can reset the array at the end of the loop reset.

The final test, input is the ID of two categories, after the completion of the function, the returned array is the ID of all subcategories, if we want to call the specified category of products and get sub categories and the number of products to achieve the above method is not perfect


Magento Home and category page sidebar often need to call a category of products, such as the home page featured product. These classifications are generally inactive, and we can add more best-selling products from the store to the category and call them from the foreground. The main use of the following code is to get the product under the specified category in the Magento.

The code is as follows Copy Code

$products = Mage::getmodel (' catalog/category ')->load ($category _id)
->getproductcollection ()
->addattributetoselect (' * ')
->addattributetofilter (' status ', 1)
->addattributetofilter (' visibility ', 4);

Modify the $category_id above to show the product's category ID, which can be seen in the category page. The above code also incidentally some filtering, the product status is active, and in the visible state.

In many Magento projects, customer requirements display each subcategory under each current category and the number of products under that category, similar to the category (108) Form. As shown below

To do this, you must know how to get the subcategory of the current taxonomy and understand the count () method in the Product collection class. This method is used to obtain the number of products that are filtered by the product collection under Any form.

The code is as follows Copy Code


Get the current classification model
$currCat = Mage::registry (' current_category ');

Gets the model collection for all subcategories of the current taxonomy
$collection = Mage::getmodel (' catalog/category ')->getcategories ($currCat->getentityid ());

Model collection of Loop subcategories
foreach ($collection as $cat) {
if ($cat->getisactive ()) {
$category = Mage::getmodel (' catalog/category ')->load ($cat->getentityid ());

Gets the product collection of the subcategory and obtains the number of products for the subcategory by using the Count () method
$prodCollection = Mage::getresourcemodel (' catalog/product_collection ')->addcategoryfilter ($category);
Mage::getsingleton (' Catalog/product_status ')->addvisiblefiltertocollection ($prodCollection);
Mage::getsingleton (' catalog/product_visibility ')->addvisibleincatalogfiltertocollection ($prodCollection);

$html. = ' <a href= <?php echo $category->geturl ()?> "><?php echo $category->getname ()? ></a& Gt (<?php echo $prodCollection->count ()?>) <br/> ';
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.