Sunday , 21 September 2014
Lesson 21: Magento Custom Attributes (Part 2)

Lesson 21: Magento Custom Attributes (Part 2)

Hi guys,

Welcome back to Magento tutorial 21 with “Magento Custom Attributes”. In the previous part of this lesson, we examined how to add a custom attribute to a product. Now, let’s see an EAV attribute in the product collection and instructions on how to get a product attribute value.

2. EAV attribute in the product collection

By default, a new attribute added to a product won’t be loaded in collection. To load it, use function addAttributeToSelect:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('is_featured');

Config to load EAV attribute in product collection outside frontend.

In file config.xml of Catalog module of Core Magento, add attribute code:

Code example:  app\code\local\Magestore\Lesson21\etc\config.xml

[xml]<frontend>

<product>
<collection>
<attributes>
<gift_amount/>
</attributes>
</collection>
</product>
</frontend>[/xml]

3. How to get a product attribute value

3.1. Attribute has already loaded in product collection

Use function getData:

[php]$collection = $observer->getCollection();
foreach($collection as $product){
$isFeatured = $product->getIsFeatured();
// or $isFeatured = $product->getData(‘is_featured’);
}[/php]

3.2. Attribute has not loaded in product collection yet

We get attribute value from EAV storage tables.

For example: In product list page, discount products whose attribute “is featured” is “yes”.

Step 1: Declare event

catalog_product_collection_load_after  trong file config.xml

Code example: app\code\local\Magestore\Lesson21\etc\config.xml

[xml]<events>
<catalog_product_collection_load_after>
<observers>
<lesson21_observers>
<class>lesson21/observer</class>
<method>productCollectionLoadAfter</method>
</lesson21_observers>
</observers>
</catalog_product_collection_load_after>
</events>[/xml]

Step 2: Write function productCollectionLoadAfter in file model observer

Code example: app\code\local\Magestore\Lesson21\Model\Observer.php

[php]public function productCollectionLoadAfter($observer) {
$storeId = Mage::app()->getStore()->getId();
$collection = $observer->getCollection();
foreach ($collection as $product) {
$isFeatured = Mage::getResourceModel(‘catalog/product’)
->getAttributeRawValue($product->getId(), ‘is_featured’, $storeId);
if($isFeatured)
$product->setFinalPrice($product->getFinalPrice()/2);
}
}[/php]

getAttributeRawValue gets value of attribute is_featured without loading product.

+ $product->getId(): product id

+ is_featured: attribute code of attribute needed to get value

+ $storeId: id of store showing product

Result shown as belows:

product attribute value

So, we have just finished another lesson in series Magento Open Course. After learning this tutorial, you should know:

  1. How to add a custom attribute to a product
  2. EAV attribute in the product collection
  3. How to get a product attribute value

Let’s practice and leave us a comment if you have any difficulties.

 

About Blanka

Blanka
This is our talented developer who has been developing many outstanding extensions. If you meet Blanka in person, you won't never forget his special laugh, very cheerful and relaxing.

One comment