Magento2: how can i override protected function












0














i want to override CategoryProcessor.php namespace MagentoCatalogImportExportModelImportProduct;



I have created my custom module but not override.



my registration file



<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Magenticians_Showcategory',
__DIR__
);


composer.json



{
"name": "Magenticians/Showcategory",
"description": "remove category on menu when import the product csv",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0"
},
"type": "magento2-module",
"version": "1.0.0",
"license": [
"Commercial"
],
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
"Magenticians\Showcategory\": ""
}
}
}


module.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Magenticians_Showcategory" setup_version="1.0.0">
</module>
</config>


di.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MagentoCatalogImportExportModelImportProductCategoryProcessor" type="MagenticiansShowcategoryModelImportProductCategoryProcessor" />
</config>


CategoryProcessor.php overrided .
"just change true to false in include in menue"



<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MagenticiansShowcategoryModelImportProduct;

/**
* Class CategoryProcessor
*
* @api
* @since 100.0.2
*/
class CategoryProcessor
{
/**
* Delimiter in category path.
*/
const DELIMITER_CATEGORY = '/';

/**
* @var MagentoCatalogModelResourceModelCategoryCollectionFactory
*/
protected $categoryColFactory;

/**
* Categories text-path to ID hash.
*
* @var array
*/
protected $categories = ;

/**
* Categories id to object cache.
*
* @var array
*/
protected $categoriesCache = ;

/**
* Instance of catalog category factory.
*
* @var MagentoCatalogModelCategoryFactory
*/
protected $categoryFactory;

/**
* Failed categories during creation
*
* @var array
* @since 100.1.0
*/
protected $failedCategories = ;

/**
* @param MagentoCatalogModelResourceModelCategoryCollectionFactory $categoryColFactory
* @param MagentoCatalogModelCategoryFactory $categoryFactory
*/
public function __construct(
MagentoCatalogModelResourceModelCategoryCollectionFactory $categoryColFactory,
MagentoCatalogModelCategoryFactory $categoryFactory
) {
$this->categoryColFactory = $categoryColFactory;
$this->categoryFactory = $categoryFactory;
$this->initCategories();
}

/**
* @return $this
*/
protected function initCategories()
{
if (empty($this->categories)) {
$collection = $this->categoryColFactory->create();
$collection->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('url_path');
/* @var $collection MagentoCatalogModelResourceModelCategoryCollection */
foreach ($collection as $category) {
$structure = explode(self::DELIMITER_CATEGORY, $category->getPath());
$pathSize = count($structure);

$this->categoriesCache[$category->getId()] = $category;
if ($pathSize > 1) {
$path = ;
for ($i = 1; $i < $pathSize; $i++) {
$path = $collection->getItemById((int)$structure[$i])->getName();
}
/** @var string $index */
$index = $this->standardizeString(
implode(self::DELIMITER_CATEGORY, $path)
);
$this->categories[$index] = $category->getId();
}
}
}
return $this;
}

/**
* Creates a category.
*
* @param string $name
* @param int $parentId
*
* @return int
*/
protected function createCategory($name, $parentId)
{
/** @var MagentoCatalogModelCategory $category */
$category = $this->categoryFactory->create();
if (!($parentCategory = $this->getCategoryById($parentId))) {
$parentCategory = $this->categoryFactory->create()->load($parentId);
}
$category->setPath($parentCategory->getPath());
$category->setParentId($parentId);
$category->setName($name);
$category->setIsActive(true);
$category->setIncludeInMenu(false);
$category->setAttributeSetId($category->getDefaultAttributeSetId());
try {
$category->save();
$this->categoriesCache[$category->getId()] = $category;
} catch (Exception $e) {
$this->addFailedCategory($category, $e);
}

return $category->getId();
}

/**
* Returns ID of category by string path creating nonexistent ones.
*
* @param string $categoryPath
*
* @return int
*/
protected function upsertCategory($categoryPath)
{
/** @var string $index */
$index = $this->standardizeString($categoryPath);

if (!isset($this->categories[$index])) {
$pathParts = explode(self::DELIMITER_CATEGORY, $categoryPath);
$parentId = MagentoCatalogModelCategory::TREE_ROOT_ID;
$path = '';

foreach ($pathParts as $pathPart) {
$path .= $this->standardizeString($pathPart);
if (!isset($this->categories[$path])) {
$this->categories[$path] = $this->createCategory($pathPart, $parentId);
}
$parentId = $this->categories[$path];
$path .= self::DELIMITER_CATEGORY;
}
}

return $this->categories[$index];
}

/**
* Returns IDs of categories by string path creating nonexistent ones.
*
* @param string $categoriesString
* @param string $categoriesSeparator
*
* @return array
*/
public function upsertCategories($categoriesString, $categoriesSeparator)
{
$categoriesIds = ;
$categories = explode($categoriesSeparator, $categoriesString);

foreach ($categories as $category) {
try {
$categoriesIds = $this->upsertCategory($category);
} catch (MagentoFrameworkExceptionAlreadyExistsException $e) {
$this->addFailedCategory($category, $e);
}
}

return $categoriesIds;
}

/**
* Add failed category
*
* @param string $category
* @param MagentoFrameworkExceptionAlreadyExistsException $exception
*
* @return $this
*/
private function addFailedCategory($category, $exception)
{
$this->failedCategories =
[
'category' => $category,
'exception' => $exception,
];
return $this;
}

/**
* Return failed categories
*
* @return array
* @since 100.1.0
*/
public function getFailedCategories()
{
return $this->failedCategories;
}

/**
* Resets failed categories' array
*
* @return $this
* @since 100.2.0
*/
public function clearFailedCategories()
{
$this->failedCategories = ;
return $this;
}

/**
* Get category by Id
*
* @param int $categoryId
*
* @return MagentoCatalogModelCategory|null
*/
public function getCategoryById($categoryId)
{
return isset($this->categoriesCache[$categoryId]) ? $this->categoriesCache[$categoryId] : null;
}

/**
* Standardize a string.
* For now it performs only a lowercase action, this method is here to include more complex checks in the future
* if needed.
*
* @param string $string
* @return string
*/
private function standardizeString($string)
{
return mb_strtolower($string);
}
}


but can't override the model. correct me where I am wrong?










share|improve this question
























  • Please share the code of MagenticiansShowcategoryModelImportProductCategoryProcessor
    – Amit Bera
    yesterday










  • i have update my question ,, you can see MagenticiansShowcategoryModelImportProductCategoryProcessor code also.
    – Hafiz Arslan
    yesterday










  • You may need to use extends MagentoCatalogImportExportModelImportProductCategoryProcessor in your class definition. It is also good to change only the required function in your override class.
    – Pritam Info 24
    yesterday










  • when i extend ,,class CategoryProcessor extends MagentoCatalogImportExportModelImportProductCategoryProcessor,,it give me an error :Fatal error: Class 'MagenticiansShowcategoryModelImportProductMagentoCatalogImportExportModelImportProductCategoryProcessor' not found in /var/www/html/olympic-supply-company/app/code/Magenticians/Showcategory/Model/Import/Product/CategoryProcessor.php on line 14
    – Hafiz Arslan
    yesterday


















0














i want to override CategoryProcessor.php namespace MagentoCatalogImportExportModelImportProduct;



I have created my custom module but not override.



my registration file



<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Magenticians_Showcategory',
__DIR__
);


composer.json



{
"name": "Magenticians/Showcategory",
"description": "remove category on menu when import the product csv",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0"
},
"type": "magento2-module",
"version": "1.0.0",
"license": [
"Commercial"
],
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
"Magenticians\Showcategory\": ""
}
}
}


module.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Magenticians_Showcategory" setup_version="1.0.0">
</module>
</config>


di.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MagentoCatalogImportExportModelImportProductCategoryProcessor" type="MagenticiansShowcategoryModelImportProductCategoryProcessor" />
</config>


CategoryProcessor.php overrided .
"just change true to false in include in menue"



<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MagenticiansShowcategoryModelImportProduct;

/**
* Class CategoryProcessor
*
* @api
* @since 100.0.2
*/
class CategoryProcessor
{
/**
* Delimiter in category path.
*/
const DELIMITER_CATEGORY = '/';

/**
* @var MagentoCatalogModelResourceModelCategoryCollectionFactory
*/
protected $categoryColFactory;

/**
* Categories text-path to ID hash.
*
* @var array
*/
protected $categories = ;

/**
* Categories id to object cache.
*
* @var array
*/
protected $categoriesCache = ;

/**
* Instance of catalog category factory.
*
* @var MagentoCatalogModelCategoryFactory
*/
protected $categoryFactory;

/**
* Failed categories during creation
*
* @var array
* @since 100.1.0
*/
protected $failedCategories = ;

/**
* @param MagentoCatalogModelResourceModelCategoryCollectionFactory $categoryColFactory
* @param MagentoCatalogModelCategoryFactory $categoryFactory
*/
public function __construct(
MagentoCatalogModelResourceModelCategoryCollectionFactory $categoryColFactory,
MagentoCatalogModelCategoryFactory $categoryFactory
) {
$this->categoryColFactory = $categoryColFactory;
$this->categoryFactory = $categoryFactory;
$this->initCategories();
}

/**
* @return $this
*/
protected function initCategories()
{
if (empty($this->categories)) {
$collection = $this->categoryColFactory->create();
$collection->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('url_path');
/* @var $collection MagentoCatalogModelResourceModelCategoryCollection */
foreach ($collection as $category) {
$structure = explode(self::DELIMITER_CATEGORY, $category->getPath());
$pathSize = count($structure);

$this->categoriesCache[$category->getId()] = $category;
if ($pathSize > 1) {
$path = ;
for ($i = 1; $i < $pathSize; $i++) {
$path = $collection->getItemById((int)$structure[$i])->getName();
}
/** @var string $index */
$index = $this->standardizeString(
implode(self::DELIMITER_CATEGORY, $path)
);
$this->categories[$index] = $category->getId();
}
}
}
return $this;
}

/**
* Creates a category.
*
* @param string $name
* @param int $parentId
*
* @return int
*/
protected function createCategory($name, $parentId)
{
/** @var MagentoCatalogModelCategory $category */
$category = $this->categoryFactory->create();
if (!($parentCategory = $this->getCategoryById($parentId))) {
$parentCategory = $this->categoryFactory->create()->load($parentId);
}
$category->setPath($parentCategory->getPath());
$category->setParentId($parentId);
$category->setName($name);
$category->setIsActive(true);
$category->setIncludeInMenu(false);
$category->setAttributeSetId($category->getDefaultAttributeSetId());
try {
$category->save();
$this->categoriesCache[$category->getId()] = $category;
} catch (Exception $e) {
$this->addFailedCategory($category, $e);
}

return $category->getId();
}

/**
* Returns ID of category by string path creating nonexistent ones.
*
* @param string $categoryPath
*
* @return int
*/
protected function upsertCategory($categoryPath)
{
/** @var string $index */
$index = $this->standardizeString($categoryPath);

if (!isset($this->categories[$index])) {
$pathParts = explode(self::DELIMITER_CATEGORY, $categoryPath);
$parentId = MagentoCatalogModelCategory::TREE_ROOT_ID;
$path = '';

foreach ($pathParts as $pathPart) {
$path .= $this->standardizeString($pathPart);
if (!isset($this->categories[$path])) {
$this->categories[$path] = $this->createCategory($pathPart, $parentId);
}
$parentId = $this->categories[$path];
$path .= self::DELIMITER_CATEGORY;
}
}

return $this->categories[$index];
}

/**
* Returns IDs of categories by string path creating nonexistent ones.
*
* @param string $categoriesString
* @param string $categoriesSeparator
*
* @return array
*/
public function upsertCategories($categoriesString, $categoriesSeparator)
{
$categoriesIds = ;
$categories = explode($categoriesSeparator, $categoriesString);

foreach ($categories as $category) {
try {
$categoriesIds = $this->upsertCategory($category);
} catch (MagentoFrameworkExceptionAlreadyExistsException $e) {
$this->addFailedCategory($category, $e);
}
}

return $categoriesIds;
}

/**
* Add failed category
*
* @param string $category
* @param MagentoFrameworkExceptionAlreadyExistsException $exception
*
* @return $this
*/
private function addFailedCategory($category, $exception)
{
$this->failedCategories =
[
'category' => $category,
'exception' => $exception,
];
return $this;
}

/**
* Return failed categories
*
* @return array
* @since 100.1.0
*/
public function getFailedCategories()
{
return $this->failedCategories;
}

/**
* Resets failed categories' array
*
* @return $this
* @since 100.2.0
*/
public function clearFailedCategories()
{
$this->failedCategories = ;
return $this;
}

/**
* Get category by Id
*
* @param int $categoryId
*
* @return MagentoCatalogModelCategory|null
*/
public function getCategoryById($categoryId)
{
return isset($this->categoriesCache[$categoryId]) ? $this->categoriesCache[$categoryId] : null;
}

/**
* Standardize a string.
* For now it performs only a lowercase action, this method is here to include more complex checks in the future
* if needed.
*
* @param string $string
* @return string
*/
private function standardizeString($string)
{
return mb_strtolower($string);
}
}


but can't override the model. correct me where I am wrong?










share|improve this question
























  • Please share the code of MagenticiansShowcategoryModelImportProductCategoryProcessor
    – Amit Bera
    yesterday










  • i have update my question ,, you can see MagenticiansShowcategoryModelImportProductCategoryProcessor code also.
    – Hafiz Arslan
    yesterday










  • You may need to use extends MagentoCatalogImportExportModelImportProductCategoryProcessor in your class definition. It is also good to change only the required function in your override class.
    – Pritam Info 24
    yesterday










  • when i extend ,,class CategoryProcessor extends MagentoCatalogImportExportModelImportProductCategoryProcessor,,it give me an error :Fatal error: Class 'MagenticiansShowcategoryModelImportProductMagentoCatalogImportExportModelImportProductCategoryProcessor' not found in /var/www/html/olympic-supply-company/app/code/Magenticians/Showcategory/Model/Import/Product/CategoryProcessor.php on line 14
    – Hafiz Arslan
    yesterday
















0












0








0







i want to override CategoryProcessor.php namespace MagentoCatalogImportExportModelImportProduct;



I have created my custom module but not override.



my registration file



<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Magenticians_Showcategory',
__DIR__
);


composer.json



{
"name": "Magenticians/Showcategory",
"description": "remove category on menu when import the product csv",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0"
},
"type": "magento2-module",
"version": "1.0.0",
"license": [
"Commercial"
],
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
"Magenticians\Showcategory\": ""
}
}
}


module.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Magenticians_Showcategory" setup_version="1.0.0">
</module>
</config>


di.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MagentoCatalogImportExportModelImportProductCategoryProcessor" type="MagenticiansShowcategoryModelImportProductCategoryProcessor" />
</config>


CategoryProcessor.php overrided .
"just change true to false in include in menue"



<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MagenticiansShowcategoryModelImportProduct;

/**
* Class CategoryProcessor
*
* @api
* @since 100.0.2
*/
class CategoryProcessor
{
/**
* Delimiter in category path.
*/
const DELIMITER_CATEGORY = '/';

/**
* @var MagentoCatalogModelResourceModelCategoryCollectionFactory
*/
protected $categoryColFactory;

/**
* Categories text-path to ID hash.
*
* @var array
*/
protected $categories = ;

/**
* Categories id to object cache.
*
* @var array
*/
protected $categoriesCache = ;

/**
* Instance of catalog category factory.
*
* @var MagentoCatalogModelCategoryFactory
*/
protected $categoryFactory;

/**
* Failed categories during creation
*
* @var array
* @since 100.1.0
*/
protected $failedCategories = ;

/**
* @param MagentoCatalogModelResourceModelCategoryCollectionFactory $categoryColFactory
* @param MagentoCatalogModelCategoryFactory $categoryFactory
*/
public function __construct(
MagentoCatalogModelResourceModelCategoryCollectionFactory $categoryColFactory,
MagentoCatalogModelCategoryFactory $categoryFactory
) {
$this->categoryColFactory = $categoryColFactory;
$this->categoryFactory = $categoryFactory;
$this->initCategories();
}

/**
* @return $this
*/
protected function initCategories()
{
if (empty($this->categories)) {
$collection = $this->categoryColFactory->create();
$collection->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('url_path');
/* @var $collection MagentoCatalogModelResourceModelCategoryCollection */
foreach ($collection as $category) {
$structure = explode(self::DELIMITER_CATEGORY, $category->getPath());
$pathSize = count($structure);

$this->categoriesCache[$category->getId()] = $category;
if ($pathSize > 1) {
$path = ;
for ($i = 1; $i < $pathSize; $i++) {
$path = $collection->getItemById((int)$structure[$i])->getName();
}
/** @var string $index */
$index = $this->standardizeString(
implode(self::DELIMITER_CATEGORY, $path)
);
$this->categories[$index] = $category->getId();
}
}
}
return $this;
}

/**
* Creates a category.
*
* @param string $name
* @param int $parentId
*
* @return int
*/
protected function createCategory($name, $parentId)
{
/** @var MagentoCatalogModelCategory $category */
$category = $this->categoryFactory->create();
if (!($parentCategory = $this->getCategoryById($parentId))) {
$parentCategory = $this->categoryFactory->create()->load($parentId);
}
$category->setPath($parentCategory->getPath());
$category->setParentId($parentId);
$category->setName($name);
$category->setIsActive(true);
$category->setIncludeInMenu(false);
$category->setAttributeSetId($category->getDefaultAttributeSetId());
try {
$category->save();
$this->categoriesCache[$category->getId()] = $category;
} catch (Exception $e) {
$this->addFailedCategory($category, $e);
}

return $category->getId();
}

/**
* Returns ID of category by string path creating nonexistent ones.
*
* @param string $categoryPath
*
* @return int
*/
protected function upsertCategory($categoryPath)
{
/** @var string $index */
$index = $this->standardizeString($categoryPath);

if (!isset($this->categories[$index])) {
$pathParts = explode(self::DELIMITER_CATEGORY, $categoryPath);
$parentId = MagentoCatalogModelCategory::TREE_ROOT_ID;
$path = '';

foreach ($pathParts as $pathPart) {
$path .= $this->standardizeString($pathPart);
if (!isset($this->categories[$path])) {
$this->categories[$path] = $this->createCategory($pathPart, $parentId);
}
$parentId = $this->categories[$path];
$path .= self::DELIMITER_CATEGORY;
}
}

return $this->categories[$index];
}

/**
* Returns IDs of categories by string path creating nonexistent ones.
*
* @param string $categoriesString
* @param string $categoriesSeparator
*
* @return array
*/
public function upsertCategories($categoriesString, $categoriesSeparator)
{
$categoriesIds = ;
$categories = explode($categoriesSeparator, $categoriesString);

foreach ($categories as $category) {
try {
$categoriesIds = $this->upsertCategory($category);
} catch (MagentoFrameworkExceptionAlreadyExistsException $e) {
$this->addFailedCategory($category, $e);
}
}

return $categoriesIds;
}

/**
* Add failed category
*
* @param string $category
* @param MagentoFrameworkExceptionAlreadyExistsException $exception
*
* @return $this
*/
private function addFailedCategory($category, $exception)
{
$this->failedCategories =
[
'category' => $category,
'exception' => $exception,
];
return $this;
}

/**
* Return failed categories
*
* @return array
* @since 100.1.0
*/
public function getFailedCategories()
{
return $this->failedCategories;
}

/**
* Resets failed categories' array
*
* @return $this
* @since 100.2.0
*/
public function clearFailedCategories()
{
$this->failedCategories = ;
return $this;
}

/**
* Get category by Id
*
* @param int $categoryId
*
* @return MagentoCatalogModelCategory|null
*/
public function getCategoryById($categoryId)
{
return isset($this->categoriesCache[$categoryId]) ? $this->categoriesCache[$categoryId] : null;
}

/**
* Standardize a string.
* For now it performs only a lowercase action, this method is here to include more complex checks in the future
* if needed.
*
* @param string $string
* @return string
*/
private function standardizeString($string)
{
return mb_strtolower($string);
}
}


but can't override the model. correct me where I am wrong?










share|improve this question















i want to override CategoryProcessor.php namespace MagentoCatalogImportExportModelImportProduct;



I have created my custom module but not override.



my registration file



<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Magenticians_Showcategory',
__DIR__
);


composer.json



{
"name": "Magenticians/Showcategory",
"description": "remove category on menu when import the product csv",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0"
},
"type": "magento2-module",
"version": "1.0.0",
"license": [
"Commercial"
],
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
"Magenticians\Showcategory\": ""
}
}
}


module.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Magenticians_Showcategory" setup_version="1.0.0">
</module>
</config>


di.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MagentoCatalogImportExportModelImportProductCategoryProcessor" type="MagenticiansShowcategoryModelImportProductCategoryProcessor" />
</config>


CategoryProcessor.php overrided .
"just change true to false in include in menue"



<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MagenticiansShowcategoryModelImportProduct;

/**
* Class CategoryProcessor
*
* @api
* @since 100.0.2
*/
class CategoryProcessor
{
/**
* Delimiter in category path.
*/
const DELIMITER_CATEGORY = '/';

/**
* @var MagentoCatalogModelResourceModelCategoryCollectionFactory
*/
protected $categoryColFactory;

/**
* Categories text-path to ID hash.
*
* @var array
*/
protected $categories = ;

/**
* Categories id to object cache.
*
* @var array
*/
protected $categoriesCache = ;

/**
* Instance of catalog category factory.
*
* @var MagentoCatalogModelCategoryFactory
*/
protected $categoryFactory;

/**
* Failed categories during creation
*
* @var array
* @since 100.1.0
*/
protected $failedCategories = ;

/**
* @param MagentoCatalogModelResourceModelCategoryCollectionFactory $categoryColFactory
* @param MagentoCatalogModelCategoryFactory $categoryFactory
*/
public function __construct(
MagentoCatalogModelResourceModelCategoryCollectionFactory $categoryColFactory,
MagentoCatalogModelCategoryFactory $categoryFactory
) {
$this->categoryColFactory = $categoryColFactory;
$this->categoryFactory = $categoryFactory;
$this->initCategories();
}

/**
* @return $this
*/
protected function initCategories()
{
if (empty($this->categories)) {
$collection = $this->categoryColFactory->create();
$collection->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('url_path');
/* @var $collection MagentoCatalogModelResourceModelCategoryCollection */
foreach ($collection as $category) {
$structure = explode(self::DELIMITER_CATEGORY, $category->getPath());
$pathSize = count($structure);

$this->categoriesCache[$category->getId()] = $category;
if ($pathSize > 1) {
$path = ;
for ($i = 1; $i < $pathSize; $i++) {
$path = $collection->getItemById((int)$structure[$i])->getName();
}
/** @var string $index */
$index = $this->standardizeString(
implode(self::DELIMITER_CATEGORY, $path)
);
$this->categories[$index] = $category->getId();
}
}
}
return $this;
}

/**
* Creates a category.
*
* @param string $name
* @param int $parentId
*
* @return int
*/
protected function createCategory($name, $parentId)
{
/** @var MagentoCatalogModelCategory $category */
$category = $this->categoryFactory->create();
if (!($parentCategory = $this->getCategoryById($parentId))) {
$parentCategory = $this->categoryFactory->create()->load($parentId);
}
$category->setPath($parentCategory->getPath());
$category->setParentId($parentId);
$category->setName($name);
$category->setIsActive(true);
$category->setIncludeInMenu(false);
$category->setAttributeSetId($category->getDefaultAttributeSetId());
try {
$category->save();
$this->categoriesCache[$category->getId()] = $category;
} catch (Exception $e) {
$this->addFailedCategory($category, $e);
}

return $category->getId();
}

/**
* Returns ID of category by string path creating nonexistent ones.
*
* @param string $categoryPath
*
* @return int
*/
protected function upsertCategory($categoryPath)
{
/** @var string $index */
$index = $this->standardizeString($categoryPath);

if (!isset($this->categories[$index])) {
$pathParts = explode(self::DELIMITER_CATEGORY, $categoryPath);
$parentId = MagentoCatalogModelCategory::TREE_ROOT_ID;
$path = '';

foreach ($pathParts as $pathPart) {
$path .= $this->standardizeString($pathPart);
if (!isset($this->categories[$path])) {
$this->categories[$path] = $this->createCategory($pathPart, $parentId);
}
$parentId = $this->categories[$path];
$path .= self::DELIMITER_CATEGORY;
}
}

return $this->categories[$index];
}

/**
* Returns IDs of categories by string path creating nonexistent ones.
*
* @param string $categoriesString
* @param string $categoriesSeparator
*
* @return array
*/
public function upsertCategories($categoriesString, $categoriesSeparator)
{
$categoriesIds = ;
$categories = explode($categoriesSeparator, $categoriesString);

foreach ($categories as $category) {
try {
$categoriesIds = $this->upsertCategory($category);
} catch (MagentoFrameworkExceptionAlreadyExistsException $e) {
$this->addFailedCategory($category, $e);
}
}

return $categoriesIds;
}

/**
* Add failed category
*
* @param string $category
* @param MagentoFrameworkExceptionAlreadyExistsException $exception
*
* @return $this
*/
private function addFailedCategory($category, $exception)
{
$this->failedCategories =
[
'category' => $category,
'exception' => $exception,
];
return $this;
}

/**
* Return failed categories
*
* @return array
* @since 100.1.0
*/
public function getFailedCategories()
{
return $this->failedCategories;
}

/**
* Resets failed categories' array
*
* @return $this
* @since 100.2.0
*/
public function clearFailedCategories()
{
$this->failedCategories = ;
return $this;
}

/**
* Get category by Id
*
* @param int $categoryId
*
* @return MagentoCatalogModelCategory|null
*/
public function getCategoryById($categoryId)
{
return isset($this->categoriesCache[$categoryId]) ? $this->categoriesCache[$categoryId] : null;
}

/**
* Standardize a string.
* For now it performs only a lowercase action, this method is here to include more complex checks in the future
* if needed.
*
* @param string $string
* @return string
*/
private function standardizeString($string)
{
return mb_strtolower($string);
}
}


but can't override the model. correct me where I am wrong?







magento2 category import topmenu






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday

























asked yesterday









Hafiz Arslan

459




459












  • Please share the code of MagenticiansShowcategoryModelImportProductCategoryProcessor
    – Amit Bera
    yesterday










  • i have update my question ,, you can see MagenticiansShowcategoryModelImportProductCategoryProcessor code also.
    – Hafiz Arslan
    yesterday










  • You may need to use extends MagentoCatalogImportExportModelImportProductCategoryProcessor in your class definition. It is also good to change only the required function in your override class.
    – Pritam Info 24
    yesterday










  • when i extend ,,class CategoryProcessor extends MagentoCatalogImportExportModelImportProductCategoryProcessor,,it give me an error :Fatal error: Class 'MagenticiansShowcategoryModelImportProductMagentoCatalogImportExportModelImportProductCategoryProcessor' not found in /var/www/html/olympic-supply-company/app/code/Magenticians/Showcategory/Model/Import/Product/CategoryProcessor.php on line 14
    – Hafiz Arslan
    yesterday




















  • Please share the code of MagenticiansShowcategoryModelImportProductCategoryProcessor
    – Amit Bera
    yesterday










  • i have update my question ,, you can see MagenticiansShowcategoryModelImportProductCategoryProcessor code also.
    – Hafiz Arslan
    yesterday










  • You may need to use extends MagentoCatalogImportExportModelImportProductCategoryProcessor in your class definition. It is also good to change only the required function in your override class.
    – Pritam Info 24
    yesterday










  • when i extend ,,class CategoryProcessor extends MagentoCatalogImportExportModelImportProductCategoryProcessor,,it give me an error :Fatal error: Class 'MagenticiansShowcategoryModelImportProductMagentoCatalogImportExportModelImportProductCategoryProcessor' not found in /var/www/html/olympic-supply-company/app/code/Magenticians/Showcategory/Model/Import/Product/CategoryProcessor.php on line 14
    – Hafiz Arslan
    yesterday


















Please share the code of MagenticiansShowcategoryModelImportProductCategoryProcessor
– Amit Bera
yesterday




Please share the code of MagenticiansShowcategoryModelImportProductCategoryProcessor
– Amit Bera
yesterday












i have update my question ,, you can see MagenticiansShowcategoryModelImportProductCategoryProcessor code also.
– Hafiz Arslan
yesterday




i have update my question ,, you can see MagenticiansShowcategoryModelImportProductCategoryProcessor code also.
– Hafiz Arslan
yesterday












You may need to use extends MagentoCatalogImportExportModelImportProductCategoryProcessor in your class definition. It is also good to change only the required function in your override class.
– Pritam Info 24
yesterday




You may need to use extends MagentoCatalogImportExportModelImportProductCategoryProcessor in your class definition. It is also good to change only the required function in your override class.
– Pritam Info 24
yesterday












when i extend ,,class CategoryProcessor extends MagentoCatalogImportExportModelImportProductCategoryProcessor,,it give me an error :Fatal error: Class 'MagenticiansShowcategoryModelImportProductMagentoCatalogImportExportModelImportProductCategoryProcessor' not found in /var/www/html/olympic-supply-company/app/code/Magenticians/Showcategory/Model/Import/Product/CategoryProcessor.php on line 14
– Hafiz Arslan
yesterday






when i extend ,,class CategoryProcessor extends MagentoCatalogImportExportModelImportProductCategoryProcessor,,it give me an error :Fatal error: Class 'MagenticiansShowcategoryModelImportProductMagentoCatalogImportExportModelImportProductCategoryProcessor' not found in /var/www/html/olympic-supply-company/app/code/Magenticians/Showcategory/Model/Import/Product/CategoryProcessor.php on line 14
– Hafiz Arslan
yesterday












0






active

oldest

votes











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f256778%2fmagento2-how-can-i-override-protected-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f256778%2fmagento2-how-can-i-override-protected-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1300-talet

1300-talet

Display a custom attribute below product name in the front-end Magento 1.9.3.8