[英]Search Product by name in magento and get array of product ids
我想在magento
搜索特定商店的產品,並希望以編程方式獲取陣列中的所有product ids
。 類似下面的方法,它將$searchstring
作為參數並return $ids
數組,其中包含產品名稱包含search string
的所有產品的產品ID。
function getProductIdsBySearch($searchstring, $storeId) {
$ids = array();
//
// Code to Search Product by $searchstring and get Product IDs
//
return $ids;
}
喜歡: - 如果我們在目錄上有以下產品
ID Product Name
1 Temp
2 ProductTemp
3 ProductTempData
4 ABCTEMPXYZ
5 ABCXYZ
6 Tempdata
並且搜索字符串是臨時的,那么它應該返回1,2,3,4,6而不是5,因為temp與id = 5
的產品名稱不匹配。
您始終可以使用'like'進行過濾查詢。
試試看...
function getProductIdsBySearch($searchstring, $storeId = '') {
$ids = array();
// Code to Search Product by $searchstring and get Product IDs
$product_collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('name', array('like' => '%'.$searchstring.'%'))
->load();
foreach ($product_collection as $product) {
$ids[] = $product->getId();
}
//return array of product ids
return $ids;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.