By default, you can load product category IDs using this:

$product = Mage::getModel('catalog/product')->load($_productId);
$category_ids = $product->getCategoryIds();

But how can we load the category IDs without loading the whole product model? Note that getCategoryIds() is a function of product model not data.


You could do something like this:

$product = Mage::getModel('catalog/product');
$categoryIds = $product->getResource()->getCategoryIds($product);

Just mentioning it here since this would be possible too (because getCategoryIds actually does need the product's Id (pulls it from $product->getId()) from the whole submitted model only):

$obj = new Varien_Object();
$cats = Mage::getResourceSingleton('catalog/product')->getCategoryIds($obj->setId($productId));

For the record, that's the getCategoryIds method from Mage_Catalog_Model_Resource_Product:

     * Retrieve product category identifiers
     * @param Mage_Catalog_Model_Product $product
     * @return array
    public function getCategoryIds($product)
        $adapter = $this->_getReadAdapter();

        $select = $adapter->select()
            ->from($this->_productCategoryTable, 'category_id')
            ->where('product_id = ?', (int)$product->getId());

        return $adapter->fetchCol($select);

relation between product and category manage it in catalog_category_product

you can use this sql query

select cc.* from catalog_category_entity cc
   join catalog_category_product cp on cc.entity_id = cp.category_id
   where cp.product_id = {{your product id}};

see Josep's answer here

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
$categories = $product->getCategoryIds(); /*will return category ids array*/

Due to getCategoryIds() logic in Mage_Catalog_Model_Product it's enough to have product ID as stated before in Christoph Farnleitner answer so it can be as easy as:


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.