【问题标题】:Show price range in homepage in Magento在 Magento 的主页中显示价格范围
【发布时间】:2013-08-01 21:53:10
【问题描述】:

我试图在我的 Magento 网站主页的侧边栏中显示价格范围。但是,默认情况下 Magento 已为价格范围设置类别过滤器,因此如果没有类别 ID,则价格范围将不起作用。有没有办法实现这个功能?

我想出了创建一个新页面来显示产品列表的想法。问题是我不知道如何在我的过滤器上设置价格范围。这是我的基本想法:

$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('price_to', '4000')
    ->addAttributeToFilter('price_from', '1000');

然后显示符合条件的产品,但由于显而易见的原因,我不能使用 price_to 和 price_from。有人可以指出我正确的方向吗?提前致谢。

【问题讨论】:

  • 不能通过分层导航来完成吗。- System->Configuration->Catalog->Catalog->分层导航
  • 好吧,这仅适用于类别页面,但我想在主页中添加价格范围。

标签: php magento


【解决方案1】:

所以我做了很多研究,但我的案例没有理想的结果,所以我创建了一个类似于 list.phtml 的新 phtml 文件,我在其中使用如下过滤器调用产品集合:

//this is dirty but this is the fastest solution I came up with
$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 

if(false != strpos($url, 'to=500')):
$_below = 500;
elseif(false != strpos($url, 'to=1000')):
$_to = 1000;
$_from = 500;
elseif(false != strpos($url, 'to=2000')):
$_to = 2000;
$_from = 1001;
elseif(false != strpos($url, 'from=2001')):
$_above = 2000;
endif;

if(false != strpos($url, 'to=500')):
$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('price', array(
        'lteq' => $_below));
elseif(false != strpos($url, 'from=2001')):
$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('price', array(
        'gteq' => $_above));
else:
$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('price', array(
        'from' => $_from,
        'to' => $_to)); 
endif;

我在哪里设置 $_from 和 $_to 取决于选择的范围链接。然后,我在专门为其添加的 CMS 页面中调用 custom.phtml:

{{block type="catalog/product_list" template="catalog/layer/customrange.phtml"}}

然后,在边栏上,我添加了必要的范围(硬编码),如下所示:

<ul><li><a href="<?php echo $this->getUrl('custom-page').'?from=&to=500'; ?>">below 500</a></li></ul> 

所以我现在有了没有类别过滤器的价格范围。我知道这并不完美,鉴于我的侧边栏上的范围链接是硬编码的,而且我在分页和工具栏上遇到了许多问题(说实话,它根本不起作用) 但这是我能想出的最快的解决方案并且它有效。如果有人有更好的解决方案,请随时告诉我,希望这对其他人有帮助。

【讨论】:

  • 好主意!!!我知道它会起作用,但你能分享完整的代码吗?可以节省构建模块的时间
  • 实际上,这总结了我用我的代码所做的几乎所有事情,但无论如何,我正在更新我的答案,并提供更多细节。
猜你喜欢
  • 2011-06-26
  • 2011-12-04
  • 2013-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-30
相关资源
最近更新 更多