[英]i want to search product in magento by name and category id
i want to make custom search query where i want to search product by it's name using product name like query and category it : 我想进行自定义搜索查询,在其中我要使用产品名称(例如查询)对产品进行名称搜索并对其进行分类:
i tried below code but its now working 我尝试下面的代码,但它现在可以工作
$searchstring='comp';
$product_collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('category_id', array('in' => array('finset' => '141')));
->addAttributeToFilter('name', array('like' => '%'.$searchstring.'%'))
->load();
foreach ($product_collection as $product) {
echo $product->getName().'<br>';
}
please guide my how can i do this 请指导我该怎么做
You can use addCategoryFilter
to filter a category. 您可以使用
addCategoryFilter
过滤类别。
$searchstring='comp';
$category = Mage::getModel('catalog/category')->load(141);
$product_collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addCategoryFilter($category)
->addAttributeToFilter('name', array('like' => '%'.$searchstring.'%'));
foreach ($product_collection as $product) {
echo $product->getName().'<br>';
}
I'm don't have knowlegde about magento, but does this code work in PHP? 我没有关于magento的知识,但是这段代码可以在PHP中工作吗? Maybe it's not a syntax fault, but have you already tried something like this:
也许这不是语法错误,但您是否已经尝试过以下方法:
$product_collection = Mage::getResourceModel('catalog/product_collection');
$product_collection->addAttributeToSelect('*');
$product_collection->addAttributeToFilter('category_id', array('in' => array('finset' => '141')));
$product_collection->addAttributeToFilter('name', array('like' => '%'.$searchstring.'%'));
$product_collection->load();
may be this code bellow can help you : 也许下面的代码可以帮助您:
$product_collection = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addAttributeToFilter('category_id', array(
array('finset' => $id1),
array('finset' => $id2))
)
->addAttributeToFilter('name', array('like' => '%'.$searchstring.'%'));
Searching product by category
In "advanced search" feature, searching product by category is not the default setting. But we can do this by modifying the foloowing files:
app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php
app/code/core/Mage/CatalogSearch/Model/Advanced.php
app/design/yourdesign/yourdesign/template/catalogsearch/advanced/form.phtml
At the bottom of app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php we can add following codes:
public function getStoreCategories()
{
$helper = Mage::helper('catalog/category');
return $helper->getStoreCategories();
}
In app/code/core/Mage/CatalogSearch/Model/Advanced.php, replace the getSearchCriterias() by following functions:
public function getSearchCriterias()
{
$search = $this->_searchCriterias;
/* display category filtering criteria */
if(isset($_GET['category']) && is_numeric($_GET['category'])) {
$category = Mage::getModel('catalog/category')->load($_GET['category']);
$search[] = array('name'=>'Category','value'=>$category->getName());
}
return $search;
}
Replace the getProductCollection() by following function:
public function getProductCollection(){
if (is_null($this->_productCollection)) {
$this->_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addMinimalPrice()
->addStoreFilter();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection);
/* include category filtering */
if(isset($_GET['category']) && is_numeric($_GET['category'])) $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['category']),true);
}
return $this->_productCollection;
}
In page app/design/yourdesign/yourdesign/template/catalogsearch/advanced/form.phtml, find the codes as following:
<input name="<?php echo $_code ?>" id="<?php echo $_code ?>" value="<?php echo $this->htmlEscape($this->getAttributeValue($_attribute)) ?>" title="<?php echo $this->htmlEscape($this->getAttributeLabel($_attribute)) ?>" class="input-text <?php echo $this->getAttributeValidationClass($_attribute) ?>" type="text" />
<?php endswitch; ?>
</li>
<?php endforeach; ?>
at the bottom of this piece of codes, add following codes:
<li>
<label for="category_search_field">Search by Category:</label>
<select name="category" id="category_search_field">
<option value="">-- Any Category --</option>
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php if($_category->hasChildren()): ?>
<option class="parent-cat" value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
<?php foreach ($_category->getChildren() as $subcategory):
if($subcategory->getIsActive()) : ?>
<option value="<?php echo $subcategory->getId(); ?>"<?php echo ($this->getRequest()->getQuery('category') == $subcategory->getId() ? ' selected="selected"': "") ?>><?php echo $subcategory->getName(); ?></option>
<?php endif; endforeach; ?>
<?php elseif($_category->getIsActive()): ?>
<option value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>
<?php endif; ?>
<?php endforeach ?>
</select>
</li>
Same as above you can add change for product name filter also... 与上述相同,您还可以为商品名称过滤器添加更改...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.