2014-03-07 83 views
1

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

我已经尝试过不同的方法,但它似乎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

感谢,感谢任何回答这个问题:-)

回答

0

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

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

UPDATE

  • 创建模块xml文件

应用的/ etc /模块/ Custom_Products.xml

如下

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

应用程序/代码/本地/自定义/产品的/ etc/config.xml中

文件如下

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

应用程序/代码/本地/ 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); 
    } 

} 
?> 
  • 创建

应用程序/设计/ YOUR_PACKAGE/YOUR_THEME /模板/目录/产品/ customlist。PHTML

如下

<?php  
    $_productCollection=$this->_collection; 
    ... 
    your products code here 
    ...  

?> 

<?php 
    echo $this->getChildHtml('pager'); 
?> 

检查,如果它是为你工作。

+0

你好Dushyant,这种方法会有道理,但因为我不完全是一个magento专家,你可以帮助我通过这个或给我看一些简单的步骤如何做到这一点? – vikingen

+0

检查我更新的答案 –

相关问题