【问题标题】:Magento - How to display products below no results page?Magento - 如何在无结果页面下方显示产品?
【发布时间】:2014-04-10 00:09:26
【问题描述】:

基本上,如果用户搜索产品并且搜索返回 emtpy,我希望能够在“没有与您的搜索匹配的结果”文本下显示特定类别。

我已经尝试了不同的方法,但似乎 results.phtml 会从搜索查询字符串中对每个不匹配的产品进行排序。字符串是否匹配某组产品无关紧要 - 它只会显示这些产品。

我正在尝试使用简单的代码:

<?php echo $this->getLayout()->createBlock('catalog/product_list')->setCategoryId(4)->setTemplate('catalog/product/list.phtml')->toHtml() ?>

这段代码可以在除 results.phtml 之外的模板页面上的任何位置使用,因为搜索功能会阻止所有不匹配的产品。 (如果它与使用此代码块的搜索匹配,它将显示一个产品)。

这里有类似的问题,但没有解决方案:Display specific category products on no results search page

谢谢,感谢您对此问题的任何回答 :-)

【问题讨论】:

    标签: php html magento magento-1.8


    【解决方案1】:

    您可以如下创建自定义块并设置分页如here

    <?php echo $this->getLayout()->createBlock('yourmodule/product_customlist')->setTemplate('catalog/product/list.phtml')->toHtml() ?>
    

    更新

    • 创建模块 xml 文件

    app/etc/modules/Custom_Products.xml

    如下

    <config>
        <modules>
            <Custom_Products>
                <active>true</active>
                <codePool>local</codePool>
            </Custom_Products>
        </modules>
    </config> 
    
    • 创建

    app/code/local/Custom/Products/etc/config.xml

    文件如下

    <config>
        <global>
            <blocks>
                <customproducts>
                    <class>Custom_Products_Block</class>
                </customproducts>
            </blocks>
        </global>
    </config>
    
    • 创建

    app/code/local/Custom/Products/Block/Customlist.php

    文件如下

    <?php
    class Custom_Products_Block_Customlist extends Mage_Core_Block_Template
    {
    
        protected function _construct()
        {
            parent::_construct();
    
            // We get our collection through our model
            $this->_collection = Mage::getModel('catalog/category')->load($category_id)
            ->getProductCollection()
            ->addAttributeToSelect('*') // add all attributes - optional
            ->addAttributeToFilter('status', 1) // enabled
            ->addAttributeToFilter('visibility', 4) //visibility in catalog,search
            ->setOrder('price', 'ASC'); //sets the order by price
    
            // Instantiate a new Pager block
            $pager = new Mage_Page_Block_Html_Pager();
    
    
            // We set our limit (here an integer store in configuration).
            // /!\ The limit must be set before the collection
            $pager
            ->setLimit(5)
            ->setCollection($this->_collection);
    
            // Add our Pager block to our current list block
            $this->setChild('pager', $pager);
        }
    
    }
    ?>
    
    • 创建

    app/design/YOUR_PACKAGE/YOUR_THEME/template/catalog/product/customlist.phtml

    如下

    <?php    
        $_productCollection=$this->_collection;
        ...
        your products code here
        ...     
    
    ?>
    
    <?php   
       echo $this->getChildHtml('pager');
    ?>
    

    检查它是否适合你。

    【讨论】:

    • 您好 Dushyant,这种方法是有道理的,但由于我不是 magento 专家,您能否帮助我完成此操作或向我展示如何执行此操作的一些简单步骤?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多