2013-03-29 7 views
5

में मैं श्रेणी आईडी मैं इस कोडजांच श्रेणी बच्चे श्रेणी है या नहीं Magento

<?php echo $current_catid=$this->getCategoryId(); ?> 

अब मैं जाँच करने के लिए कि इस श्रेणी बच्चे श्रेणी या नहीं है चाहता हूँ से आईडी मिल गया है।

यदि उसके बच्चे हैं तो बच्चे श्रेणी छवि और नाम और यूआरएल दिखाएंगे।

उत्तर

4

मेरी अंत

<?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

धन्यवाद आदमी, आप नायक हैं –

8

यदि आप current_category आईडी तो लोड श्रेणी है

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

और जाँच count($category->getChildren());

अन्य तरीकों गिनती बच्चों

count($category->getChildrenNodes()); 

$category->getChildrenCount(); 

इस तरह आप श्रेणी अगर जाँच कर सकते हैं के लिए कर रहे हैं बच्चे हैं या नहीं।

getChildren() विधियां आपको बच्चों की श्रेणी आईडी देती हैं और आईडी पर आधारित आप श्रेणी छवि और श्रेणी का नाम प्राप्त कर सकते हैं।

+0

मैं अपने कोड की कोशिश की, लेकिन इसके नहीं:

if (Mage::helper('catalog/category_flat')->isEnabled()) { $childrenCount = $category->getResource()->getChildrenAmount($category); } else { $childrenCount = $category->getResource()->getChildrenCount(); } 
$ श्रेणी के साथ

मैं आप पहले से ही है, के रूप में लगता है काम करते हैं .. जब मैं बच्चों की गिनती करता हूं तो हमेशा 1 दिखा रहा है लेकिन इसकी 2 बच्चे श्रेणी है, मैंने यह कोड $ current_catid = $ this-> getCategoryId() प्राप्त किया है; $ श्रेणी = मेज :: getModel ('कैटलॉग/श्रेणी') -> लोड ($ current_catid); इको गिनती ($ श्रेणी-> getChildren()); –

+0

इसके बजाय getChildre() -> गिनती() विधियों का प्रयास करें या आप गिनती ($ श्रेणी-> getChildrenNodes()) का भी उपयोग कर सकते हैं; गिनती श्रेणी के बच्चों के लिए – Mufaddal

+0

मैंने कोशिश की क्योंकि आपने मुझे $ श्रेणी-> getChildren() -> गिनती(); यह मुझे घातक त्रुटि दे रहा है –

1

थोड़ा की उम्र में यह एक अपने काम के ठीक करने का प्रयास करें, लेकिन मैं एक ही समाधान के लिए और @ Mufaddal के समाधान काम नहीं किया खोज रहा था। तब मुझे getChildrenCategories() मिला।

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

आप श्रेणी बच्चे श्रेणी मौजूद है या नहीं की जाँच करने के एक और विकल्प ..

<?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;}?> 

अगर यह पूरा करने में मदद मुझे पता है है ...

धन्यवाद रवि

1

असल में है यह इस बात पर निर्भर करता है कि "फ्लैट कैटलॉग श्रेणी का उपयोग करें" विकल्प सक्षम है या नहीं।

इसलिए

, श्रेणी बच्चे श्रेणी या नहीं है की जाँच करने के लिए सबसे अच्छा तरीका:

$category = Mage::getModel('catalog/category')->load(id); 
संबंधित मुद्दे