我想在magento
中搜索特定商店的产品,并希望以编程方式获取数组中的所有product ids
。比如下面的方法,它将$searchstring
作为参数,return $ids
数组包含所有产品ID的产品ID,其中产品名称包含search string
。
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
并且搜索字符串 temp 然后它应该返回1,2,3,4,6而不是5,因为 temp 与具有{{1的产品名称不匹配}}。
答案 0 :(得分:9)
您始终可以使用'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;
}
答案 1 :(得分:-1)
我已经实现了这个,但我只得到一个结果。`$ids = array();
// Code to Search Product by $searchstring and get Product IDs
//$searchstring='Strive Shoulder Pack';
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToSelect('*');
$productCollection->addAttributeToFilter('name', array('like' => '%'.$searchstring.'%'));
/*$productCollection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH); */
/*$productCollection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);*/
$productCollection->load();
foreach ($productCollection as $product) {
$imageUrl = $this->_imageHelper->init($product, 'product_page_image_small')
->setImageFile($product->getSmallImage())->getUrl();
$ids['1']['product_id'] = $product->getId();
$ids['1']['product_name'] = $product->getName();
$ids['1']['product_image'] = $imageUrl;
$ids['1']['product_description'] = $product->getDescription();
$ids['1']['product_price'] = $product->getPrice();
$ids['1']['rating'] = $this->getProductRatingCount($product);
}`