duanbi8529 2019-02-15 21:50
浏览 8

从其他类别获得类似产品

currently when a user searches for something on my site with a specific category and no results come up, a message appears saying there are no results. This is fine, but I would like to search on other categories to see if the product does in fact exist in another category.

This is my code so far:

<?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; ?>

The problem is I keep getting the same recommended results despite changing the search term. Anyone know why this is happening?

Any help is greatly appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源
    • ¥15 安卓JNI项目使用lua上的问题
    • ¥20 RL+GNN解决人员排班问题时梯度消失
    • ¥60 要数控稳压电源测试数据
    • ¥15 能帮我写下这个编程吗
    • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
    • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错