コード例 #1
0
 /**
  * @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;
 }
コード例 #2
0
ファイル: Category.php プロジェクト: shabbirvividads/magento2
 /**
  * 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;
 }
コード例 #3
0
ファイル: Category.php プロジェクト: pavelnovitsky/magento2
 /**
  * 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();
 }
コード例 #4
0
ファイル: Product.php プロジェクト: opexsw/magento2
 /**
  * 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;
 }
コード例 #5
0
ファイル: Rule.php プロジェクト: shabbirvividads/magento2
 /**
  * @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;
 }
コード例 #6
0
ファイル: CategoryProcessor.php プロジェクト: kid17/magento2
 /**
  * @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;
 }
コード例 #7
0
ファイル: Flat.php プロジェクト: shabbirvividads/magento2
 /**
  * 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());
 }
コード例 #8
0
ファイル: Category.php プロジェクト: aiesh/magento2
 /**
  * Get categories collection
  *
  * @return Collection
  */
 protected function _getCategoriesCollection()
 {
     return $this->_collectionFactory->create();
 }
コード例 #9
0
 /**
  * @param string $urlKey
  * @return \Magento\Framework\Object
  */
 protected function getCategoryByUrlKey($urlKey)
 {
     $category = $this->categoryFactory->create()->addAttributeToFilter('url_key', $urlKey)->addUrlRewriteToResult()->getFirstItem();
     return $category;
 }
コード例 #10
0
ファイル: Author.php プロジェクト: nhc/Magento2SampleModule
 /**
  * @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;
 }