Algolia_Algoliasearch_Helper_Entity_Categoryhelper::getObject PHP Method

getObject() public method

public getObject ( Mage_Catalog_Model_Category $category )
$category Mage_Catalog_Model_Category
    public function getObject(Mage_Catalog_Model_Category $category)
    {
        $storeId = $category->getStoreId();
        /** @var Algolia_Algoliasearch_Helper_Entity_Producthelper $productHelper */
        $productHelper = Mage::helper('algoliasearch/entity_producthelper');
        $collection = $productHelper->getProductCollectionQuery($storeId, null, true, true);
        $productCollection = clone $collection;
        $productCollection = $productCollection->addCategoryFilter($category);
        $category->setProductCount($productCollection->getSize());
        $transport = new Varien_Object();
        Mage::dispatchEvent('algolia_category_index_before', array('category' => $category, 'custom_data' => $transport));
        $customData = $transport->getData();
        $category->getUrlInstance()->setStore($storeId);
        $path = '';
        foreach ($category->getPathIds() as $categoryId) {
            if ($path != '') {
                $path .= ' / ';
            }
            $path .= $this->getCategoryName($categoryId, $storeId);
        }
        $data = array('objectID' => $category->getId(), 'name' => $category->getName(), 'path' => $path, 'level' => $category->getLevel(), 'url' => $category->getUrl(), 'include_in_menu' => $category->getIncludeInMenu(), '_tags' => array('category'), 'popularity' => 1, 'product_count' => $category->getProductCount());
        try {
            $imageUrl = $this->getThumbnailUrl($category) ?: $category->getImageUrl();
            if ($imageUrl) {
                /** @var Algolia_Algoliasearch_Helper_Image $imageHelper */
                $imageHelper = Mage::helper('algoliasearch/image');
                $data['image_url'] = $imageHelper->removeProtocol($imageUrl);
            }
        } catch (\Exception $e) {
            // no image, no default, not fatal
        }
        foreach ($this->config->getCategoryAdditionalAttributes($storeId) as $attribute) {
            $value = $category->getData($attribute['attribute']);
            $attribute_resource = $category->getResource()->getAttribute($attribute['attribute']);
            if ($attribute_resource) {
                $value = $attribute_resource->getFrontend()->getValue($category);
            }
            if (isset($data[$attribute['attribute']])) {
                $value = $data[$attribute['attribute']];
            }
            if ($value) {
                $data[$attribute['attribute']] = $value;
            }
        }
        $data = array_merge($data, $customData);
        foreach ($data as &$data0) {
            $data0 = $this->try_cast($data0);
        }
        return $data;
    }

Usage Example

Beispiel #1
0
 public function rebuildStoreCategoryIndexPage($storeId, $collectionDefault, $page, $pageSize, $emulationInfo = null)
 {
     if ($this->config->isEnabledBackend($storeId) === false) {
         $this->logger->log('INDEXING IS DISABLED FOR ' . $this->logger->getStoreName($storeId));
         return;
     }
     $emulationInfoPage = null;
     if ($emulationInfo === null) {
         $emulationInfoPage = $this->startEmulation($storeId);
     }
     $collection = clone $collectionDefault;
     $collection->setCurPage($page)->setPageSize($pageSize);
     $collection->load();
     $index_name = $this->category_helper->getIndexName($storeId);
     $indexData = array();
     /** @var $category Mage_Catalog_Model_Category */
     foreach ($collection as $category) {
         if (!$this->category_helper->isCategoryActive($category->getId(), $storeId)) {
             continue;
         }
         $category->setStoreId($storeId);
         $category_obj = $this->category_helper->getObject($category);
         if ($category_obj['product_count'] > 0) {
             array_push($indexData, $category_obj);
         }
     }
     if (count($indexData) > 0) {
         $this->algolia_helper->addObjects($indexData, $index_name);
     }
     unset($indexData);
     $collection->walk('clearInstance');
     $collection->clear();
     unset($collection);
     if ($emulationInfo === null) {
         $this->stopEmulation($emulationInfoPage);
     }
 }