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

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

作者:Doabilit    项目:magento2de   
protected function setUp()
 {
     $this->_category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Category');
     $this->_category->load(5);
     $layer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Layer\\Category', ['data' => ['current_category' => $this->_category]]);
     $this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Layer\\Filter\\Category', ['layer' => $layer]);
 }

作者:BlackIkeEagl    项目:magento2-continuousph   
/**
  * Build response for ajax request
  *
  * @param \Magento\Catalog\Model\Category $category
  * @param \Magento\Backend\Model\View\Result\Page $resultPage
  *
  * @return \Magento\Framework\Controller\Result\Json
  *
  * @deprecated
  */
 protected function ajaxRequestResponse($category, $resultPage)
 {
     // prepare breadcrumbs of selected category, if any
     $breadcrumbsPath = $category->getPath();
     if (empty($breadcrumbsPath)) {
         // but if no category, and it is deleted - prepare breadcrumbs from path, saved in session
         $breadcrumbsPath = $this->_objectManager->get('Magento\\Backend\\Model\\Auth\\Session')->getDeletedPath(true);
         if (!empty($breadcrumbsPath)) {
             $breadcrumbsPath = explode('/', $breadcrumbsPath);
             // no need to get parent breadcrumbs if deleting category level 1
             if (count($breadcrumbsPath) <= 1) {
                 $breadcrumbsPath = '';
             } else {
                 array_pop($breadcrumbsPath);
                 $breadcrumbsPath = implode('/', $breadcrumbsPath);
             }
         }
     }
     $eventResponse = new \Magento\Framework\DataObject(['content' => $resultPage->getLayout()->getUiComponent('category_form')->getFormHtml() . $resultPage->getLayout()->getBlock('category.tree')->getBreadcrumbsJavascript($breadcrumbsPath, 'editingCategoryBreadcrumbs'), 'messages' => $resultPage->getLayout()->getMessagesBlock()->getGroupedHtml(), 'toolbar' => $resultPage->getLayout()->getBlock('page.actions.toolbar')->toHtml()]);
     $this->_eventManager->dispatch('category_prepare_ajax_response', ['response' => $eventResponse, 'controller' => $this]);
     /** @var \Magento\Framework\Controller\Result\Json $resultJson */
     $resultJson = $this->_objectManager->get('Magento\\Framework\\Controller\\Result\\Json');
     $resultJson->setHeader('Content-type', 'application/json', true);
     $resultJson->setData($eventResponse->getData());
     return $resultJson;
 }

作者:rogya    项目:m2-sampledata-generato   
/**
  * 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;
 }

作者:algoli    项目:algoliasearch-magento-   
public function aroundGetUrlInstance(Category $category, \Closure $proceed)
 {
     if ($category->getStoreId() == 0) {
         return $proceed();
     } else {
         return $this->objectManager->create(self::FRONTEND_URL)->setStoreId($category->getStoreId());
     }
 }

作者:smile-s    项目:elasticsuit   
/**
  * Reindex category's products after reindexing the category
  *
  * @param \Magento\Catalog\Model\Category $subject The cateogry 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 (!empty($subject->getAffectedProductIds())) {
         $this->processFullTextIndex($subject->getAffectedProductIds());
     }
     return;
 }

作者:pradeep-wagent    项目:magento   
public function testAddCatalogToTopMenuItemsWithFlat()
 {
     $observer = $this->_preparationData();
     $this->_category->expects($this->once())->method('getChildrenNodes')->will($this->returnValue([$this->_childrenCategory]));
     $this->_category->expects($this->once())->method('getUseFlatResource')->will($this->returnValue(true));
     $this->_categoryFlatState->expects($this->once())->method('isFlatEnabled')->will($this->returnValue(true));
     $this->_observer->execute($observer);
 }

作者:kidaa3    项目:magento2-platforms   
/**
  * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $url
  * @param int $storeId
  * @return array
  */
 protected function generateForCustom($url, $storeId)
 {
     $urls = [];
     $targetPath = !$url->getRedirectType() ? $url->getTargetPath() : $this->categoryUrlPathGenerator->getUrlPathWithSuffix($this->category, $storeId);
     if ($url->getRequestPath() !== $targetPath) {
         $urls[$url->getRequestPath() . '_' . $storeId] = $this->urlRewriteFactory->create()->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE)->setEntityId($this->category->getId())->setRequestPath($url->getRequestPath())->setTargetPath($targetPath)->setRedirectType($url->getRedirectType())->setStoreId($storeId)->setDescription($url->getDescription())->setIsAutogenerated(0)->setMetadata($url->getMetadata());
     }
     return $urls;
 }

作者:aies    项目:magento   
/**
  * @param \Magento\Catalog\Model\Category $category
  * @return Node
  */
 protected function getNode(\Magento\Catalog\Model\Category $category)
 {
     $nodeId = $category->getId();
     $node = $this->categoryTree->loadNode($nodeId);
     $node->loadChildren();
     $this->prepareCollection();
     $this->categoryTree->addCollectionData($this->categoryCollection);
     return $node;
 }

作者:pradeep-wagent    项目:magento   
/**
  * @param \Magento\Catalog\Model\Category $subject
  * @param \Magento\Catalog\Model\Category $result
  * @return \Magento\Catalog\Model\Category
  *
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function afterSave(\Magento\Catalog\Model\Category $subject, \Magento\Catalog\Model\Category $result)
 {
     /** @var \Magento\Catalog\Model\Category $result */
     $productIds = $result->getAffectedProductIds();
     if ($productIds) {
         $this->productRuleProcessor->reindexList($productIds);
     }
     return $result;
 }

作者:avra91    项目:Magento2SampleModul   
/**
  * @access public
  * @param \Magento\Catalog\Model\Category $category
  * @return mixed
  */
 public function getSelectedAuthors(CategoryModel $category)
 {
     if (!$category->hasSelectedAuthors()) {
         $authors = [];
         foreach ($this->getSelectedAuthorsCollection($category) as $author) {
             $authors[] = $author;
         }
         $category->setSelectedAuthors($authors);
     }
     return $category->getData('selected_authors');
 }

作者:plemin    项目:Gemto   
/**
  * @access public
  * @param \Magento\Catalog\Model\Category $category
  * @return mixed
  */
 public function getSelectedArticles(CategoryModel $category)
 {
     if (!$category->hasSelectedArticles()) {
         $articles = [];
         foreach ($this->getSelectedArticlesCollection($category) as $article) {
             $articles[] = $article;
         }
         $category->setSelectedArticles($articles);
     }
     return $category->getData('selected_articles');
 }

作者:shabbirvividad    项目:magento   
/**
  * Generate list of urls for global scope
  *
  * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
  */
 protected function generateForGlobalScope()
 {
     $urls = [];
     $categoryId = $this->category->getId();
     foreach ($this->category->getStoreIds() as $id) {
         if (!$this->isGlobalScope($id) && !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore($id, $categoryId, Category::ENTITY)) {
             $urls = array_merge($urls, $this->generateForSpecificStoreView($id));
         }
     }
     return $urls;
 }

作者:smile-s    项目:elasticsuit   
/**
  * 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;
 }

作者:Nost    项目:nosto-magento   
/**
  * @param Category $category
  * @return string
  */
 protected function buildPath(Category $category)
 {
     $data = [];
     $path = $category->getPath();
     foreach (explode('/', $path) as $categoryId) {
         $category = $this->_categoryRepository->get($categoryId);
         if ($category && $category->getLevel() > 1) {
             $data[] = $category->getName();
         }
     }
     return count($data) ? '/' . implode('/', $data) : '';
 }

作者:aies    项目:magento   
/**
  * Get Category from request
  *
  * @return Category
  */
 protected function _getCategory()
 {
     if (!$this->_category) {
         $this->_category = $this->_objectManager->create('Magento\\Catalog\\Model\\Category');
         $categoryId = (int) $this->getRequest()->getParam('category', 0);
         if (!$categoryId && $this->_getUrlRewrite()->getId()) {
             $categoryId = $this->_getUrlRewrite()->getCategoryId();
         }
         if ($categoryId) {
             $this->_category->load($categoryId);
         }
     }
     return $this->_category;
 }

作者:pradeep-wagent    项目:magento   
/**
  * @param \Magento\Catalog\Model\Category $category
  * @param int $storeId
  * @return $this
  */
 public function getProductCollection(\Magento\Catalog\Model\Category $category, $storeId)
 {
     /** @var $layer \Magento\Catalog\Model\Layer */
     $layer = $this->catalogLayer->setStore($storeId);
     $collection = $category->getResourceCollection();
     $collection->addAttributeToSelect('url_key')->addAttributeToSelect('name')->addAttributeToSelect('is_anchor')->addAttributeToFilter('is_active', 1)->addIdFilter($category->getChildren())->load();
     /** @var $productCollection \Magento\Catalog\Model\ResourceModel\Product\Collection */
     $productCollection = $this->collectionFactory->create();
     $currentCategory = $layer->setCurrentCategory($category);
     $layer->prepareProductCollection($productCollection);
     $productCollection->addCountToCategories($collection);
     $category->getProductCollection()->setStoreId($storeId);
     $products = $currentCategory->getProductCollection()->addAttributeToSort('updated_at', 'desc')->setVisibility($this->visibility->getVisibleInCatalogIds())->setCurPage(1)->setPageSize(50);
     return $products;
 }

作者:kidaa3    项目:magento2-platforms   
/**
  * @param Category $category
  * @param int $storeId
  * @param bool $saveRewriteHistory
  * @return UrlRewrite[]
  */
 public function getCategoryProductsUrlRewrites(Category $category, $storeId, $saveRewriteHistory)
 {
     /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
     $productCollection = $category->getProductCollection()->addAttributeToSelect('name')->addAttributeToSelect('url_key')->addAttributeToSelect('url_path');
     $productUrls = [];
     foreach ($productCollection as $product) {
         if (in_array($product->getId(), $this->isSkippedProduct)) {
             continue;
         }
         $this->isSkippedProduct[] = $product->getId();
         $product->setStoreId($storeId);
         $product->setData('save_rewrites_history', $saveRewriteHistory);
         $productUrls = array_merge($productUrls, $this->productUrlRewriteGenerator->generate($product));
     }
     return $productUrls;
 }

作者:Doabilit    项目:magento2de   
/**
  * @param Category $category
  * @return void
  */
 public function processDelete(Category $category)
 {
     /** @var \Magento\Catalog\Model\ResourceModel\Category $resourceModel */
     $resourceModel = $category->getResource();
     /**
      * Update children count for all parent categories
      */
     $parentIds = $category->getParentIds();
     if ($parentIds) {
         $childDecrease = $category->getChildrenCount() + 1;
         // +1 is itself
         $data = ['children_count' => new \Zend_Db_Expr('children_count - ' . $childDecrease)];
         $where = ['entity_id IN(?)' => $parentIds];
         $resourceModel->getConnection()->update($resourceModel->getEntityTable(), $data, $where);
     }
 }

作者:andrewhowdenco    项目:m2onk8   
public function testIsInRootCategoryList()
 {
     $this->assertFalse($this->_model->isInRootCategoryList());
     $this->_model->unsetData();
     $this->_model->load(3);
     $this->assertTrue($this->_model->isInRootCategoryList());
 }

作者:pradeep-wagent    项目:magento   
public function testGetCategoryWithAppliedId()
 {
     $storeId = 1234;
     $categoryId = 4321;
     $this->store->expects($this->once())->method('getId')->will($this->returnValue($storeId));
     $this->layer->expects($this->any())->method('getCurrentCategory')->will($this->returnValue($this->category));
     $this->category->expects($this->once())->method('setStoreId')->with($this->equalTo($storeId))->will($this->returnSelf());
     $this->category->expects($this->once())->method('load')->with($this->equalTo($categoryId))->will($this->returnSelf());
     $this->category->expects($this->any())->method('getId')->will($this->returnValue($categoryId));
     $this->category->expects($this->any())->method('getPathIds')->will($this->returnValue([20, 10]));
     $this->coreRegistry->expects($this->once())->method('register')->with($this->equalTo('current_category_filter'), $this->equalTo($this->category), $this->equalTo(true))->will($this->returnSelf());
     $this->target->setCategoryId($categoryId);
     $this->assertSame($this->category, $this->target->getCategory());
     $this->assertSame(20, $this->target->getResetValue());
     return $this->target;
 }


问题


面经


文章

微信
公众号

扫码关注公众号