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

Php 从list.phtml结果中排除产品(magento),php,magento,Php,Magento,是否有办法(通过属性值)从Magento的list.phtml视图中强制排除项? 我们的产品应该可以单独携带、查看,但不应该通过搜索或导航到其类别来找到 您可以扩展Mage\u Catalog\u Model\u类别的getProductCollection方法(然后创建自定义块类以替换某些模板,如果愿意的话) 假设这是在一个名为的模块中完成的,它看起来像: class <YourNamespace>_<YourModule>_Model_Category extends

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

您可以扩展
Mage\u Catalog\u Model\u类别的
getProductCollection
方法(然后创建自定义块类以替换某些模板,如果愿意的话)

假设这是在一个名为
的模块中完成的,它看起来像:

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);
            }
        }
    }
}
class\uuuu Model\u Category扩展了Mage\u Catalog\u Model\u Category{
公共函数getProductCollection(){
$collection=parent::getProductCollection()
foreach($key=>$item的集合){
如果(){
$collection->removitembykey($key);
}
}
}
}
可以是集合中项目(产品)的任何配置选项或属性


如果您只希望某个产品不在类别产品列表中列出,则更简单的解决方案是将其从类别中删除

希望我能帮忙

lg


flo

您可以扩展
Mage\u Catalog\u Model\u类别的
getProductCollection
方法(然后创建自定义块类以替换某些模板,如果愿意)

假设这是在一个名为
的模块中完成的,它看起来像:

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);
            }
        }
    }
}
class\uuuu Model\u Category扩展了Mage\u Catalog\u Model\u Category{
公共函数getProductCollection(){
$collection=parent::getProductCollection()
foreach($key=>$item的集合){
如果(){
$collection->removitembykey($key);
}
}
}
}
可以是集合中项目(产品)的任何配置选项或属性


如果您只希望某个产品不在类别产品列表中列出,则更简单的解决方案是将其从类别中删除

希望我能帮忙

lg


flo

我知道在表示层添加业务逻辑不是一个好的做法,但无论如何,这是一种将产品列表限制在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');
}

如果($\u销售类型=='无价格')
{
echo(“你想要什么就做什么”);
}

我知道在表示层添加业务逻辑不是一种好的做法,但无论如何,这是一种将产品列表限制在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');
}

如果($\u销售类型=='无价格')
{
echo(“你想要什么就做什么”);
}

我们最终没有创建新模块就解决了这个问题。我们在/app/code/local/mage/catalog/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/code/local/mage/catalog/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

为什么要将它们添加到类别?为什么要将它们添加到类别?如果您只希望某个产品不在类别产品列表中列出,则更简单的解决方案是将它们从类别中删除。是的,这是显而易见的解决方案:)您能告诉我扩展和创建自定义类的步骤吗?或者为我指出另一个指导您完成此过程的资源方向?可以在此处找到自定义模块教程:
http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/custom_module_with_custom_database_table
我试图避免创建自定义模块。。。因此,我正在我的列表中尝试以下操作$_helper=$this->helper('catalog/output')$_productCollection->addAttributeToSelect(数组(“状态”、“可见性”、“web可用性”)$_productCollection->addAttributeToFilter('status',1)$_productCollection->addAttributeToFilter('visibility',4)$_productCollection->addAttributeToFilter('web_可用性',数组('gteq'=>524));但结果并不是通过网络的可用性来过滤的。是否有一些语法我遗漏了?更简单的解决方案是,如果您只想让某个产品不在类别产品列表中列出,就将它们从类别中删除。是的,这是显而易见的解决方案:)您能告诉我扩展和创建自定义类的步骤吗?或者为我指出另一个指导您完成此过程的资源方向?可以在此处找到自定义模块教程:
http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/custom_module_with_custom_database_table
我试图避免创建自定义模块。。。因此,我正在我的列表中尝试以下操作$_helper=$this->helper('catalog/output')$_productCollection->addAttributeToSelect(数组(“状态”、“可见性”、“web可用性”)$_productCollection->addAttributeToFilter('status',1)$_productCollection->addAttributeToFilter('visibility',4)$_productCollection->addAttributeToFilter('web_可用性',数组('gteq'=>524));但结果并不是通过网络的可用性来过滤的。有没有我遗漏的语法?