public function productDataToJson(Varien_Event_Observer $observer)
    $categoryModel = Mage::getModel('catalog/category');
    $treeModel = $categoryModel->getTreeModel();
    $categoryIds = $treeModel->getCollection()->getAllIds();
    $file = new Varien_Io_File();
    $jsonData = [];

    $productCollection = Mage::getResourceModel('catalog/product_collection');
        'category_id', 'catalog/category_product', 'category_id',
        'product_id = entity_id', null, 'left'

        ->addAttributeToFilter('category_id', [
            ['in' => $categoryIds]

     * Collect all the products we need to generate the required JSON
    foreach ($productCollection as $product) {
        $productData = $product->getData();

        foreach ($productData as $attrCode => $attrVal) {
            if ($attrText = $product->getAttributeText($attrCode)) {
                $productData[$attrCode] = $attrText;

        $jsonData[] = array_merge(
                'category_ids' => $product->getCategoryIds()

     * Split the results back out into the format you need
    foreach ($categoryIds as $categoryId) {
        $categoryProducts = [];
        array_filter($jsonData, function ($item) use($categoryId, &$categoryProducts) {
            if (array_search($categoryId, $item['category_ids']) !== FALSE) {
                $categoryProducts[] = $item;

        $jsonResult = Mage::helper('core')->jsonEncode($jsonData);

            'path' => Mage::getBaseDir() . '/jsondata/'

        $file->write('/c_' . $categoryId . '_node.json', $jsonResult);

I am getting all product details of particular category in different json files, need attribute texts of all attributes also dynamically created attributes. now it shows me this error :

Item (Mage_Catalog_Model_Product) with the same id "5" already exist

how can I solve this and get attribute labes and values in my json file?

3 Answers 3


I was facing the same problem. I have tried below solution its worked for me.


Edit : /www/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php Line: 256

Replace: return parent::addItem($object);

With: try { return parent::addItem($object); } catch (Exception $ex) { }

The group clause does the trick in overcoming duplicate product id's that get returned.


If the issue not solved then add distinct(true) before join


distinct(true) had solved my problem. Hope it may be helpful.


you see this error cause you join categories, and in result you have few items with same ID To prevent it you can group collection. Try to add


before foreach

  • Call to a member function getSource() on boolean in /opt/... error occured :( Jul 26, 2017 at 11:54

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.