Adding Extra Attributes to All Product Collections

This can be very useful for attributes which need to be available on all product collections. This is to be used sparingly, however – adding a lot of attributes to a product collection can drastically impact performance; all attributes should also be indexed into the flat tables so that they’re available when flat catalog is on.

Config

<frontend>
	<events>
		<catalog_product_collection_load_before>
			<observers>
				<llapgoch_pcattributes_add>
					<model>llapgoch_addpcattributes/observer</model>
					<method>addAttributes</method>
					<type>model</type>
				</llapgoch_pcattributes_add>
			</observers>
		</catalog_product_collection_load_before>
	</events>
</frontend>

<default>
	<llapgoch_addpcattributes>
		<general>
			<attributes></attributes>
		</general>
	</llapgoch_addpcattributes>
</default>

– We add an observer to the catalog_product_collection_load_before event. This will make sure our attributes are added to the collection wherever it’s instantiated from.
– We also add a default node as a placeholder – we’ll allow loaded attributes to be set via the admin’s system configuration.

Observer

<?php
class Llapgoch_AddProductCollectionAttributes_Model_Observer{
	public function addAttributes($observer){
		$attrs = Mage::helper('llapgoch_addpcattributes')->getAttributesToAdd();
		
		if(is_array($attrs) && count($attrs)){
			$observer->getCollection()->addAttributeToSelect($attrs);
		}
	}
}

– All we need to do is get the attributes we’d like to add to the collection and add them to the select object.

Helper

<?php
class Llapgoch_AddProductCollectionAttributes_Helper_Data extends Mage_Core_Helper_Abstract{
	const XML_PATH_PRODUCT_ATTRIBUTES = "llapgoch_addpcattributes/general/attributes";
	
	public function getAttributesToAdd(){
		$attrs = explode(" ", Mage::getStoreConfig(self::XML_PATH_PRODUCT_ATTRIBUTES));
		
		if(count($attrs)){
			return $attrs;
		}
		
		return false;
	}
		
}

– We load the attributes which can either be set in the admin area (see System Configuration) or overridden by another module’s config.xml.
– We split the attributes into an array using spaces, but this could be any character.

System Configuration

<?xml version="1.0"?>
<config>
	<tabs>
		<llapgoch translate="label">
			<label>LLAP-Goch</label>
			<sort_order>100</sort_order>
		</llapgoch>
	</tabs>
	<sections>
		<llapgoch_addpcattributes translate="label" module="llapgoch_addpcattributes">
			<label>Product Attributes</label>
			<tab>llapgoch</tab>
			
			<sort_order>1</sort_order>
			<show_in_default>1</show_in_default>
			<show_in_website>1</show_in_website>
			<show_in_store>1</show_in_store>
			
			<groups>
				<general translate="label">
					<label>General</label>

					<sort_order>10</sort_order>
					<show_in_default>1</show_in_default>
					<show_in_website>1</show_in_website>
					<show_in_store>1</show_in_store>
					<fields>
						<attributes translate="label">
							<label>Collection Attributes To Add</label>
							<comment>Space separate attributes to be added to every product collection</comment>
							<frontend_type>text</frontend_type>
							<backend_model>llapgoch_addpcattributes/system_config_backend_attributestring</backend_model>
							<sort_order>1</sort_order>
							<show_in_default>1</show_in_default>
							<show_in_website>1</show_in_website>
							<show_in_store>1</show_in_store>
						</attributes>
					</fields>
				</general>
			</groups>
		</llapgoch_addpcattributes>
	</sections>
</config>

– We just add our field as a text area to the system config.
– We use a backend model which will be used to warn the user if commas are detected in the string because we’ve chosen to split attributes on spaces.

Backend Model

<?php
class Llapgoch_AddProductCollectionAttributes_Model_System_Config_Backend_Attributestring extends Mage_Core_Model_Config_Data{
	public function _afterSave(){
		$helper = Mage::helper('llapgoch_addpcattributes');
		
		if(strpos($this->getValue(), ",") !== false){
			 Mage::getSingleton('core/session')->addNotice($helper->__('Please use spaces to separate your attribute names instead of commas'));
		}
		
		parent::_afterSave();
		
	}
}

– Checks the string for the existence of spaces and adds a notice for the user if so.

This code is available as a complete module here: https://github.com/llapgoch/magento-add-attributes-to-product-collection