2013-02-21 122 views
0

您能帮我解决一下如何将有源滤波器应用于产品集合吗?如何使用有源过滤器过滤产品集合?

例如:

$_category = Mage::getModel('catalog/category')->load($category_id); 
$productsCollection = $_category->getProductCollection(); 

抓取有源滤波器:

$_filters = Mage::getSingleton('catalog/layer')->getState()->getFilters(); 

如何过滤使用$ _filters产品在$ productsCollection?

感谢您的关注!

回答

1

Magento将过滤器应用于函数Mage_Catalog_Model_Resource_Layer_Filter_Attribute::applyFilterToCollection中的集合。继它的逻辑,你可以使用此代码:

$_filters = Mage::getSingleton('catalog/layer')->getState()->getFilters(); 
$productsCollection = $_category->getProductCollection(); 
foreach ($_filters as $filterItem) { 
    $filter = $filterItem->getFilter(); /**Mage_Catalog_Model_Layer_Filter_Attribute**/ 
    $attribute = $filter->getAttributeModel(); 
    $connection = Mage::getSingleton('core/resource')->getConnection('core_read'); 
    $tableAlias = $attribute->getAttributeCode() . '_idx'; 
    $conditions = array(
     "{$tableAlias}.entity_id = e.entity_id", 
     $connection->quoteInto("{$tableAlias}.attribute_id = ?", 
     $attribute->getAttributeId()), 
     $connection->quoteInto("{$tableAlias}.store_id = ?", $collection->getStoreId()), 
      $connection->quoteInto("{$tableAlias}.value = ?", $value) 
     ); 

    $productsCollection->getSelect()->join(
     array($tableAlias => $filter->getResource()->getMainTable()), 
     implode(' AND ', $conditions), 
     array() 
     ); 

} 
+0

谢谢你的回答。你让我更接近解决我的问题:) 我还有几个问题: 1.据我了解,$ value是过滤器的值。我怎么才能得到它? 2.如果我调用$ filter-> getResource() - > getMainTable(),则会导致以下错误: 致命错误:调用Z:\ home \ mysite中的非对象的成员函数getMainTable()如果我打印出 var_dump($ filter-> getResource());如果我打印出来的数据,我们可以使用var_dump($ filter-> getResource())。 然后我看到 NULL 你能帮忙吗? 谢谢! – user2096044 2013-02-22 12:26:24

+0

而不是'$ filter-> getResource() - > getMainTable()''你可以使用'Mage :: getResourceModel('catalog/layer_filter_attribute') - > getMainTable();'。这应该返回表名“catalog_product_index_eav” – 2013-02-22 12:56:33

+0

在分层导航过滤的情况下,从请求对象中获取过滤值。您的请求是否具有带过滤器值的参数?如果是的话,你可以通过'Mage :: app() - > getRequest() - > getParam($ attribute-> getAttributeCode())来访问它' – 2013-02-22 13:23:06

1

选择一个类别,当你的错误是过滤对象是Mage_Catalog_Model_Layer_Filter_Category类型的和没有财产attribute_model。您必须添加一个检查滤波回路,以防止这种情况:

foreach($_filters as $filterItem) 
{ 
    $filter = $filterItem->getFilter(); 
    if (!($filter instanceof Mage_Catalog_Model_Layer_Filter_Attribute)) { 
     continue; 
    } 

有多个选择过滤器值的问题也可以解决两种方式。如果你想在一时尚应用的过滤器值,这样做:

$connection->quoteInto("{$tableAlias}.value = in(?)", explode('_', Mage::app()->getRequest()->getParam($attribute->getAttributeCode()))) 

如果过滤器值必须在与时尚应用,程序更复杂一点。基本上你必须执行多个连接到同一个表,并且必须为每个连接提供唯一的表别名:

$connection = Mage::getSingleton('core/resource')->getConnection('core_read'); 
    $tableAlias = $attribute->getAttributeCode() . '_idx'; 

    $values = explode('_', Mage::app()->getRequest()->getParam($attribute->getAttributeCode())); 

    foreach ($values as $value) { 
     $tableAliasModified = $tableAlias.'_'.$value; 
     $conditions = array(
      "{$tableAliasModified}.entity_id = e.entity_id", 
      $connection->quoteInto("{$tableAliasModified}.attribute_id = ?", 
      $attribute->getAttributeId()), 
      $connection->quoteInto("{$tableAliasModified}.store_id = ?", Mage::app()->getStore()->getStoreId()), 
       $connection->quoteInto("{$tableAliasModified}.value = ?", $value) 
      ); 

     $productsCollection->getSelect()->join(
      array($tableAliasModified => Mage::getResourceModel('catalog/layer_filter_attribute')->getMainTable()), 
      implode(' AND ', $conditions), 
      array() 
      ); 
    } 
+0

差不多完成了,你能帮我解决我的最后一个问题吗? – user2096044 2013-02-27 10:28:57