簡體   English   中英

在magento中按名稱搜索產品並獲取產品ID數組

[英]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,因為tempid = 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM