php Magento-Catalog-Model-CategoryFactory类(方法)实例源码

下面列出了php Magento-Catalog-Model-CategoryFactory 类(方法)源码代码实例,从而了解它的用法。

作者:pradeep-wagent    项目:magento   
/**
  * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  */
 protected function setUp()
 {
     /** @var \Magento\Framework\Registry $var */
     $this->coreRegistry = $var = $this->getMockBuilder('\\Magento\\Framework\\Registry')->disableOriginalConstructor()->setMethods(['register'])->getMock();
     $this->category = $this->getMockBuilder('Magento\\Catalog\\Model\\Category')->disableOriginalConstructor()->setMethods(['getId', 'setStoreId', 'load', 'getPathIds'])->getMock();
     $this->categoryFactory = $this->getMockBuilder('Magento\\Catalog\\Model\\CategoryFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock();
     $this->categoryFactory->expects($this->any())->method('create')->will($this->returnValue($this->category));
     $this->store = $this->getMockBuilder('\\Magento\\Store\\Model\\Store')->disableOriginalConstructor()->setMethods(['getId'])->getMock();
     $this->layer = $this->getMockBuilder('Magento\\Catalog\\Model\\Layer')->disableOriginalConstructor()->setMethods(['getCurrentStore', 'getCurrentCategory'])->getMock();
     $this->layer->expects($this->any())->method('getCurrentStore')->will($this->returnValue($this->store));
     $objectManagerHelper = new ObjectManagerHelper($this);
     $this->target = $objectManagerHelper->getObject('Magento\\Catalog\\Model\\Layer\\Filter\\DataProvider\\Category', ['coreRegistry' => $this->coreRegistry, 'categoryFactory' => $this->categoryFactory, 'layer' => $this->layer]);
 }

作者:buskamuz    项目:magento2-skeleto   
protected function setUp()
 {
     $helper = new ObjectManager($this);
     $this->category = $this->getMockBuilder('Magento\\Catalog\\Model\\Category')->setMethods(['load', 'getId', '__wakeup'])->disableOriginalConstructor()->getMock();
     $this->categoryFactory = $this->getMockBuilder('Magento\\Catalog\\Model\\CategoryFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock();
     $this->categoryFactory->expects($this->any())->method('create')->will($this->returnValue($this->category));
     $this->registry = $this->getMockBuilder('Magento\\Framework\\Registry')->setMethods(['registry'])->disableOriginalConstructor()->getMock();
     $this->store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->setMethods(['getRootCategoryId', 'getFilters', '__wakeup'])->disableOriginalConstructor()->getMockForAbstractClass();
     $this->storeManager = $this->getMockBuilder('Magento\\Framework\\StoreManagerInterface')->setMethods(['getStore'])->disableOriginalConstructor()->getMockForAbstractClass();
     $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->store));
     $this->stateKeyGenerator = $this->getMockBuilder('Magento\\Catalog\\Model\\Layer\\Category\\StateKey')->setMethods(['toString'])->disableOriginalConstructor()->getMock();
     $this->collectionFilter = $this->getMockBuilder('Magento\\Catalog\\Model\\Layer\\Category\\CollectionFilter')->setMethods(['filter'])->disableOriginalConstructor()->getMock();
     $this->collectionProvider = $this->getMockBuilder('Magento\\Catalog\\Model\\Layer\\ItemCollectionProviderInterface')->disableOriginalConstructor()->getMockForAbstractClass();
     $this->filter = $this->getMockBuilder('Magento\\Catalog\\Model\\Layer\\Filter\\Item')->setMethods(['getFilter', 'getValueString'])->disableOriginalConstructor()->getMock();
     $this->abstractFilter = $this->getMockBuilder('Magento\\Catalog\\Model\\Layer\\Filter\\AbstractFilter')->setMethods(['getRequestVar'])->disableOriginalConstructor()->getMock();
     $this->context = $this->getMockBuilder('Magento\\Catalog\\Model\\Layer\\ContextInterface')->setMethods(['getStateKey', 'getCollectionFilter'])->disableOriginalConstructor()->getMockForAbstractClass();
     $this->context->expects($this->any())->method('getStateKey')->will($this->returnValue($this->stateKeyGenerator));
     $this->context->expects($this->any())->method('getCollectionFilter')->will($this->returnValue($this->collectionFilter));
     $this->context->expects($this->any())->method('getCollectionProvider')->will($this->returnValue($this->collectionProvider));
     $this->state = $this->getMockBuilder('Magento\\Catalog\\Model\\Layer\\State')->disableOriginalConstructor()->getMock();
     $this->stateFactory = $this->getMockBuilder('Magento\\Catalog\\Model\\Layer\\StateFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock();
     $this->stateFactory->expects($this->any())->method('create')->will($this->returnValue($this->state));
     $this->collection = $this->getMockBuilder('Magento\\Catalog\\Model\\Resource\\Product\\Collection')->disableOriginalConstructor()->getMock();
     $this->model = $helper->getObject('Magento\\Catalog\\Model\\Layer', ['registry' => $this->registry, 'categoryFactory' => $this->categoryFactory, 'storeManager' => $this->storeManager, 'context' => $this->context, 'layerStateFactory' => $this->stateFactory]);
 }

作者:kidaa3    项目:magento2-platforms   
/**
  * @param \Magento\Framework\View\Element\Template\Context $context
  * @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
  * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
  * @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
  * @param \Magento\Framework\App\Http\Context $httpContext
  * @param \Magento\Catalog\Helper\Category $catalogCategory
  * @param \Magento\Framework\Registry $registry
  * @param \Magento\Catalog\Model\Indexer\Category\Flat\State $flatState
  * @param array $data
  */
 public function __construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Catalog\Model\CategoryFactory $categoryFactory, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Catalog\Model\Layer\Resolver $layerResolver, \Magento\Framework\App\Http\Context $httpContext, \Magento\Catalog\Helper\Category $catalogCategory, \Magento\Framework\Registry $registry, \Magento\Catalog\Model\Indexer\Category\Flat\State $flatState, array $data = [])
 {
     $this->_productCollectionFactory = $productCollectionFactory;
     $this->_catalogLayer = $layerResolver->get();
     $this->httpContext = $httpContext;
     $this->_catalogCategory = $catalogCategory;
     $this->_registry = $registry;
     $this->flatState = $flatState;
     $this->_categoryInstance = $categoryFactory->create();
     parent::__construct($context, $data);
 }

作者:kidaa3    项目:magento2-platforms   
/**
  * Get or create new instance of category
  *
  * @return \Magento\Catalog\Model\Product
  */
 private function _getCategory()
 {
     if (!$this->hasData('category')) {
         $this->setCategory($this->_categoryFactory->create());
     }
     return $this->getCategory();
 }

作者:Atli    项目:docker-magento   
/**
  * Get parent category options
  *
  * @return array
  */
 protected function _getParentCategoryOptions()
 {
     $items = $this->_categoryFactory->create()->getCollection()->addAttributeToSelect('name')->addAttributeToSort('entity_id', 'ASC')->setPageSize(3)->load()->getItems();
     $result = array();
     if (count($items) === 2) {
         $item = array_pop($items);
         $result = array($item->getEntityId() => $item->getName());
     }
     return $result;
 }

作者:aies    项目:magento   
/**
  * @param int $id
  * @return CategoryModel
  * @throws \Magento\Framework\Exception\NoSuchEntityException
  */
 private function getCategory($id)
 {
     /** @var CategoryModel $category */
     $category = $this->categoryFactory->create();
     $category->load($id);
     if (!$category->getId()) {
         throw NoSuchEntityException::singleField(Category::ID, $id);
     }
     return $category;
 }

作者:aies    项目:magento   
/**
  * @param int $categoryId
  * @return Category
  * @throws \Magento\Framework\Exception\NoSuchEntityException
  */
 public function load($categoryId)
 {
     /** @var Category $category */
     $category = $this->categoryFactory->create();
     $category->load($categoryId);
     if (!$category->getId()) {
         throw new NoSuchEntityException('There is no category with provided ID');
     }
     return $category;
 }

作者:aies    项目:magento   
/**
  * {@inheritdoc}
  */
 public function tree($rootCategoryId = null, $depth = null)
 {
     $category = null;
     if (!is_null($rootCategoryId)) {
         /** @var \Magento\Catalog\Model\Category $category */
         $category = $this->categoryFactory->create()->load($rootCategoryId);
         if (!$category->getId()) {
             throw new \Magento\Framework\Exception\NoSuchEntityException('Root Category does not exist');
         }
     }
     $result = $this->categoryTree->getTree($this->categoryTree->getRootNode($category), $depth);
     return $result;
 }

作者:kid1    项目:magento   
/**
  * Creates a category.
  *
  * @param string $name
  * @param int $parentId
  *
  * @return int
  */
 protected function createCategory($name, $parentId)
 {
     /** @var \Magento\Catalog\Model\Category $category */
     $category = $this->categoryFactory->create();
     $parentCategory = $this->categoryFactory->create()->load($parentId);
     $category->setPath($parentCategory->getPath());
     $category->setParentId($parentId);
     $category->setName($name);
     $category->setIsActive(true);
     $category->setIncludeInMenu(true);
     $category->setAttributeSetId($category->getDefaultAttributeSetId());
     $category->save();
     return $category->getId();
 }

作者:Sebwit    项目:magento2-category-sideba   
/**
  * Get all categories
  *
  * @param bool $sorted
  * @param bool $asCollection
  * @param bool $toLoad
  *
  * @return array|\Magento\Catalog\Model\ResourceModel\Category\Collection|\Magento\Framework\Data\Tree\Node\Collection
  */
 public function getCategories($sorted = false, $asCollection = false, $toLoad = true)
 {
     $cacheKey = sprintf('%d-%d-%d-%d', $this->getSelectedRootCategory(), $sorted, $asCollection, $toLoad);
     if (isset($this->_storeCategories[$cacheKey])) {
         return $this->_storeCategories[$cacheKey];
     }
     /**
      * Check if parent node of the store still exists
      */
     $category = $this->_categoryFactory->create();
     $storeCategories = $category->getCategories($this->getSelectedRootCategory(), $recursionLevel = 1, $sorted, $asCollection, $toLoad);
     $this->_storeCategories[$cacheKey] = $storeCategories;
     return $storeCategories;
 }

作者:MauroNigrel    项目:magento2-module-setup_tool   
/**
  * @return \Magento\Catalog\Model\Category
  */
 protected function getTreeRootCategory()
 {
     if (!$this->treeRootCategory) {
         $this->treeRootCategory = $this->categoryFactory->create()->load(Category::TREE_ROOT_ID);
     }
     return $this->treeRootCategory;
 }

作者:aies    项目:magento   
/**
  * Retrieve loaded category collection
  *
  * @return AbstractCollection
  */
 protected function _getProductCollection()
 {
     if (is_null($this->_productCollection)) {
         $layer = $this->getLayer();
         /* @var $layer \Magento\Catalog\Model\Layer */
         if ($this->getShowRootCategory()) {
             $this->setCategoryId($this->_storeManager->getStore()->getRootCategoryId());
         }
         // if this is a product view page
         if ($this->_coreRegistry->registry('product')) {
             // get collection of categories this product is associated with
             $categories = $this->_coreRegistry->registry('product')->getCategoryCollection()->setPage(1, 1)->load();
             // if the product is associated with any category
             if ($categories->count()) {
                 // show products from this category
                 $this->setCategoryId(current($categories->getIterator()));
             }
         }
         $origCategory = null;
         if ($this->getCategoryId()) {
             /** @var \Magento\Catalog\Model\Category $category */
             $category = $this->_categoryFactory->create()->load($this->getCategoryId());
             if ($category->getId()) {
                 $origCategory = $layer->getCurrentCategory();
                 $layer->setCurrentCategory($category);
             }
         }
         $this->_productCollection = $layer->getProductCollection();
         $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
         if ($origCategory) {
             $layer->setCurrentCategory($origCategory);
         }
     }
     return $this->_productCollection;
 }

作者:kidaa3    项目:magento2-platforms   
/**
  * Retrieve category url
  *
  * @param ModelCategory $category
  * @return string
  */
 public function getCategoryUrl($category)
 {
     if ($category instanceof ModelCategory) {
         return $category->getUrl();
     }
     return $this->_categoryFactory->create()->setData($category->getData())->getUrl();
 }

作者:aies    项目:magento   
/**
  * Set current attribute to entry (for specified product)
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param \Magento\Framework\Gdata\Gshopping\Entry $entry
  * @return \Magento\Framework\Gdata\Gshopping\Entry
  */
 public function convertAttribute($product, $entry)
 {
     $productCategories = $product->getCategoryIds();
     // TODO: set Default value for product_type attribute if product isn't assigned for any category
     $value = 'Shop';
     if (!empty($productCategories)) {
         $category = $this->_categoryFactory->create()->load(array_shift($productCategories));
         $breadcrumbs = array();
         foreach ($category->getParentCategories() as $cat) {
             $breadcrumbs[] = $cat->getName();
         }
         $value = implode(' > ', $breadcrumbs);
     }
     $this->_setAttribute($entry, 'product_type', self::ATTRIBUTE_TYPE_TEXT, $value);
     return $entry;
 }

作者:smile-s    项目:elasticsuit   
/**
  * Load current category using the request params.
  *
  * @return CategoryInterface
  */
 private function loadCategory()
 {
     $category = $this->categoryFactory->create();
     $storeId = $this->getRequest()->getParam('store');
     $categoryId = $this->getRequest()->getParam('entity_id');
     $category->setStoreId($storeId)->load($categoryId);
     return $category;
 }

作者:Atli    项目:docker-magento   
/**
  * Load category
  *
  * @param int $id
  * @throws \Magento\Framework\Exception\NoSuchEntityException
  * @return \Magento\Catalog\Model\Category
  */
 protected function loadCategory($id)
 {
     $model = $this->categoryFactory->create();
     $model->load($id);
     if (!$model->getId()) {
         throw NoSuchEntityException::singleField(CategoryDataObject::ID, $id);
     }
     return $model;
 }

作者:Sebwit    项目:magento2-category-sideba   
/**
  * Return array of options as value-label pairs
  *
  * @return array Format: array(array('value' => '<value>', 'label' => '<label>'), ...)
  */
 public function toOptionArray()
 {
     $cacheKey = sprintf('%d-%d-%d-%d', 1, false, false, true);
     if (isset($this->_storeCategories[$cacheKey])) {
         return $this->_storeCategories[$cacheKey];
     }
     /**
      * Check if parent node of the store still exists
      */
     $category = $this->_categoryFactory->create();
     $storeCategories = $category->getCategories(1, $recursionLevel = 1, false, false, true);
     $this->_storeCategories[$cacheKey] = $storeCategories;
     $resultArray = [];
     foreach ($storeCategories as $category) {
         $resultArray[$category->getId()] = $category->getName();
     }
     return $resultArray;
 }

作者:pradeep-wagent    项目:magento   
/**
  * Get categories collection
  *
  * @return \Magento\Catalog\Model\ResourceModel\Category\Collection
  */
 public function getCategoryCollection()
 {
     $collection = $this->_getData('category_collection');
     if ($collection === null) {
         $collection = $this->_categoryFactory->create()->getCollection()->addAttributeToSelect(['name', 'is_active'])->setLoadProductCount(true);
         $this->setData('category_collection', $collection);
     }
     return $collection;
 }

作者:dotmaile    项目:dotmailer-magento2-extensio   
/**
  * Get last purchased category.
  *
  * @return string
  */
 public function getLastCategoryPur()
 {
     $categoryId = $this->customer->getLastCategoryId();
     //customer last category id
     if ($categoryId) {
         return $this->categoryFactory->create()->setStoreId($this->customer->getStoreId())->load($categoryId)->getName();
     }
     return '';
 }

作者:smile-s    项目:elasticsuit   
/**
  * Load the root category used for a virtual category.
  *
  * @param CategoryInterface $category Virtual category.
  *
  * @return CategoryInterface
  */
 private function getVirtualRootCategory(CategoryInterface $category)
 {
     $storeId = $this->getStoreId();
     $rootCategory = $this->categoryFactory->create()->setStoreId($storeId);
     if ($category->getVirtualCategoryRoot() !== null && !empty($category->getVirtualCategoryRoot())) {
         $rootCategoryId = $category->getVirtualCategoryRoot();
         $rootCategory->load($rootCategoryId);
     }
     return $rootCategory;
 }


问题


面经


文章

微信
公众号

扫码关注公众号