2013-03-29 20 views

Trả lời

4

Vui lòng thử thế này, tốt làm việc của nó ở cuối của tôi

<?php 
$parentCategoryId = 10; 
$categories = Mage::getModel('catalog/category')->load($parentCategoryId)->getChildren(); 
$catArray = explode(',', $categories); 
foreach($catArray as $child) 
{ 
$_child = Mage::getModel('catalog/category')->load($child); 
echo $_child->getName() . '<br />'; 
echo $_child->getUrl() . '<br />'; 
echo $_child->getDescription() . '<br />'; 
} 
?> 
+0

Cảm ơn người đàn ông, bạn là anh hùng –

8

Nếu bạn có id current_category sau đó loại tải

$category = Mage::getModel('catalog/category')->load(id);

và kiểm tra count($category->getChildren());

Các phương pháp khác là cho trẻ em đếm

count($category->getChildrenNodes()); 

$category->getChildrenCount(); 

Bằng cách này bạn có thể kiểm tra xem loại có con hay không.

getChildren() phương pháp cung cấp cho bạn id danh mục trẻ em và dựa trên id bạn có thể lấy tên danh mục và hình ảnh.

+0

tôi đã cố gắng mã của bạn nhưng nó không làm việc .. luôn luôn hiển thị 1 khi tôi đếm các trẻ em nhưng nó có 2 loại con tôi đặt mã này $ current_catid = $ this-> getCategoryId(); $ category = Mage :: getModel ('danh mục/danh mục') -> tải ($ current_catid); số echo ($ category-> getChildren()); –

+0

Hãy thử phương thức getChildre() -> count() thay vào đó hoặc bạn cũng có thể sử dụng số đếm ($ category-> getChildrenNodes()); cho trẻ em thuộc nhóm số – Mufaddal

+0

tôi đã thử điều này như bạn đã nói với tôi $ category-> getChildren() -> count(); nó mang lại cho tôi lỗi nghiêm trọng –

1

Một chút cũ, nhưng tôi đã tìm kiếm các giải pháp tương tự và @ giải pháp Mufaddal đã không làm việc. Sau đó, tôi tìm thấy getChildrenCategories().

$_category = Mage::registry('current_category'); 
count($_category->getChildrenCategories()); 
0

bạn có một tùy chọn để kiểm tra loại mục con tồn tại hay không ..

<?php 
    $currentCategoryId = Mage::registry('current_category')->getId(); 
    $collection = Mage::getModel('catalog/category')->getCollection() 
     ->addAttributeToFilter('is_active', 1) //only active categories 
     ->addAttributeToFilter('parent_id', $currentCategoryId); 
    $currentCat = Mage::registry('current_category'); 
    $subCategories = Mage::getModel('catalog/category')->load($currentCat->getParentId())->getChildrenCategories(); 

    if($collection->getSize() >= 1){//there some thing....}else{ 

//Get Subcategory.... 


foreach ($subCategories as $subCategoryId): 

        if($subCategoryId->getIsActive()) 
        { $products = Mage::getModel('catalog/category')->load($subCategoryId->getId()) 
        ->getProductCollection() 
        ->addAttributeToSelect('entity_id') 
        ->addAttributeToFilter('status', 1) 
        ->addAttributeToFilter('visibility', 4); 


         <li <?php if($subCategoryId->getId()==$currentCategoryId){?>class="active"<?php } ?>> 
          <a href="<?php echo $subCategoryId->getURL(); ?>"> 
           <?php //echo $subCategoryId->getName()." (".$products->count().")"; ?> 
           <?php echo $subCategoryId->getName(); ?> 
          </a> 
         </li> 
         } endforeach;}?> 

nếu nó giúp đầy đủ cho tôi biết ...

Cảm ơn Ravi

1

Trên thực tế nó phụ thuộc vào việc có hay không tùy chọn "Use Flat Catalog Category" được bật.

Vì vậy, phương pháp tốt nhất để kiểm tra loại có thể loại con hay không là:

if (Mage::helper('catalog/category_flat')->isEnabled()) { 
    $childrenCount = $category->getResource()->getChildrenAmount($category); 
} else { 
    $childrenCount = $category->getResource()->getChildrenCount(); 
} 

với $ category Tôi giả sử bạn có đã có, như:

$category = Mage::getModel('catalog/category')->load(id); 
Các vấn đề liên quan