0

Ive been through several answers before succumbing to ask a question. I will list what i tried and why i think this is a bit of a special case, and not a duplicate.

Am reading a .csv file from my customer. Each product has 3 fields for the category (among other fields, sku, name, weight etc).

[ParentCategoryType] => CLOTHING
[ParentCategory] => SHIRTS
[Category] => SHIRTS CASUAL
[Gender] => MEN

What I want is to create the category if it does not exist, in the tree. I.e. The current item's tree is

Men/Clothing/Shirts/Shirts Casual

but there is also a similar category starting with Women/Clothing/Shirts/Shirts Casual

I would like to check if given the current tree, there is such a category and if not add it. I realize maybe this will need to be recursive, so it can add the categories in between. For example maybe Men/Clothing/Shirts doesn't exist, and needs to add Shirts, and then Shirts Casual

I tried:

  • This answer here , which causes error 500
  • This answer here, which kinda works (it adds a category so if i recurse my list maybe ill manage) BUT this seems messy and maybe does stuff it doesn't need.
  • and many other similar, others being very vague and others outdated.


tl;dr

Given the string Men/Clothing/Shirts/Shirts Casual

I want to create the tree accordingly, so when given Men/Clothing/Pants, only pants should be created, and when given Women/Clothing/Shirts/Shirts Casual, another tree with root Women is created

0

What I ended up using.. don't know if its the best way, but am doing a recursion in the "path" i give as a parameter

The function:

$path = 'Men/Clothing/Shirts/Shirts Casual';
$parentId=2;
recursivelyCreateCategories($path, $parentId);


protected function recursivelyCreateCategories($str, $parentId) {
        $ar = explode("/", $str);
        if(empty($ar) || $str == '') {
            return;
        }
        //check if name = $ar[0] with $parId exists
        $name = $ar[0];
        if($name == '') {
            return;
        }
        $category = $this->_objectManager
            ->create('Magento\Catalog\Model\Category');
        $cate = $category->getCollection()
            ->addAttributeToFilter('name', $name)
            ->addAttributeToFilter('parent_id', $parentId)
            ->getFirstItem();

        $cate_exists = $cate->getId();

        //$exists_id = checkCategoryExists(); //returns id if exists, false if not
        if(isset($cate_exists)) {
            unset($ar[0]);
            $ar = implode("/", $ar);

            echo 'Parent exists with id: ' . $cate_exists . "\n";
            if($ar) {
                $this->recursivelyCreateCategories($ar, $cate_exists);
            } else {
                return;
            }
        } else {
            //create
            //run function
            //$create_id is newly created category id
            $category = array('category_name' => $name, 'category_parentId' => $parentId);
            $cat_to_pass = $this->prepareData($category);
            $create_id = $this->createCategory($cat_to_pass);
            if($create_id) {
                //category created
                echo 'Category ' . $category['category_name'] . ' created' . "\n";
            } else {
                //failed to create category
                echo 'Failed to create category: ' . $category['category_name'] . "\n";
            }

            unset($ar[0]);
            $ar = implode("/", $ar); //Sub1/Sub2/Sub3';
            if($ar) {
                $this->recursivelyCreateCategories($ar, $create_id);
            } else {
                return;
            }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.