Creating a new Product Attribute and then using it on a page in Magento

Creating a new product attribute and using it when you’re displaying a product turns out to be fairly simple – but I know for us at iFuel – and I think for lots of others based on the posts you see around the net, it ends up not being as simple as it seems.

For this example, we’ll create a new attribute called “ingredients”.  We need this to be a text field (text area to be exact) that Magento admins can enter the product ingredients.  Then we’ll need to be able to access this text when we display the product.

Attributes

Attributes are single pieces of information that can be assigned to any of the entities (models) in the Magento system.  There is an extensive user interface for creating and maintaining attributes and attribute sets for products.  But you can actually create attributes for other entity types, like categories, too.  (Creating an attribute for a category has to be done right in the database – I’ll try to cover that in a future post).

To create an attribute:

1. Select “Attributes” -> “Manage Attributes” from the “Catalog” menu in the Magento admin panel.

2. Click “Add New Attribute”.

Adding an Attribute
Adding an Attribute

3. Enter an attribute code – we’ve used “ingredients” here.

4. Select the scope – we’ve selected “global” but you could select store as well.

5. Select the attribute type – we’ve selected text area because we want the user to be able to enter free-form text (we’re using the wysiwig editor for Magento so they’re actually entering html).

6. Go to the “Manage Label/Options” tab and enter a label to be used for the new attribute.

7. Save the new attribute.

Attribute Sets

Once you’ve added the new attribute, you have to add it to the attribute set that you use for your products.  (If you have more than one attribute set for your products, you’ll have to add it to all of the sets you want it to appear in).

1. Select “Attributes” -> “Manage Attribute Sets”

2. Select your attribute set (if you haven’t created new attribute sets, it’ll be called “Default”)

3. Drag your new attribute from the list of “unassigned attributes” on the right into the list on the left (where you drop it will be where the admin will see it)

Adding and attribute to an attribute set in Magento
Adding and attribute to an attribute set in Magento

4. Save the attribute set.

Using the new attribute in the page

So, that’s the easy part.  Actually, using the attribute in the page is pretty easy, once you’ve got the right syntax!  Assuming the id of the product you are using is in $productid then:

[sourcecode language=’php’]

load($productid);

$ingredients = $productModel->getIngredients();

echo $ingredients;

?>

[/sourcecode]

Poof!

I kept trying $_product->getIngredients(), but that wasn’t working.  I think there’s a way to tell Magento to load that attribute when the product is being loaded.  If anyone knows, let me know!

=============================
“e-commerce done right

http://www.ifuelinteractive.com

3 Responses to “Creating a new Product Attribute and then using it on a page in Magento”

  1. You have to do this to get the attribute at a later time.

    $products= Mage::getModel(‘catalog/product’)->getCollection()
    ->addAttributeToSelect(‘ingredients’)
    ;
    $product = $products->getItemById($id);
    $ingredients = $product->getIngredients();

  2. i have added multi select attribute from admin as you mention and i am using source model for attribute value.

    Now we are able to import attribute value but at the time of export attribute value display blank in csv.
    please reply its urgent.
    Thank u

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>