1

I want to get the comparable attributes for a product but without the help of the compare session.

I have product id and I need the list of comparable attributes that are associated with the product

4
  • Is there anyway you can clarify your question? Do you only need comparable attributes on one product? Sep 8 '14 at 16:10
  • i have list of product ids and i want this each products comparaple attributes so that i can show on the page.i am know we can get this on the compare list page but i am not using the compare session.i need just attributes Sep 8 '14 at 16:12
  • So you want to select the attributes for a product that are comparable correct? Sep 8 '14 at 16:28
  • yes.............. Sep 8 '14 at 17:18
2

I think, that you should look at method getComparableAttributes() of class Mage_Catalog_Model_Resource_Product_Compare_Item_Collection

public function getComparableAttributes()
{
    if (is_null($this->_comparableAttributes)) {
        $this->_comparableAttributes = array();
        $setIds = $this->_getAttributeSetIds();
        if ($setIds) {
            $attributeIds = $this->_getAttributeIdsBySetIds($setIds);

            $select = $this->getConnection()->select()
                ->from(array('main_table' => $this->getTable('eav/attribute')))
                ->join(
                    array('additional_table' => $this->getTable('catalog/eav_attribute')),
                    'additional_table.attribute_id=main_table.attribute_id'
                )
                ->joinLeft(
                    array('al' => $this->getTable('eav/attribute_label')),
                    'al.attribute_id = main_table.attribute_id AND al.store_id = ' . (int) $this->getStoreId(),
                    array('store_label' => $this->getConnection()->getCheckSql('al.value IS NULL', 'main_table.frontend_label', 'al.value'))
                )
                ->where('additional_table.is_comparable=?', 1)
                ->where('main_table.attribute_id IN(?)', $attributeIds);
            $attributesData = $this->getConnection()->fetchAll($select);
            if ($attributesData) {
                $entityType = Mage_Catalog_Model_Product::ENTITY;
                Mage::getSingleton('eav/config')
                    ->importAttributesData($entityType, $attributesData);
                foreach ($attributesData as $data) {
                    $attribute = Mage::getSingleton('eav/config')
                        ->getAttribute($entityType, $data['attribute_code']);
                    $this->_comparableAttributes[$attribute->getAttributeCode()] = $attribute;
                }
                unset($attributesData);
            }
        }
    }
    return $this->_comparableAttributes;
}

as you can see can see, all attributes which option is_comparable can be used for compare.

According to it, you should get all proruduct's attributes with is_comparable=1

Foe exmaple, you can get something like:

/** @var Mage_Catalog_Model_Resource_Product_Attribute_Collection $attributes */
$attributes = Mage::getResourceModel('catalog/product_attribute_collection');
$attributes->addFieldToFilter('additional_table.is_comparable', array('1'));

/** @var Mage_Catalog_Model_Resource_Product_Collection $collection */
$collection = Mage::getModel('catalog/product')
    ->getCollection();
foreach ($attributes->getItems() as $attribute)
{
    $collection->addAttributeToSelect($attribute->getAttributeCode());
}

$collection->addFieldToFilter('entity_id', array(1, 5, 6));

Where entity_id is id of needed product.

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.