在magento中按名称搜索产品并获取产品ID数组

Sha*_*shi 5 php magento magento-1.7

我想搜索特定商店的产品,magento并希望以product ids编程方式获取所有数组.像下面的方法,它$searchstring作为参数和return $ids数组,其中包含产品名称包含的所有产品的产品ID search string.

function getProductIdsBySearch($searchstring, $storeId) {
     $ids = array();
     //
     // Code to Search Product by $searchstring and get Product IDs
     //
     return $ids;
}
Run Code Online (Sandbox Code Playgroud)

喜欢: - 如果我们在目录上有以下产品

ID      Product Name  
1        Temp   
2        ProductTemp   
3        ProductTempData  
4        ABCTEMPXYZ  
5        ABCXYZ  
6        Tempdata  
Run Code Online (Sandbox Code Playgroud)

并且搜索字符串是临时的,那么它应该返回1,2,3,4,6而不是5,因为temp与产品的名称不匹配id = 5.

das*_*hbh 10

您始终可以使用'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;
}
Run Code Online (Sandbox Code Playgroud)