Magento 1.9 Product Model – How to Get Attribute Labels

attributesevent-observerjsonmagento-1.9

public function productDataToJson(Varien_Event_Observer $observer)
{
  $category = Mage::getModel('catalog/category');
  $tree = $category->getTreeModel();
  $tree->load();
  $ids = $tree->getCollection()->getAllIds();
  $jsonFilePath = Mage::getBaseDir().'/jsondata';
  foreach ($ids as $id) {
    $array = array();
    $products = $category->load($id)
        ->getProductCollection()
         ->addAttributeToSelect('*')
         ->addStoreFilter(Mage::app()->getStore()->getId());
    foreach ($products as $_product) {
        $array[] = array_merge($_product->getData(), array('category_ids' => $_product->getCategoryIds()));
    }
    $jsonData = Mage::helper('core')->jsonEncode($array);
    file_put_contents($jsonFilePath.'/c_' . $id . '_node.json', $jsonData);
  }
}

I have this function in an observer which creates a JSON file of all products in every category. I am trying to get the labels for the product attributes as well as the product price with appropriate currency. However, instead of getting the label I am getting the option ID.

How can I get the label for the attribute rather than the option ID, and the products price with the appropriate currency?

Best Answer

You could try this as well - IMO it's probably a lot more performant because of the fact that it's reducing the amount of queries being issued when generating your list. Let me know if you have any issues as I wasn't able to validate it.

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');
    $productCollection->joinField(
        'category_id', 'catalog/category_product', 'category_id',
        'product_id = entity_id', null, 'left'
    );

    $productCollection->addAttributeToSelect('*')
        ->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(
            $productData,
            [
                '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);

        $file->open([
            'path' => Mage::getBaseDir() . '/jsondata/'
        ]);

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