/** * Creates a new child category for $parentCategory * * @param \Magento\Catalog\Model\Category $parentCategory * @return \Magento\Catalog\Model\Category */ protected function createCategory($parentCategory) { /** @var \Magento\Catalog\Model\Category $category */ $category = $this->objectManager->create('Magento\\Catalog\\Model\\Category'); $category->setStoreId(self::DEFAULT_STORE_ID)->setParentId($parentCategory->getId())->setName(self::NAMES_PREFIX . $this->titlesGenerator->generateCategoryTitle())->setAttributeSetId($category->getDefaultAttributeSetId())->setLevel($parentCategory->getLevel() + 1)->setPath($parentCategory->getPath())->setIsActive(1)->save(); return $category; }
/** * Reindex category's data after into search engine after reindexing the category * * @param \Magento\Catalog\Model\Category $subject The category being reindexed * @param callable $proceed The parent function we are plugged on * : Magento\Catalog\Model\Category::reindex() * * @return \Magento\Catalog\Model\Category */ public function aroundReindex(\Magento\Catalog\Model\Category $subject, callable $proceed) { $proceed(); if ($subject->getLevel() > 1) { $categoryIndexer = $this->indexerRegistry->get(\Smile\ElasticsuiteCatalog\Model\Category\Indexer\Fulltext::INDEXER_ID); if (!$categoryIndexer->isScheduled()) { $categoryIndexer->reindexRow($subject->getId()); } } return; }
/** * @param \Magento\Catalog\Model\Category $category * @param boolean $recursive * @return int[] */ public function getChildrenIds(Category $category, $recursive = false) { $cacheKey = $category->getId() . '_' . (int) $recursive; if (!isset($this->childrenIds[$cacheKey])) { $connection = $category->getResource()->getReadConnection(); $select = $connection->select()->from($category->getResource()->getEntityTable(), 'entity_id')->where($connection->quoteIdentifier('path') . ' LIKE :c_path'); $bind = ['c_path' => $category->getPath() . '/%']; if (!$recursive) { $select->where($connection->quoteIdentifier('level') . ' <= :c_level'); $bind['c_level'] = $category->getLevel() + 1; } $this->childrenIds[$cacheKey] = $connection->fetchCol($select, $bind); } return $this->childrenIds[$cacheKey]; }
/** * @param \Magento\Catalog\Model\Category $category * @return bool */ protected function isNeedToGenerateUrlPathForParent($category) { return $category->isObjectNew() || $category->getLevel() >= self::MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING; }
/** * Return children ids of category * * @param \Magento\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); }
/** * Return children ids of category * * @param \Magento\Catalog\Model\Category $category * @param boolean $recursive * @return array */ public function getChildren($category, $recursive = true) { $linkField = $this->getLinkField(); $attributeId = $this->getIsActiveAttributeId(); $backendTable = $this->getTable([$this->getEntityTablePrefix(), 'int']); $connection = $this->getConnection(); $checkSql = $connection->getCheckSql('c.value_id > 0', 'c.value', 'd.value'); $linkField = $this->getLinkField(); $bind = ['attribute_id' => $attributeId, 'store_id' => $category->getStoreId(), 'scope' => 1, 'c_path' => $category->getPath() . '/%']; $select = $this->getConnection()->select()->from(['m' => $this->getEntityTable()], 'entity_id')->joinLeft(['d' => $backendTable], "d.attribute_id = :attribute_id AND d.store_id = 0 AND d.{$linkField} = m.{$linkField}", [])->joinLeft(['c' => $backendTable], "c.attribute_id = :attribute_id AND c.store_id = :store_id AND c.{$linkField} = m.{$linkField}", [])->where($checkSql . ' = :scope')->where($connection->quoteIdentifier('path') . ' LIKE :c_path'); if (!$recursive) { $select->where($connection->quoteIdentifier('level') . ' <= :c_level'); $bind['c_level'] = $category->getLevel() + 1; } return $connection->fetchCol($select, $bind); }
public function testGetLevel() { $this->assertEquals(0, $this->_model->getLevel()); $this->_model->setData('level', 1); $this->assertEquals(1, $this->_model->getLevel()); }
public function getObject(Category $category) { /** @var $productCollection Mage_Catalog_Model_Resource_Product_Collection */ $productCollection = $category->getProductCollection(); $productCollection = $productCollection->addMinimalPrice(); $category->setProductCount($productCollection->getSize()); $transport = new DataObject(); $this->eventManager->dispatch('algolia_category_index_before', ['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 = ['objectID' => $category->getId(), 'name' => $category->getName(), 'path' => $path, 'level' => $category->getLevel(), 'url' => $category->getUrl(), 'include_in_menu' => $category->getIncludeInMenu(), '_tags' => ['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_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; }
/** * Return children ids of category * * @param \Magento\Catalog\Model\Category $category * @param bool $recursive * @param bool $isActive * @return array */ public function getChildren($category, $recursive = true, $isActive = true) { $select = $this->_getReadAdapter()->select()->from($this->getMainStoreTable($category->getStoreId()), 'entity_id')->where('path LIKE ?', "{$category->getPath()}/%"); if (!$recursive) { $select->where('level <= ?', $category->getLevel() + 1); } if ($isActive) { $select->where('is_active = ?', '1'); } $_categories = $this->_getReadAdapter()->fetchAll($select); $categoriesIds = []; foreach ($_categories as $_category) { $categoriesIds[] = $_category['entity_id']; } return $categoriesIds; }
/** * {@inheritdoc} */ public function getLevel() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getLevel'); if (!$pluginInfo) { return parent::getLevel(); } else { return $this->___callPlugins('getLevel', func_get_args(), $pluginInfo); } }
/** * Validate category for be using as filter * * @param \Magento\Catalog\Model\Category $category * @return mixed */ protected function _isValidCategory($category) { if ($category->getId()) { while ($category->getLevel() != 0) { if (!$category->getIsActive()) { return false; } $category = $category->getParentCategory(); } return true; } return false; }