Question

How can we include category_ids in product collection getData? Join field didn't work for me.

For example, look at the below code

Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('color','left')
            ->addAttributeToSelect('footerwear_style','left')
            ->addAttributeToSelect('footwear_common_features','left')
            ->addAttributeToSelect('keywords_for_search','left')
            ->addAttributeToSelect('url_path', 'left')
            ->addAttributeToSelect('image', 'left')
            ->getData();

Not returning category_ids, See the result i got

{
0 => [
  attribute_set_id: "4"
 color: null
 created_at: "2020-05-28 03:04:52"
 entity_id: "40"
 entity_type_id: "4"
 final_price: "0.0000"
 footerwear_style: "50,53,59,63"
 footwear_common_features: "111,194,118"
 has_options: "1"
 image: "/3/5/35500249_g_1400x1630_4.jpg"
 keywords_for_search: "35500249, 00249, GB00249, Georgia, Georgia Boots, Georgia Boot Georgia Giant Waterproof Work Boot, black work boots, work boots, waterproof work boots, black waterproof work boots, waterproof boots, 6" soft toe boots, 6 inch soft toe boots, 6" boots, 6 inch boots, soft toe work boots, comfortable boots, quality work boots, slip resistant work boots, leather work boots, boots, lace up boots, botas de Trabajo con punta suave, botas impermeables, botas de trabajo impermeables, botas de trabajo comodas, botas antideslizante
↵
↵
↵
↵
↵"
max_price: "144.9800"
min_price: "144.9800"
minimal_price: "144.9800"
name: "Georgia Giant Waterproof Work Boot"
price: "0.0000"
required_options: "1"
sku: "35500249"
tax_class_id: "4"
tier_price: null
type_id: "configurable"
updated_at: "2020-05-28 03:04:52"
url_path: "georgia-giant-waterproof-work-boot"
visibility: "4"
]  
}

Please advise. Thanks

Was it helpful?

Solution

Since the category assignments are stored in a separate table with a many-to-many relation between products and categories I would suggest to use MySQL's GROUP_CONCAT function.

Something like this should solve your problem:

$collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('color','left')
            ->addAttributeToSelect('footerwear_style','left')
            ->addAttributeToSelect('footwear_common_features','left')
            ->addAttributeToSelect('keywords_for_search','left')
            ->addAttributeToSelect('url_path', 'left')
            ->addAttributeToSelect('image', 'left');

$collection->joinField(
            'category_ids',
            'catalog/category_product',
            new Zend_Db_Expr("GROUP_CONCAT(category_id SEPARATOR ',')"),
            'product_id = entity_id',
            null,
            'left');

$collection->getSelect()->group('entity_id');           

$collection->getData();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top