0

enter image description here

I am beginner in the Magento development and I would like to know what are the attributes of products available for use. Example: getName, getPrice, getSpecialPrice etc.

1 Answer 1

1

Product attributes

The attributes available to use are somewhat static, and somewhat dynamic.

Magento ships with a selection of default product attribute, for example "name", "status", etc.

If you inspect the catalog_product_entity table you will see the default "fields". They're attributes, but Magento calls them "fields" as they're not modifiable. These include SKU, type ID (simple, configurable, etc), etc.

To view the available product attributes from the administration panel, head to Catalog -> Attributes -> Manage Attributes. This page will show all system and user defined attributes (you can filter the results by the "System" filter).

You'll see all the available product attributes here. The attribute_code is the value you use to retrieve that product's attribute value from the model. It can be done in camel cased format, i.e. ->getMyAttributeName() (implemented as a magic method by Varien_Object), or using ->getData('my_attribute_name').


Getting product attribute values

If you take "name" as an example, the difference between the two applications above is that Magento core classes, community and local extensions of Mage_Catalog_Model_Product can override the behaviour of $product->getName(), while it's harder to override specific behaviour for $product->getData('name').

For example, if I want to prefix all product names with "foobar", I could add the following to a local product model extension (XML configuration and module structure not shown here for brevity):

# File: app/code/local/MyCompany/Catalog/Model/Product.php
class MyCompany_Catalog_Model_Product extends Mage_Core_Model_Product
{
    /**
     * Prefix all product names with "foobar"! Parent could return null as well.
     * @return string|null
     */
    public function getName()
    {
        $defaultName = parent::getName();
        if (!$defaultName) {
            return $defaultName;
        }
        return sprintf('foobar %s', $defaultName);
    }
}

While at the same time if you wanted to override the getData() equivalent, you'd have to do something like this:

public function getData($attribute)
{
    if ('name' === $attribute) {
        // as above
    }
    return parent::getData($attribute);
}

Getting all product attributes

If you want to get a list of all product attributes programmatically, you can use the EAV attribute collection and filter it by the product's entity_type_id:

$entityType = Mage::getModel('eav/config')->getEntityType(Mage_Catalog_Model_Product::ENTITY);

/** @var Mage_Eav_Model_Resource_Entity_Attribute_Collection $attributes */
$attributes = Mage::getModel('eav/entity_attribute')->getCollection()
    ->setEntityTypeFilter($entityType);

foreach ($attributes as $attribute) { /** @var Mage_Eav_Model_Entity_Attribute $attribute */
    var_dump($attribute->getAttributeCode());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.