从list.phtml结果中排除产品(magento)


Exclude products from list.phtml results (magento)

在Magento中,有没有办法强制从list.phtml视图中排除项目(通过属性值)?我们的产品应该是可装盒的,可以单独查看,但不应该通过搜索或导航到其类别来找到。

您可以扩展Mage_Catalog_Model_CategorygetProductCollection方法(如果愿意,可以创建一个自定义Block类来替换某些模板)。

假设这是在一个名为<YourNamespace>_<YourModule>的模块中完成的,这看起来像:

class <YourNamespace>_<YourModule>_Model_Category extends Mage_Catalog_Model_Category {
    public function getProductCollection() {
        $collection = parent::getProductCollection()
        foreach($collection as $key => $item) {
            if (<YOUR_REMOVE_CRITERIA_HERE>) {
                $collection->removeItemByKey($key);
            }
        }
    }
}

<YOUR_REMOVE_CRITERIA_HERE>可以是从配置选项到集合中项目(产品)的属性的任何内容。

更简单的解决方案是将它们从类别中删除,如果你只想让一个产品不在类别产品列表中列出。

希望我能帮忙,

lg,

flo

我们最终在没有创建新模块的情况下解决了这个问题。我们在/app/code/local/mage/controlog/block/product中创建了list.php的覆盖,并包含以下代码行:

$collection->clear()
->addAttributeToFilter('my_attribute_name', array('gteq'=> 524 )) //custom attrib ID
->addAttributeToFilter('visibility', array('eq'=> 4 )) // Visibility is equal to "Catalog/Search"
->load();

之后:

protected function _beforeToHtml()
{
    $toolbar = $this->getToolbarBlock();
    // called prepare sortable parameters
    $collection = $this->_getProductCollection();
    // use sortable parameters
    if ($orders = $this->getAvailableOrders()) {
        $toolbar->setAvailableOrders($orders);
    }
    if ($sort = $this->getSortBy()) {
        $toolbar->setDefaultOrder($sort);
    }
    if ($dir = $this->getDefaultDirection()) {
        $toolbar->setDefaultDirection($dir);
    }
    if ($modes = $this->getModes()) {
        $toolbar->setModes($modes);
    }

之前:

// set collection to toolbar and apply sort

我们还为新产品页面在new.php中添加了同样的逻辑,这样它就可以过滤掉不应该可见的产品。

Brad

我知道在表示层添加业务逻辑不是一个好的做法,但无论如何,在app''design''frontend''iln''default''template''catalog''product''list.php 中添加产品列表是一种快速的方法

<?php $_selling_type = $_product->getResource()->getAttribute('selling_type')->getFrontend()->getValue($_product); ?>
if ($_selling_type == 'No Price') 
{
  echo ('what ever you want');
}