2015-02-09 124 views
0

我想從我擁有的隨機菜單中排除一個特定類別。我已經搜索了答案,並在這裏找到了一些潛在的解決方案,但是他們都沒有工作。Magento:從下拉菜單中排除特定類別

這是我目前有:

<?php 
    $catID = $this->category_id; 
    $catName = $this->category_name; 
    $catType = $this->category_type; 
    $category = Mage::getModel('catalog/category')->load($catID); 
    $categories = $category->getCollection() 
     ->addAttributeToSelect(array('name', 'thumbnail', 'description')) 
     ->addAttributeToFilter('is_active', 1) 
     ->addIdFilter($category->getChildren()); 
    $catIdEx = 38; 
    $categories->getSelect()->join(array('cats' => 'catalog_category_product'), 'cats.product_id = e.entity_id'); 
    $categories->getSelect()->where('cats.category_id', array('neq' => $catIdEx)); 
    $categories->getSelect()->group(array('e.entity_id')); 
    $categories->getSelect()->order('RAND()'); 
    $categories->getSelect()->limit(1); 
?> 
<?php foreach ($categories as $category): ?> 
<div> 
    <a href="<?php echo $category->getUrl(); ?>"> 
     <img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" class="boxedimage" /> 
    </a> 
    <br /> 
    <h3>Spotlight <?php echo $catName ?></h3> 
    <?php 
     $sdesc = $category->getDescription(); 
     $sdesc = trim($sdesc); 
     $limit = 230; 
     if (strlen($sdesc) > $limit) { 
      $sdesc = substr($sdesc, 0, strrpos(substr($sdesc, 0, $limit), ' ')); 
     } 
    ?> 
    <?php echo $sdesc."..."; ?> 
    <div><a href="<?php echo $category->getUrl(); ?>" class="go">View <?php echo $category->getName(); ?> <?php echo $catType ?>...</a></div> 
</div> 
<?php endforeach; ?> 

回答

0

你收集的呼叫看起來很潔癖的原因我肯定讓你感覺,所以我會用這個去 - 如果你只是想排除類別,將其添加到foreach中;

<?php foreach ($categories as $category): ?> 
<?php if($category->getId() !== $catIdEx) { ?> 
<div> 
<!-- your other code here --> 

</div> 
<?php } ?> 
<?php endforeach; ?> 
+0

這似乎沒有任何工作。我離開了我的編碼。我也刪除了應該在上面進行過濾的代碼。 – Corey 2015-02-09 19:21:59

+0

http://www.steinjewelrycompany.com/fine-gifts-and-accessories 我只有2個類別目前在下拉列表中的首飾上激活。您將在右側看到Spotlight設計器。勞力士根本不應該出現,它仍然找到方向。勞力士也是貓ID:38。我檢查確定我有很多次正確的ID。 – Corey 2015-02-09 19:23:09

+0

聽起來像是您的$ catIdEx值不正確。在foreach循環中添加<?php echo'ID是:'。$ category-> getId(); ?>確定你試圖排除的那個人擁有你認爲它的ID – PixieMedia 2015-02-09 19:23:55

0
$excluded_category = array('20','4','6'); 
<?php foreach ($categories as $category): ?> 
<?php if(!in_array($category->getId(),$excluded_category)) { ?> 
<div> 
<!-- your other code here --> 

</div> 
<?php } ?> 
<?php endforeach; ?> 
+0

Manoj。這確實有效,但並不完全是我需要它做的。該代碼正在一個隨機腳本上運行,該腳本選擇一個類別來突出顯示。每次刷新或轉到新頁面時都會更改。消除這個掉落整個元素。我只是想讓它在腳本中被遺忘,所以另一個可以替代它。如果這是有道理的。 – Corey 2015-02-10 15:12:08