Pergunta

I'm using catalogsearch/query to search the products in our database, but this is also giving back child products of configurable products. Because those products don't get returned by catalog/product I get inconsistencies between the total product count (which comes from catalogsearch/query) and the actual products (which come from catalog/product because I need additional attributes for the products). I have been looking in the database but I can't seem to find a property which indicates that a product is a child product of a configurable product. I did find the option to check if a product has a parent product, I could use that to filter out those products, but this is not a solution because the total product count would still be inaccurate. We need this total product count to precalulate the size of a view.

This is my current search code:

// apply search
if (!empty($search)) {
    $catalogSearchModel = Mage::getModel('catalogsearch/query')->setQueryText($search);
    $catalogSearchModelCollection = $catalogSearchModel->getResultCollection();

    $category = Mage::getModel('catalog/category')->load($categoryId);
    $catalogSearchModelCollection->addCategoryFilter($category);

    if (!empty($range)) {
        $catalogSearchModelCollection->getSelect()->limit(($range[1] + $range[0]), $range[0]);
    } else {
        $catalogSearchModelCollection->getSelect()->limit(100);
    }
    $data = $catalogSearchModelCollection->getData();

    $productCount = $catalogSearchModelCollection->getSize();

    if (empty($data)) {
        // No products found to filter from, set some imposible `where` to make sure it doesn't return products
        $collection->addAttributeToFilter(array(array("attribute" => "entity_id", "eq" => 0)));
    } else {
        $filters = array();
        foreach ($data as $key => $value) {
            $filters[] = array("attribute" => "entity_id", "eq" => $value["entity_id"]);
        }

        $productCollection->addAttributeToFilter($filters);
    }
}

So in short, how can I add a filter to the search collection which excludes child products?

Foi útil?

Solução

I believe you want to add this filter which limits your results to only those products that are enabled and visible in search. That should get the counts to match.

->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
->addAttributeToFilter('visibility', array('in' => array(
    Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH,
    Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
))
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top