43 \Magento\Framework\DataObject\IdentityInterface,
127 'custom_design_from',
130 'custom_layout_update',
131 'custom_apply_to_products',
242 \
Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
263 $this->_treeModel = $categoryTreeResource;
264 $this->_categoryTreeFactory = $categoryTreeFactory;
265 $this->_storeCollectionFactory = $storeCollectionFactory;
267 $this->_productCollectionFactory = $productCollectionFactory;
268 $this->_catalogConfig = $catalogConfig;
295 if ($this->flatState->isAvailable()) {
297 $this->_useFlatResource =
true;
308 if ($this->customAttributesCodes ===
null) {
309 $this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService);
323 return parent::_getResource();
354 return $this->_categoryTreeFactory->create();
375 public function move($parentId, $afterCategoryId)
382 $parent = $this->categoryRepository->get($parentId, $this->
getStoreId());
384 throw new \Magento\Framework\Exception\LocalizedException(
386 'Sorry, but we can\'t find the new parent category you selected.' 392 if (!$this->
getId()) {
393 throw new \Magento\Framework\Exception\LocalizedException(
394 __(
'Sorry, but we can\'t find the new category you selected.')
397 throw new \Magento\Framework\Exception\LocalizedException(
399 'We can\'t move the category because the parent category name matches the child category name.' 407 $this->setMovedCategoryId($this->
getId());
412 $this->_eventObject => $this,
414 'category_id' => $this->
getId(),
415 'prev_parent_id' => $oldParentId,
416 'parent_id' => $parentId,
421 $this->_eventManager->dispatch($this->_eventPrefix .
'_move_before', $eventParams);
422 $this->
getResource()->changeParent($this, $parent, $afterCategoryId);
423 $this->_eventManager->dispatch($this->_eventPrefix .
'_move_after', $eventParams);
427 $this->setAffectedCategoryIds([$this->
getId(), $oldParentId, $parentId]);
428 }
catch (\Exception $e) {
432 $this->_eventManager->dispatch(
'category_move', $eventParams);
433 if ($this->flatState->isFlatEnabled()) {
434 $flatIndexer = $this->indexerRegistry->get(Indexer\
Category\Flat\State::INDEXER_ID);
435 if (!$flatIndexer->isScheduled()) {
436 $flatIndexer->reindexList([$this->
getId(), $oldParentId, $parentId]);
439 $productIndexer = $this->indexerRegistry->get(Indexer\
Category\Product::INDEXER_ID);
440 if (!$productIndexer->isScheduled()) {
441 $productIndexer->reindexList(array_merge($this->
getPathIds(), $oldParentIds));
443 $this->_eventManager->dispatch(
'clean_cache_by_tags', [
'object' => $this]);
444 $this->_cacheManager->clean([self::CACHE_TAG]);
456 return $this->
getResource()->getEntityType()->getDefaultAttributeSetId();
466 $collection = $this->_productCollectionFactory->create()->setStoreId(
468 )->addCategoryFilter(
486 if ($noDesignAttributes) {
487 foreach (
$result as $k => $a) {
488 if (in_array($k, $this->_designAttributes)) {
507 if (!$this->
getId()) {
511 $array = $this->
getData(
'products_position');
512 if ($array ===
null) {
513 $array = $this->
getResource()->getProductsPosition($this);
514 $this->
setData(
'products_position', $array);
526 if ($this->getInitialSetupFlag()) {
530 $storeIds = $this->
getData(
'store_ids');
535 if (!$this->
getId()) {
545 $storeCollection = $this->_storeCollectionFactory->create()->loadByCategoryIds($nodes);
546 foreach ($storeCollection as
$store) {
551 if (!in_array($entityStoreId, $storeIds)) {
552 array_unshift($storeIds, $entityStoreId);
554 if (!in_array(0, $storeIds)) {
555 array_unshift($storeIds, 0);
558 $this->
setData(
'store_ids', $storeIds);
571 if ($this->
hasData(
'store_id')) {
572 return (
int)$this->
_getData(
'store_id');
574 return (
int)$this->_storeManager->getStore()->getId();
602 Profiler::start(
'REWRITE: ' . __METHOD__, [
'group' =>
'REWRITE',
'method' => __METHOD__]);
605 Profiler::stop(
'REWRITE: ' . __METHOD__);
609 $rewrite = $this->urlFinder->findOneByData([
616 Profiler::stop(
'REWRITE: ' . __METHOD__);
621 Profiler::stop(
'REWRITE: ' . __METHOD__);
634 Profiler::start(
'REGULAR: ' . __METHOD__, [
'group' =>
'REGULAR',
'method' => __METHOD__]);
637 Profiler::stop(
'REGULAR: ' . __METHOD__);
649 return $this->filter->translitUrl($str);
663 $store = $this->_storeManager->getStore();
665 $isRelativeUrl = substr(
$image, 0, 1) ===
'/';
667 $mediaBaseUrl =
$store->getBaseUrl(
668 \
Magento\Framework\UrlInterface::URL_TYPE_MEDIA
671 if ($isRelativeUrl) {
680 throw new \Magento\Framework\Exception\LocalizedException(
681 __(
'Something went wrong while getting the image url.')
695 if (!$this->
hasData(
'parent_category')) {
696 $this->
setData(
'parent_category', $this->categoryRepository->get($this->getParentId()));
698 return $this->
_getData(
'parent_category');
708 $parentId = $this->
getData(self::KEY_PARENT_ID);
709 if (isset($parentId)) {
713 return intval(array_pop($parentIds));
748 foreach ($this->_designAttributes as $attrName) {
749 $result[] = $this->_getAttribute($attrName);
762 if (!$this->_useFlatResource) {
794 public function getChildren($recursive =
false, $isActive =
true, $sortByPosition =
false)
809 foreach (
$path as $itemId) {
810 if ($itemId == $this->_storeManager->getStore()->getRootCategoryId()) {
837 $ids = $this->
getData(
'path_ids');
839 $ids = explode(
'/', $this->
getPath());
840 $this->
setData(
'path_ids', $ids);
852 if (!$this->hasLevel()) {
853 return count(explode(
'/', $this->
getPath())) - 1;
855 return $this->
getData(self::KEY_LEVEL);
876 return $this->
_getResource()->getChildrenAmount($this) > 0;
886 return $this->
_getData(
'request_path');
896 return $this->
_getData(self::KEY_NAME);
908 throw new \Magento\Framework\Exception\LocalizedException(
__(
'Can\'t delete root category.'));
910 return parent::beforeDelete();
927 if ($this->_useFlatResource) {
930 if (!$this->_registry->registry(
'_category_is_anchor_attribute')) {
931 $model = $this->_getAttribute(
'is_anchor');
932 $this->_registry->register(
'_category_is_anchor_attribute',
$model);
935 $isAnchorAttribute = $this->_registry->registry(
'_category_is_anchor_attribute');
936 if ($isAnchorAttribute) {
937 $anchors = $this->
getResource()->findWhereAttributeIs(
$path, $isAnchorAttribute, 1);
950 if (!$this->
hasData(self::KEY_PRODUCT_COUNT)) {
955 return $this->
getData(self::KEY_PRODUCT_COUNT);
968 public function getCategories($parent, $recursionLevel = 0, $sorted =
false, $asCollection =
false, $toLoad =
true)
970 $categories = $this->
getResource()->getCategories($parent, $recursionLevel, $sorted, $asCollection, $toLoad);
981 return $this->
getResource()->getParentCategories($this);
991 return $this->
getResource()->getChildrenCategories($this);
1001 return $this->
getResource()->getParentDesignCategory($this);
1012 return $this->
getResource()->isInRootCategoryList($this);
1022 $available = $this->
getData(self::KEY_AVAILABLE_SORT_BY);
1023 if (empty($available)) {
1026 if ($available && !is_array($available)) {
1027 $available = explode(
',', $available);
1040 $availableSortBy = [];
1041 $defaultSortBy = $this->_catalogConfig->getAttributeUsedForSortByArray();
1044 if (isset($defaultSortBy[$sortBy])) {
1045 $availableSortBy[$sortBy] = $defaultSortBy[$sortBy];
1050 if (!$availableSortBy) {
1051 $availableSortBy = $defaultSortBy;
1054 return $availableSortBy;
1064 if (!($sortBy = $this->
getData(
'default_sort_by'))) {
1065 $sortBy = $this->_catalogConfig->getProductListDefaultSortBy($this->
getStoreId());
1068 if (!isset($available[$sortBy])) {
1069 $sortBy = array_keys($available);
1070 $sortBy = $sortBy[0];
1094 $result = parent::afterSave();
1095 $this->
_getResource()->addCommitCallback([$this,
'reindex']);
1106 if ($this->flatState->isFlatEnabled()) {
1107 $flatIndexer = $this->indexerRegistry->get(Indexer\
Category\Flat\State::INDEXER_ID);
1108 if (!$flatIndexer->isScheduled()) {
1109 $idsList = [$this->
getId()];
1111 $idsList = array_merge($idsList, explode(
',', $this->
getAllChildren()));
1113 $flatIndexer->reindexList($idsList);
1116 $productIndexer = $this->indexerRegistry->get(Indexer\
Category\Product::INDEXER_ID);
1117 if (!$productIndexer->isScheduled()
1118 && (!empty($this->getAffectedProductIds()) || $this->
dataHasChangedFor(
'is_anchor'))
1120 $productIndexer->reindexList($this->
getPathIds());
1132 return parent::afterDeleteCommit();
1143 self::CACHE_TAG .
'_' . $this->
getId(),
1160 return $this->
getData(self::KEY_PATH);
1168 return $this->
getData(self::KEY_POSITION);
1176 return $this->
getData(
'children_count');
1184 return $this->
getData(
'created_at');
1192 return $this->
getData(self::KEY_UPDATED_AT);
1201 return $this->
getData(self::KEY_IS_ACTIVE);
1209 return $this->
getData(
'category_id');
1217 return $this->
getData(
'display_mode');
1225 return $this->
getData(self::KEY_INCLUDE_IN_MENU);
1233 return $this->
getData(
'url_key');
1241 return $this->
getData(self::KEY_CHILDREN_DATA);
1255 $hasToArray =
function (
$model) {
1256 return is_object(
$model) && method_exists(
$model,
'__toArray') && is_callable([
$model,
'__toArray']);
1259 if ($hasToArray(
$value)) {
1262 foreach (
$value as $nestedKey => $nestedValue) {
1263 if ($hasToArray($nestedValue)) {
1264 $value[$nestedKey] = $nestedValue->__toArray();
1278 public function toFlatArray()
1282 if (array_key_exists(
'custom_attributes', $dataArray) && !empty($dataArray[
'custom_attributes'])) {
1284 $customAttributes = $dataArray[
'custom_attributes'];
1285 unset($dataArray[
'custom_attributes']);
1286 foreach ($customAttributes as $attributeValue) {
1304 return $this->
setData(self::KEY_PARENT_ID, $parentId);
1326 return $this->
setData(self::KEY_IS_ACTIVE, $isActive);
1337 return $this->
setData(self::KEY_POSITION, $position);
1348 return $this->
setData(self::KEY_LEVEL, $level);
1357 return $this->
setData(self::KEY_UPDATED_AT, $updatedAt);
1366 return $this->
setData(self::KEY_CREATED_AT, $createdAt);
1384 return $this->
setData(self::KEY_AVAILABLE_SORT_BY, $availableSortBy);
1393 return $this->
setData(self::KEY_INCLUDE_IN_MENU, $includeInMenu);
1404 return $this->
setData(self::KEY_PRODUCT_COUNT, $productCount);
1413 return $this->
setData(self::KEY_CHILDREN_DATA, $childrenData);
getAllChildren($asArray=false)
getAttributes($noDesignAttributes=false)
_getExtensionAttributes()
elseif(isset( $params[ 'redirect_parent']))
getDefaultAttributeSetId()
setChildrenData(array $childrenData=null)
setIncludeInMenu($includeInMenu)
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
setProductCount($productCount)
setData($key, $value=null)
getCategories($parent, $recursionLevel=0, $sorted=false, $asCollection=false, $toLoad=true)
move($parentId, $afterCategoryId)
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Api\CategoryAttributeRepositoryInterface $metadataService, \Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTreeResource, \Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryTreeFactory, \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeCollectionFactory, \Magento\Framework\UrlInterface $url, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Catalog\Model\Config $catalogConfig, \Magento\Framework\Filter\FilterManager $filter, Indexer\Category\Flat\State $flatState, \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator, UrlFinderInterface $urlFinder, \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry, CategoryRepositoryInterface $categoryRepository, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
setAvailableSortBy($availableSortBy)
const CACHE_PRODUCT_CATEGORY_TAG
setExtensionAttributes(\Magento\Catalog\Api\Data\CategoryExtensionInterface $extensionAttributes)
static toFlatArray($data)
getParentDesignCategory()
getChildren($recursive=false, $isActive=true, $sortByPosition=false)
getAvailableSortByOptions()
getData($key='', $index=null)
getCustomAttributesCodes()
dataHasChangedFor($field)
getImageUrl($attributeCode='image')
$_productCollectionFactory
$categoryUrlPathGenerator
isDeleted($isDeleted=null)
if(!isset($_GET['name'])) $name