簡體   English   中英

從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,

弗洛

我們最終在沒有創建新模塊的情況下解決了這個問題。 我們在/ 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中添加了相同的邏輯,因此它將濾除不可見的產品。

布拉德

我知道在表示層添加業務邏輯不是一種好習慣,但是無論如何,要通過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');
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM