iFUEL INTERACTIVE

A NY Interactive Agency and a Division of Agency212

See you on Flickr See you on Twitter See you on FaceBook Subscribe
Creating a new Product Attribute and then using it on a page in Magento
September 29th, 2009  |  1 COMMENT  |  Development, Magento
Tags: ,

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:


<?php

$productModel = Mage::getModel('catalog/product')->load($productid);

$ingredients = $productModel->getIngredients();

echo $ingredients;

?>

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

COMMENTS

  • 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();

LEAVE COMMENT
Your name:  (required)

Your email:  (required, will not be published)

Website:  (not required)

Message:  (required)

Categories
Archives
Tags
Meta
Copyright iFuel Interactive © 2010. All Rights Reserved.
Going up? Click here.A NY INTERACTIVE AGENCY