/** * @return $this */ protected function initCategories() { if (empty($this->categories) && empty($this->categoriesWithRoots)) { $collection = $this->categoryColFactory->create()->addNameToResult(); /* @var $collection \Magento\Catalog\Model\Resource\Category\Collection */ foreach ($collection as $category) { $structure = explode('/', $category->getPath()); $pathSize = count($structure); if ($pathSize > 1) { $path = []; for ($i = 1; $i < $pathSize; $i++) { $path[] = $collection->getItemById($structure[$i])->getName(); } $rootCategoryName = array_shift($path); if (!isset($this->categoriesWithRoots[$rootCategoryName])) { $this->categoriesWithRoots[$rootCategoryName] = []; } $index = implode('/', $path); $this->categoriesWithRoots[$rootCategoryName][$index] = $category->getId(); if ($pathSize > 2) { $this->categories[$index] = $category->getId(); } } } } return $this; }
/** * Return option array * * @param bool $addEmpty * @return array */ public function toOptionArray($addEmpty = true) { /** @var \Magento\Catalog\Model\Resource\Category\Collection $collection */ $collection = $this->_categoryCollectionFactory->create(); $collection->addAttributeToSelect('name')->addRootLevelFilter()->load(); $options = []; if ($addEmpty) { $options[] = ['label' => __('-- Please Select a Category --'), 'value' => '']; } foreach ($collection as $category) { $options[] = ['label' => $category->getName(), 'value' => $category->getId()]; } return $options; }
/** * Return parent categories of category * * @param \Magento\Catalog\Model\Category $category * @return \Magento\Framework\Object[] */ public function getParentCategories($category) { $pathIds = array_reverse(explode(',', $category->getPathInStore())); /** @var \Magento\Catalog\Model\Resource\Category\Collection $categories */ $categories = $this->_categoryCollectionFactory->create(); return $categories->setStore($this->_storeManager->getStore())->addAttributeToSelect('name')->addAttributeToSelect('url_key')->addFieldToFilter('entity_id', array('in' => $pathIds))->addFieldToFilter('is_active', 1)->load()->getItems(); }
/** * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function setUp() { $this->_eavConfig = $this->getMock('Magento\\Eav\\Model\\Config', array('getEntityType', 'getEntityTypeId'), array(), '', false); $this->_eavConfig->expects($this->atLeastOnce())->method('getEntityType')->with($this->equalTo('catalog_product'))->will($this->returnSelf()); $this->_eavConfig->expects($this->atLeastOnce())->method('getEntityTypeId')->will($this->returnValue('1')); $this->_optionModel = $this->getMock('\\Magento\\CatalogImportExport\\Model\\Import\\Product\\Option', array(), array(), '', false); $this->_optionFactory = $this->getMock('\\Magento\\CatalogImportExport\\Model\\Import\\Product\\OptionFactory', array('create'), array(), '', false); $this->_optionFactory->expects($this->atLeastOnce())->method('create')->will($this->returnValue($this->_optionModel)); $this->_storeManager = $this->getMock('\\Magento\\Store\\Model\\StoreManager', array('getWebsites', 'getStores'), array(), '', false); $this->_storeManager->expects($this->atLeastOnce())->method('getWebsites')->will($this->returnValue(array())); $this->_storeManager->expects($this->atLeastOnce())->method('getStores')->will($this->returnValue(array())); $this->_setCol = $this->getMock('\\Magento\\Eav\\Model\\Resource\\Entity\\Attribute\\Set\\Collection', array('setEntityTypeFilter'), array(), '', false); $this->_setCol->expects($this->atLeastOnce())->method('setEntityTypeFilter')->with($this->equalTo('1'))->will($this->returnValue(array())); $this->_setColFactory = $this->getMock('\\Magento\\Eav\\Model\\Resource\\Entity\\Attribute\\Set\\CollectionFactory', array('create'), array(), '', false); $this->_setColFactory->expects($this->atLeastOnce())->method('create')->will($this->returnValue($this->_setCol)); $this->_importConfig = $this->getMock('\\Magento\\ImportExport\\Model\\Import\\Config', array('getEntityTypes'), array(), '', false); $this->_importConfig->expects($this->atLeastOnce())->method('getEntityTypes')->with('catalog_product')->will($this->returnValue(array())); $this->_categoryCol = $this->getMock('\\Magento\\Catalog\\Model\\Resource\\Category\\Collection', array('addNameToResult'), array(), '', false); $this->_categoryCol->expects($this->atLeastOnce())->method('addNameToResult')->will($this->returnValue(array())); $this->_categoryColFactory = $this->getMock('\\Magento\\Catalog\\Model\\Resource\\Category\\CollectionFactory', array('create'), array(), '', false); $this->_categoryColFactory->expects($this->atLeastOnce())->method('create')->will($this->returnValue($this->_categoryCol)); $this->_product = $this->getMock('\\Magento\\Catalog\\Model\\Product', array('getProductEntitiesInfo', '__wakeup'), array(), '', false); $this->_product->expects($this->atLeastOnce())->method('getProductEntitiesInfo')->with($this->equalTo(array('entity_id', 'type_id', 'attribute_set_id', 'sku')))->will($this->returnValue(array())); $this->_productFactory = $this->getMock('\\Magento\\Catalog\\Model\\ProductFactory', array('create'), array(), '', false); $this->_productFactory->expects($this->atLeastOnce())->method('create')->will($this->returnValue($this->_product)); $this->_customerGroupService = $this->getMock('Magento\\Customer\\Service\\V1\\CustomerGroupService', array('getGroups'), array(), '', false); $this->_customerGroupService->expects($this->atLeastOnce())->method('getGroups')->will($this->returnValue(array())); $objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); $this->_model = $objectManager->getObject('\\Magento\\CatalogImportExport\\Model\\Import\\Product', array('config' => $this->_eavConfig, 'optionFactory' => $this->_optionFactory, 'storeManager' => $this->_storeManager, 'setColFactory' => $this->_setColFactory, 'importConfig' => $this->_importConfig, 'categoryColFactory' => $this->_categoryColFactory, 'productFactory' => $this->_productFactory, 'customerGroupService' => $this->_customerGroupService)); }
/** * Get collection of product categories * * @param \Magento\Catalog\Model\Product $product * @return \Magento\Catalog\Model\Resource\Category\Collection */ public function getCategoryCollection($product) { /** @var \Magento\Catalog\Model\Resource\Category\Collection $collection */ $collection = $this->_categoryCollectionFactory->create(); $collection->joinField('product_id', 'catalog_category_product', 'product_id', 'category_id = entity_id', null)->addFieldToFilter('product_id', (int) $product->getId()); return $collection; }
/** * @param string $urlKey * @return mixed|null */ protected function getCategoryReplacement($urlKey) { $categoryCollection = $this->categoryCollectionFactory->create(); $category = $categoryCollection->addAttributeToFilter('url_key', $urlKey)->getFirstItem(); $categoryId = null; if (!empty($category)) { $categoryId = $category->getId(); } return $categoryId; }
/** * @return $this */ protected function initCategories() { if (empty($this->categories)) { $collection = $this->categoryColFactory->create(); $collection->addNameToResult(); /* @var $collection \Magento\Catalog\Model\Resource\Category\Collection */ foreach ($collection as $category) { $structure = explode(self::DELIMITER_CATEGORY, $category->getPath()); $pathSize = count($structure); if ($pathSize > 1) { $path = []; for ($i = 1; $i < $pathSize; $i++) { $path[] = $collection->getItemById((int) $structure[$i])->getName(); } $index = implode(self::DELIMITER_CATEGORY, $path); $this->categories[$index] = $category->getId(); } } } return $this; }
/** * Return array or collection of categories * * @param integer $parent * @param integer $recursionLevel * @param boolean|string $sorted * @param boolean $asCollection * @param boolean $toLoad * @return array|\Magento\Framework\Data\Collection */ public function getCategories($parent, $recursionLevel = 0, $sorted = false, $asCollection = false, $toLoad = true) { if ($asCollection) { $select = $this->_getReadAdapter()->select()->from(['mt' => $this->getMainStoreTable($this->getStoreId())], ['path'])->where('mt.entity_id = ?', $parent); $parentPath = $this->_getReadAdapter()->fetchOne($select); $collection = $this->_categoryCollectionFactory->create()->addNameToResult()->addUrlRewriteToResult()->addParentPathFilter($parentPath)->addStoreFilter()->addIsActiveFilter()->addAttributeToFilter('include_in_menu', 1)->addSortedField($sorted); if ($toLoad) { return $collection->load(); } return $collection; } return $this->getNodes($parent, $recursionLevel, $this->_storeManager->getStore()->getId()); }
/** * Get categories collection * * @return Collection */ protected function _getCategoriesCollection() { return $this->_collectionFactory->create(); }
/** * @param string $urlKey * @return \Magento\Framework\Object */ protected function getCategoryByUrlKey($urlKey) { $category = $this->categoryFactory->create()->addAttributeToFilter('url_key', $urlKey)->addUrlRewriteToResult()->getFirstItem(); return $category; }
/** * @param string $attributes * @return \Magento\Catalog\Model\Resource\Category\Collection */ public function getSelectedCategoriesCollection($attributes = '*') { if (is_null($this->categoryCollection)) { $collection = $this->categoryCollectionFactory->create(); $collection->addAttributeToSelect($attributes); $collection->joinField('position', 'sample_news_author_category', 'position', 'category_id=entity_id', '{{table}}.author_id=' . $this->getId(), 'inner'); $this->categoryCollection = $collection; } return $this->categoryCollection; }