gpt4 book ai didi

magento - 从 Magento 中的自定义 block 调用 CatalogSearch 的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-02 21:27:09 25 4
gpt4 key购买 nike

我正在编写一个 Controller ,旨在向 API 调用公开 Magento 的目录搜索功能。使用默认 REST API 的过滤器不会返回与 Magento 网站上可用的搜索功能相同质量的结果。

我已经挖掘了几天了,我在 Stack Overflow 和 Magento 讨论板上看到的各种方法似乎不起作用,我是否缺少一个步骤?

这两种方法都返回 null,我似乎无法弄清楚为什么:

 $query = Mage::helper('catalogSearch')->getQuery();

$searcher = Mage::getSingleton('catalogsearch/advanced')->addFilters(
array('name'=> $query->getQueryText(),
'description' => $query->getQueryText()));



$obj = new stdClass();
$obj->query = $query->getQueryText();
$obj->results = $searcher->getProductCollection(); //nothing returned

该方法也没有从 this SO question 修改而来似乎有效:

 $query = Mage::helper('catalogSearch')->getQuery();

$obj = new stdClass();
$obj->query = $query->getQueryText();

if ($query->getQueryText()) {
if (Mage::helper('catalogSearch')->isMinQueryLength()) {
$query->setId(0)->setIsActive(1)->setIsProcessed(1);
} else {
if ($query->getId()) {
$query->setPopularity($query->getPopularity()+1);
} else {
$query->setPopularity(1);
}
$query->prepare();
}
$obj->results = $query->getProductCollection(); //null
}

我缺少哪些步骤才能成功调用 Magento 的目录搜索模块并获取结果集合?

最佳答案

Magento 的搜索模型有点复杂,因为他们构建了一种机制来保存查询和结果以进行缓存和统计。因此,您需要准备查询对象,然后准备结果,然后可以将产品集合与搜索结果表连接起来。以下是如何在代码中执行此操作:

$searchText = 'ipad';
$query = Mage::getModel('catalogsearch/query')->setQueryText($searchText)->prepare();
$fulltextResource = Mage::getResourceModel('catalogsearch/fulltext')->prepareResult(
Mage::getModel('catalogsearch/fulltext'),
$searchText,
$query
);

$collection = Mage::getResourceModel('catalog/product_collection');
$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->getId()
),
array('relevance' => 'relevance')
);

$collection 是经过筛选的目录产品集合,您可以根据需要执行任何其他操作:

$collection->setStore(Mage::app()->getStore());
$collection->addMinimalPrice();
$collection->addFinalPrice();
$collection->addTaxPercents();
$collection->addStoreFilter();
$collection->addUrlRewrite();

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);

关于magento - 从 Magento 中的自定义 block 调用 CatalogSearch 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20978893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com