Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
Update-10-2-2019 - Fix to allow for search, if no search return empty…
Browse files Browse the repository at this point in the history
… and limit search result to 10
  • Loading branch information
ade committed Oct 14, 2019
1 parent 7362d35 commit b15039a
Showing 1 changed file with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,7 @@ protected function _prepareProductForResponse(Mage_Catalog_Model_Product $produc
*/
protected function _retrieveCollection()
{
/** @var $collection Mage_Catalog_Model_Resource_Product_Collection */
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addAttributeToSelect(array_keys(
$this->getAvailableAttributes($this->getUserType(), Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_READ)
));
$products = $collection->load();

foreach ($products as $product) {
$this->_prepareProductForResponse($product);
}

return $products->toArray();
return (isset($_GET['search_query']) && ($_GET['search_query'] !== '')) ? $this->handleSearch() : array();
}

/**
Expand All @@ -88,4 +77,29 @@ private function getGalleryFromProduct(Mage_Catalog_Model_Product $product)
}
return $gallery;
}

/**
* @return mixed
*/
private function handleSearch()
{
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addAttributeToSelect(array_keys(
$this->getAvailableAttributes($this->getUserType(), Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_READ)
));
$searchQuery = $_GET['search_query'];
$collection->addFieldToFilter(array(
array('attribute' => 'name', array('like' => '%' . $searchQuery . '%')),
array('attribute' => 'sku', array('like' => '%' . $searchQuery . '%'))
)
);
$collection->setPageSize(10)->setCurPage(1);
$products = $collection->load();

foreach ($products as $product) {
$this->_prepareProductForResponse($product);
}

return $products->toArray();
}
}

0 comments on commit b15039a

Please sign in to comment.