从另一个类别中获取类似的产品

时间:2019-02-15 21:50:41

标签: php magento-1.9

当前,当用户在我的网站上搜索具有特定类别的内容,但未显示任何结果时,出现一条消息,提示没有结果。很好,但是我想搜索其他类别以查看产品是否确实存在于其他类别中。

到目前为止,这是我的代码:

<?php
    // Category where the user searched
    $current_category = Mage::getModel('catalog/category')->load($_GET["cat"]);

    // Categories where we are going to search in
    $_categories      = Mage::getModel('catalog/category')->load(26); // Root category
    $_categories      = explode(",", $_categories->getChildren()); // Get second level categories

    foreach($_categories as $category_id):
        // Load the category
        $category = Mage::getModel('catalog/category')->load($category_id);

        // Perform text search
        $query = Mage::getModel('catalogsearch/query')->loadByQueryText($_GET["q"]);
        $query->setStoreId(4);

        $fulltextResource = Mage::getResourceModel('catalogsearch/fulltext')->prepareResult(
            Mage::getModel('catalogsearch/fulltext'),
            $_GET["q"],
            $query
        );

        $collection = Mage::getResourceModel('catalog/product_collection');
        $collection->addAttributeToSelect('*');
        $collection->getSelect()->joinInner(
            array('search_result' => $collection->getTable('catalogsearch/result')),
            $collection->getConnection()->quoteInto(
                'search_result.product_id=e.entity_id AND search_result.query_id=?',
                $query->getQueryId()
            ),
            array('relevance' => 'relevance')
        );
        $collection->addFieldToFilter('mostrar_inventario', array('finset' => 653));
        $collection->addCategoryFilter($category);
        $collection->addAttributeToSort('popularity', "desc");
        $collection->setPageSize(10)->setCurPage(1);

        // If it found results in this category break the loop.
        if ($collection) {
            $categoryDidFind = $category->getName();
            break;
        }
    endforeach;
?>

<h3>No results for <?php echo $_GET["q"]; ?> in <?php echo $current_category->getName(); ?>, but we did find it in <?php echo $categoryDidFind; ?></h3>
<?php foreach($collection as $product): ?>
    <?php echo $product->getName() . "<br>"; ?>
<?php endforeach; ?>

问题是尽管更改了搜索词,但我仍然得到相同的推荐结果。有人知道为什么会这样吗?

任何帮助将不胜感激。

0 个答案:

没有答案