0

So I am adding products programmatically, in my csv I have the category path which would look like,

Clothing/Shirts/Sweaters

Now using this how do I look up the category id? As far as I know the only way to assign a product a category is using,

->setCategoryIds(array(1));

Pulling the data from the database looks very tedious, as my above example is three sub categories deep. Is there a builtin Magento function that will make this possible?

2 Answers 2

0

You can read it from the core_url_rewrite table, giving request path as "clothing/shirts/sweaters.html". (Replace space with hyphen like "clothing/t-shirts/sweaters.html") You may have to do that recursively to reach the direct path to category though.

The right way though is to read all categories using helper class, and then search within it for the required category. This answer gives some information on how to do it.

0

There is a snippet for that, you could make it a helper since helper is singleton pattern and finder _initCategories will run just once:

https://gist.github.com/jzahedieh/6884204

public function getIdFromPath($string)
{
    if (in_array($string, array_keys($this->_categories))) {
        return $this->_categories[$string];
    }
    return false;
}

protected function _initCategories()
{
    $collection = Mage::getResourceModel('catalog/category_collection')->addNameToResult();
    /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
    foreach ($collection as $category) {
        $structure = explode('/', $category->getPath());
        $pathSize = count($structure);
        if ($pathSize > 1) {
            $path = array();
            for ($i = 1; $i < $pathSize; $i++) {
                $path[] = $collection->getItemById($structure[$i])->getName();
            }
            $rootCategoryName = array_shift($path);
            if (!isset($this->_categoriesWithRoots[$rootCategoryName])) {
                $this->_categoriesWithRoots[$rootCategoryName] = array();
            }
            $index = implode('/', $path);
            $this->_categoriesWithRoots[$rootCategoryName][$index] = $category->getId();
            if ($pathSize > 2) {
                $this->_categories[$index] = $category->getId();
            }
        }
    }
    return $this;
}
4
  • I get error, Fatal error: Call to a member function getIdFromPath() on a non-object in when I run, $fix = Mage::getModel('your_module/category_finder') ->getIdFromPath('Shop by Category/Apparel/T-Shirts'); Nov 28, 2017 at 12:48
  • Your Mage::getModel('your_module/category_finder') is not an object, maube you have a typo or some error in declaration. Do you have global -> models in your config xml? do you have the file placed in app/code/local/Your/Module/Model/Category/Finder? have you updated the class name for the model? Nov 28, 2017 at 13:09
  • Hi, have you been able to fix the issue with object manager not finding the class? If this answer have been in help could you mark it as the accepted answer? Nov 28, 2017 at 19:12
  • I believe this is the correct way to do it, however I do not know how to create a custom module to get it working to test Nov 29, 2017 at 3:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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