/** * Get direct URL to category * * @param Mage_Catalog_Model_Category $category * @return string */ protected function _getDirectUrl(Mage_Catalog_Model_Category $category) { /** @var $helper Enterprise_Catalog_Helper_Data */ $helper = $this->_factory->getHelper('enterprise_catalog'); $requestPath = $helper->getCategoryRequestPath($category->getRequestPath(), $category->getStoreId()); return $this->getUrlInstance()->getDirectUrl($requestPath); }
/** * Force the virtual rule to be loaded for a category. * * @param Mage_Catalog_Model_Category $category The category. * * @return Smile_VirtualCategories_Model_Rule */ public function getVirtualRule($category) { $virtualRule = $category->getVirtualRule(); if (!is_object($virtualRule)) { $cacheKey = $category->getId(); if ($category->getStoreId()) { $cacheKey = $cacheKey . '_' . $category->getStoreId(); } if (!isset($this->_categoryRulesCache[$cacheKey])) { $backend = Mage::getSingleton('smile_virtualcategories/category_attributes_backend_virtual'); $backend->afterLoad($category); $this->_categoryRulesCache[$cacheKey] = $category->getVirtualRule(); } $virtualRule = $this->_categoryRulesCache[$cacheKey]; } return $virtualRule; }
/** * Reindex a single virtual category. * * @param Mage_Catalog_Model_Category $category The category. * * @return void */ public function reindex($category) { /** Reindex all data from virtual categories products positions index */ $engine = Mage::helper('catalogsearch')->getEngine(); $mapping = $engine->getCurrentIndex()->getMapping('product'); $dataprovider = $mapping->getDataProvider('virtual_categories_products_position'); $dataprovider->updateAllData($category->getStoreId(), $category->getVirtualProductIds()); }
/** * Get products count in category * * @param Mage_Catalog_Model_Category $category * @return integer */ public function getProductCountExcludeOutStock($category) { $read = $this->_getReadAdapter(); $store_data = Mage::getModel('core/store')->load($category->getStoreId()); //load store object $website_id = $store_data->getWebsiteId(); //get website id from the store $select = $read->select()->from(array('main_table' => $this->getTable('catalog/category_product')), "COUNT(main_table.product_id)")->joinLeft(array('stock' => $this->getTable('cataloginventory/stock_status')), 'main_table.product_id=stock.product_id AND ' . $read->quoteInto('stock.website_id=? ', $website_id), array())->where("main_table.category_id = ?", $category->getId())->where("round(stock.qty) > 0 ")->where("stock.stock_status = ? ", 1)->group("main_table.category_id"); //echo $select->__toString(); exit; return (int) $read->fetchOne($select); }
/** * Unserializes the virtual category configuration after it has been loaded. * * @param Mage_Catalog_Model_Category $object Category saved. * * @return Smile_VirtualCategories_Model_Category_Attributes_Backend_Virtual */ public function afterLoad($object) { $attributeCode = $this->getAttribute()->getName(); $data = $object->getData($attributeCode); if ($data && is_string($data) && strlen($data)) { $data = unserialize($data); } else { $data = $this->_defaultValue; } $virtualCategoryRule = Mage::getModel('smile_virtualcategories/rule'); if (isset($data['rule_serialized'])) { $virtualCategoryRule->getConditions()->setConditions(array())->loadArray($data['rule_serialized']); if ($object->getStoreId()) { $virtualCategoryRule->setStoreId($object->getStoreId()); } } $virtualCategoryRule->setCategory($object); $object->setData('is_virtual', $data['is_virtual']); $object->setData('virtual_rule', $virtualCategoryRule); return $this; }
/** * Load url rewrite based on specified category * * @param Mage_Core_Model_Abstract $object * @param Mage_Catalog_Model_Category $category * @return Enterprise_Catalog_Model_Resource_Category */ public function loadByCategory(Mage_Core_Model_Abstract $object, Mage_Catalog_Model_Category $category) { $idField = $this->_getReadAdapter()->getIfNullSql('url_rewrite_cat.id', 'default_urc.id'); $requestPath = $this->_getReadAdapter()->getIfNullSql('url_rewrite.request_path', 'default_ur.request_path'); $select = $this->_getReadAdapter()->select()->from(array('main_table' => $this->getTable('catalog/category')), array($this->getIdFieldName() => $idField))->where('main_table.entity_id = ?', (int) $category->getId())->joinLeft(array('url_rewrite_cat' => $this->getTable('enterprise_catalog/category')), 'url_rewrite_cat.category_id = main_table.entity_id AND url_rewrite_cat.store_id = ' . (int) $category->getStoreId(), array(''))->joinLeft(array('url_rewrite' => $this->getTable('enterprise_urlrewrite/url_rewrite')), 'url_rewrite.url_rewrite_id = url_rewrite_cat.url_rewrite_id', array(''))->joinLeft(array('default_urc' => $this->getTable('enterprise_catalog/category')), 'default_urc.category_id = main_table.entity_id AND default_urc.store_id = 0', array(''))->joinLeft(array('default_ur' => $this->getTable('enterprise_urlrewrite/url_rewrite')), 'default_ur.url_rewrite_id = default_urc.url_rewrite_id', array('request_path' => $requestPath)); $result = $this->_getReadAdapter()->fetchRow($select); if ($result) { $object->setData($result); } $this->unserializeFields($object); $this->_afterLoad($object); return $this; }
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; }
public function getObject(Mage_Catalog_Model_Category $category) { /** @var $productCollection Mage_Catalog_Model_Resource_Product_Collection */ $productCollection = $category->getProductCollection(); $productCollection = $productCollection->addMinimalPrice(); $category->setProductCount($productCollection->getSize()); $transport = new Varien_Object(); Mage::dispatchEvent('algolia_category_index_before', array('category' => $category, 'custom_data' => $transport)); $customData = $transport->getData(); $storeId = $category->getStoreId(); $category->getUrlInstance()->setStore($storeId); $path = ''; foreach ($category->getPathIds() as $categoryId) { if ($path != '') { $path .= ' / '; } $path .= $this->getCategoryName($categoryId, $storeId); } $image_url = NULL; try { $image_url = $category->getImageUrl(); } catch (Exception $e) { /* no image, no default: not fatal */ } $data = array('objectID' => $category->getId(), 'name' => $category->getName(), 'path' => $path, 'level' => $category->getLevel(), 'url' => Mage::getBaseUrl() . $category->getRequestPath(), '_tags' => array('category'), 'popularity' => 1, 'product_count' => $category->getProductCount()); if (!empty($image_url)) { $data['image_url'] = $image_url; } foreach ($this->config->getCategoryAdditionalAttributes($storeId) as $attribute) { $value = $category->getData($attribute['attribute']); $attribute_ressource = $category->getResource()->getAttribute($attribute['attribute']); if ($attribute_ressource) { $value = $attribute_ressource->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; }
/** * Return children ids of category * * @param Mage_Catalog_Model_Category $category * @param boolean $recursive * @return array */ public function getChildren($category, $recursive = true) { $attributeId = $this->_getIsActiveAttributeId(); $select = $this->_getReadAdapter()->select()->from(array('m' => $this->getEntityTable()), 'entity_id')->joinLeft(array('d' => $this->getEntityTable() . '_int'), "d.attribute_id = '{$attributeId}' AND d.store_id = 0 AND d.entity_id = m.entity_id", array())->joinLeft(array('c' => $this->getEntityTable() . '_int'), "c.attribute_id = '{$attributeId}' AND c.store_id = '{$category->getStoreId()}' AND c.entity_id = m.entity_id", array())->where('(IFNULL(c.value, d.value) = ?)', '1')->where('path LIKE ?', "{$category->getPath()}/%"); if (!$recursive) { $select->where('level <= ?', $category->getLevel() + 1); } $_categories = $this->_getReadAdapter()->fetchAll($select); $categoriesIds = array(); foreach ($_categories as $_category) { $categoriesIds[] = $_category['entity_id']; } return $categoriesIds; // $this->_getTree()->load(); // return $this->_getTree()->getChildren($category->getId(), false); }
/** * Verify if a given category has custom positions defined for products * * @param Mage_Catalog_Model_Category $category The concerned category * * @return bool */ public function hasCustomPositions($category) { $result = false; if ($category->getId()) { $adapter = $this->_getReadAdapter(); $select = $adapter->select(); $storeIds = array_unique(array_map("intval", array(Mage_Core_Model_App::ADMIN_STORE_ID, $category->getStoreId()))); $select->from(array("main_table" => $this->getMainTable())); $select->where('category_id = ?', (int) $category->getId()); $select->where('store_id IN (?)', $storeIds); $result = $adapter->fetchRow($select) !== false; } return $result; }
/** * Get store identifiers where category is presented * * @param Mage_Catalog_Model_Category $category * @return array */ public function getStoreIds($category) { if (!$category->getId()) { return array(); } $nodePath = $this->_getTree()->getNodeById($category->getId())->getPath(); $nodes = array(); foreach ($nodePath as $node) { $nodes[] = $node->getId(); } $stores = array(); $storeCollection = Mage::getModel('core/store')->getCollection()->loadByCategoryIds($nodes); foreach ($storeCollection as $store) { $stores[$store->getId()] = $store->getId(); } $entityStoreId = $category->getStoreId(); if (!in_array($entityStoreId, $stores)) { array_unshift($stores, $entityStoreId); } if (!in_array(0, $stores)) { array_unshift($stores, 0); } return $stores; }
/** * Update category url key * * @param Mage_Catalog_Model_Category $category * @param string $newUrlKey * @param int $storeId */ function processCategory($category, $newUrlKey, $storeId) { $store = Mage::app()->getStore(); Mage::app()->setCurrentStore(Mage::app()->getStore(0)); if (!$this->isEntityProcessed(self::ENTITY_TYPE_CATEGORY, $category->getId() . '-' . $storeId) && !preg_match('~-[a-f0-9]{32}$~i', $newUrlKey)) { $category->setStoreId($storeId); $category->setUrlKey($newUrlKey . '-' . md5($category->getStoreId() . $category->getId())); $category->save(); $this->_reindexCategory($category->getId()); $category->unsetData('request_path'); $category->unsetData('url'); $this->markEntityProcessed(self::ENTITY_TYPE_CATEGORY, $category->getId() . '-' . $category->getStoreId()); } Mage::app()->setCurrentStore($store); }
/** * Get design update data of parent categories * * @param Mage_Catalog_Model_Category $category * @return array */ public function getDesignUpdateData($category) { $categories = array(); $pathIds = array(); foreach (array_reverse($category->getParentIds()) as $pathId) { if ($pathId == AO::app()->getStore()->getRootCategoryId()) { $pathIds[] = $pathId; break; } $pathIds[] = $pathId; } $select = $this->_getReadAdapter()->select()->from(array('main_table' => $this->getMainStoreTable($category->getStoreId())), array('main_table.entity_id', 'main_table.custom_design', 'main_table.custom_design_apply', 'main_table.custom_design_from', 'main_table.custom_design_to'))->where('main_table.entity_id IN (?)', $pathIds)->where('main_table.is_active = ?', '1')->order('main_table.path DESC'); $result = $this->_getReadAdapter()->fetchAll($select); foreach ($result as $row) { $row['id'] = $row['entity_id']; $categories[$row['entity_id']] = AO::getModel('catalog/category')->setData($row); } return $categories; }
/** * @param Mage_Catalog_Model_Category|Varien_Data_Tree_Node $category * @return $this */ public function loadByCategory($category) { $this->_getResource()->loadByCategory($this, $category->getId(), $category->getStoreId()); $this->_afterLoad(); return $this; }
/** * @param Mage_Catalog_Model_Category $category * @return string */ protected function _getCategoryPath($category) { $categoryPathIds = $category->getPathIds(); array_shift($categoryPathIds); $categoryNames = array(); foreach ($categoryPathIds as $categoryId) { $categoryNames[] = Mage::getResourceSingleton('catalog/category')->getAttributeRawValue($categoryId, 'name', $category->getStoreId()); } return implode(' > ', $categoryNames); }
/** * Get URL key for Enterprise * * @param Mage_Catalog_Model_Category $category * @return mixed */ protected function _getEnterpriseUrlKey(Mage_Catalog_Model_Category $category) { $resource = Mage::getSingleton('core/resource'); $conn = $resource->getConnection('core_read'); $tableName = $resource->getTableName(array('catalog/category', 'url_key')); $select = $conn->select()->from(array('url_key_table' => $tableName), array('url_key' => 'url_key_table.value', 'store_id' => 'MAX(url_key_table.store_id)'))->where('url_key_table.entity_id = ?', $category->getId())->where('url_key_table.store_id IN(?)', array(Mage_Core_Model_App::ADMIN_STORE_ID, $category->getStoreId())); $row = $conn->fetchRow($select); return $row['url_key']; }
protected function _prepareData(Mage_Catalog_Model_Category $category) { $this->_category = $category; $this->_currentStore = $category->getStoreId(); $this->_block = Mage::getModel('cms/block')->load($category->getMegamenuCmsBlock()); }
/** * Prepare category JSON * * @param Mage_Catalog_Model_Category $category * @return array */ public function getCategoryJSON(Mage_Catalog_Model_Category $category) { $transport = new Varien_Object(); Mage::dispatchEvent('algolia_category_index_before', array('category' => $category, 'custom_data' => $transport)); $customData = $transport->getData(); $storeId = $category->getStoreId(); $category->getUrlInstance()->setStore($storeId); $path = ''; foreach ($category->getPathIds() as $categoryId) { if ($path != '') { $path .= ' / '; } $path .= $this->getCategoryName($categoryId, $storeId); } $imageUrl = NULL; try { $imageUrl = $category->getImageUrl(); } catch (Exception $e) { /* no image, no default: not fatal */ } $data = array('objectID' => $this->getCategoryObjectId($category), 'name' => $category->getName(), 'path' => $path, 'level' => $category->getLevel(), 'url' => $category->getUrl(), '_tags' => array('category'), 'popularity' => 1); if ($this->isIndexProductCount()) { $data['product_count'] = $data['popularity'] = $category->getProductCount(); } if (!empty($imageUrl)) { $data['image_url'] = $imageUrl; } foreach ($this->getCategoryAdditionalAttributes($storeId) as $attributeCode) { $value = $category->hasData($this->_dataPrefix . $attributeCode) ? $category->getData($this->_dataPrefix . $attributeCode) : $category->getData($attributeCode); $value = Mage::getResourceSingleton('algoliasearch/fulltext')->getAttributeValue($attributeCode, $value, $storeId, Mage_Catalog_Model_Category::ENTITY); if ($value) { $data[$attributeCode] = $value; } } $data = array_merge($data, $customData); return $data; }
/** * Get category request path * * @param Mage_Catalog_Model_Category $category * @return string * @deprecated since 1.13.0.2 */ protected function _getCategoryRequestPath(Mage_Catalog_Model_Category $category) { /** * Initialize request_path value */ $category->getUrl(); /** @var $helper Enterprise_Catalog_Helper_Data */ $helper = $this->_factory->getHelper('enterprise_catalog'); return $helper->getCategoryRequestPath($category->getRequestPath(), $category->getStoreId()); }
public function testSetGetStoreId() { $this->assertEquals(Mage::app()->getStore()->getId(), $this->_model->getStoreId()); $this->_model->setStoreId(1000); $this->assertEquals(1000, $this->_model->getStoreId()); }
/** * Return children ids of category * * @param Mage_Catalog_Model_Category $category * @param boolean $recursive * @return array */ public function getChildren($category, $recursive = true) { $attributeId = (int) $this->_getIsActiveAttributeId(); $backendTable = $this->getTable(array($this->getEntityTablePrefix(), 'int')); $adapter = $this->_getReadAdapter(); $checkSql = $adapter->getCheckSql('c.value_id > 0', 'c.value', 'd.value'); $bind = array('attribute_id' => $attributeId, 'store_id' => $category->getStoreId(), 'scope' => 1); $select = $this->_getChildrenIdSelect($category, $recursive); $select->joinLeft(array('d' => $backendTable), 'd.attribute_id = :attribute_id AND d.store_id = 0 AND d.entity_id = m.entity_id', array())->joinLeft(array('c' => $backendTable), 'c.attribute_id = :attribute_id AND c.store_id = :store_id AND c.entity_id = m.entity_id', array())->where($checkSql . ' = :scope'); return $adapter->fetchCol($select, $bind); }
/** * Retrieve request path * * @param Mage_Catalog_Model_Category $category * @return bool|string */ protected function _getRequestPath(Mage_Catalog_Model_Category $category) { $rewrite = $this->getUrlRewrite(); $storeId = $category->getStoreId(); if ($storeId) { $rewrite->setStoreId($storeId); } $idPath = 'category/' . $category->getId(); $rewrite->loadByIdPath($idPath); if ($rewrite->getId()) { return $rewrite->getRequestPath(); } return false; }
/** * Return children ids of category * * @param Mage_Catalog_Model_Category $category * @param boolean $recursive * @return array */ public function getChildren($category, $recursive = true) { $attributeId = (int) $this->_getIsActiveAttributeId(); $backendTable = $this->getTable(array($this->getEntityTablePrefix(), 'int')); $adapter = $this->_getReadAdapter(); $checkSql = $adapter->getCheckSql('c.value_id > 0', 'c.value', 'd.value'); $bind = array('attribute_id' => $attributeId, 'store_id' => $category->getStoreId(), 'scope' => 1, 'c_path' => $category->getPath() . '/%'); $select = $this->_getReadAdapter()->select()->from(array('m' => $this->getEntityTable()), 'entity_id')->joinLeft(array('d' => $backendTable), 'd.attribute_id = :attribute_id AND d.store_id = 0 AND d.entity_id = m.entity_id', array())->joinLeft(array('c' => $backendTable), 'c.attribute_id = :attribute_id AND c.store_id = :store_id AND c.entity_id = m.entity_id', array())->where($checkSql . ' = :scope')->where($adapter->quoteIdentifier('path') . ' LIKE :c_path'); if (!$recursive) { $select->where($adapter->quoteIdentifier('level') . ' <= :c_level'); $bind['c_level'] = $category->getLevel() + 1; } return $adapter->fetchCol($select, $bind); }
public function move(Mage_Catalog_Model_Category $category, $newParentId) { $oldStoreId = $category->getStoreId(); $parent = Mage::getModel('catalog/category')->setStoreId($category->getStoreId())->load($category->getParentId()); $newParent = Mage::getModel('catalog/category')->setStoreId($category->getStoreId())->load($newParentId); $oldParentStores = $parent->getStoreIds(); $newParentStores = $newParent->getStoreIds(); $category->setParentId($newParentId)->save(); $parent->save(); $newParent->save(); // Add to new stores $addToStores = array_diff($newParentStores, $oldParentStores); foreach ($addToStores as $storeId) { $newCategory = clone $category; $newCategory->setStoreId($storeId)->save(); $children = $category->getAllChildren(); if ($children && ($arrChildren = explode(',', $children))) { foreach ($arrChildren as $childId) { if ($childId == $category->getId()) { continue; } $child = Mage::getModel('catalog/category')->setStoreId($oldStoreId)->load($childId)->setStoreId($storeId)->save(); } } } return $this; }