65 $this->initCategories();
73 protected function initCategories()
75 if (empty($this->categories)) {
78 ->addAttributeToSelect(
'url_key')
79 ->addAttributeToSelect(
'url_path');
83 $structure = explode(self::DELIMITER_CATEGORY,
$category->getPath());
84 $pathSize = count($structure);
89 for (
$i = 1;
$i < $pathSize;
$i++) {
94 $index = $this->standardizeString(
95 implode(self::DELIMITER_CATEGORY,
$path)
111 protected function createCategory(
$name, $parentId)
114 $category = $this->categoryFactory->create();
116 $parentCategory = $this->categoryFactory->create()->load($parentId);
118 $category->setPath($parentCategory->getPath());
135 protected function upsertCategory($categoryPath)
138 $index = $this->standardizeString($categoryPath);
140 if (!isset($this->categories[
$index])) {
141 $pathParts = preg_split(
'~(?<!\\\)' . preg_quote(self::DELIMITER_CATEGORY,
'~') .
'~', $categoryPath);
145 foreach ($pathParts as $pathPart) {
146 $path .= $this->standardizeString($pathPart);
147 if (!isset($this->categories[
$path])) {
148 $this->categories[
$path] = $this->createCategory($pathPart, $parentId);
150 $parentId = $this->categories[
$path];
155 return $this->categories[
$index];
168 $categories = explode($categoriesSeparator, $categoriesString);
172 $categoriesIds[] = $this->upsertCategory(
$category);
173 }
catch (\
Magento\Framework\Exception\AlreadyExistsException $e) {
178 return $categoriesIds;
189 private function addFailedCategory(
$category, $exception)
191 $this->failedCategories[] =
194 'exception' => $exception,
218 $this->failedCategories = [];
231 return $this->categoriesCache[$categoryId] ??
null;
242 private function standardizeString(
$string)
253 private function quoteDelimiter(
$string)
255 return str_replace(self::DELIMITER_CATEGORY,
'\\' . self::DELIMITER_CATEGORY,
$string);
264 private function unquoteDelimiter(
$string)
266 return str_replace(
'\\' . self::DELIMITER_CATEGORY, self::DELIMITER_CATEGORY,
$string);
getCategoryById($categoryId)
__construct(\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryColFactory, \Magento\Catalog\Model\CategoryFactory $categoryFactory)
upsertCategories($categoriesString, $categoriesSeparator)
if(!isset($_GET['name'])) $name