diff --git a/app/code/local/Everlytic/Productapi/Model/Api2/Product/Rest/Admin/V1.php b/app/code/local/Everlytic/Productapi/Model/Api2/Product/Rest/Admin/V1.php index 2c99c62..d6bf0f2 100644 --- a/app/code/local/Everlytic/Productapi/Model/Api2/Product/Rest/Admin/V1.php +++ b/app/code/local/Everlytic/Productapi/Model/Api2/Product/Rest/Admin/V1.php @@ -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($_GET['search_query']) : array(); } /** @@ -88,4 +77,29 @@ private function getGalleryFromProduct(Mage_Catalog_Model_Product $product) } return $gallery; } + + /** + * @param $searchQuery + * @return mixed + */ + private function handleSearch($searchQuery) + { + $collection = Mage::getResourceModel('catalog/product_collection'); + $collection->addAttributeToSelect(array_keys( + $this->getAvailableAttributes($this->getUserType(), Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_READ) + )); + $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(); + } }