Browsing articles tagged with "attribute Archives - Kalpesh Mehta"
Nov 6, 2014
kalpesh

Magento: Get all attribute values (colors, sizes, etc..)

Magento get all the attribute values you have in the store. Get all the colors and sizes values in Magento. Change the attribute name to whatever you want values for, in the below script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
require_once('app/Mage.php');
umask(0);
Mage::app('admin');
//set_time_limit(0);
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color');
$colors = array();
foreach ($attribute->getSource()->getAllOptions(true) as $option) {
        $colors[] = $option['label'];
}
sort($colors);
foreach($colors as $lbl) {
    echo "<span style='background-color:lightgrey'>".$lbl . "</span>";
        echo "<br>";
}

Replace color with any attribute you want to get values for. Above script also sorts the result set in alphabetic order to make it easy to view all values. If you don’t want to sort the values, just comment out the line which sorts it.

Sep 4, 2014
kalpesh

Magento add/remove product attribute programatically

Magento add product attribute using sql setup file in your module. Also assign your custom attribute to attribute set Default and group General programatically.

Below code will add your new attribute in Manage Products edit screen at the end of General tab with dropdown values Yes/No. It will not display in frontend website but you can change visibible on front to 1 if you wish. Note that it will assign to Default attribute set only but you can change it to whatever as per your requirement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$model = Mage::getResourceModel('catalog/setup','catalog_setup');

$data=array(
'type'=>'int',
'input'=>'boolean', //for Yes/No dropdown
'sort_order'=>50,
'label'=>'CUSTOM ATTRIBUTE CODE LABEL',
'global'=>Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'required'=>'0',
'comparable'=>'0',
'searchable'=>'0',
'is_configurable'=>'1',
'user_defined'=>'1',
'visible_on_front' => 0, //want to show on frontend?
'visible_in_advanced_search' => 0,
'is_html_allowed_on_front' => 0,
'required'=> 0,
'unique'=>false,
'apply_to' => 'configurable', //simple,configurable,bundled,grouped,virtual,downloadable
'is_configurable' => false
);

$model->addAttribute('catalog_product','CUSTOM_ATTRIBUTE_CODE',$data);

$model->addAttributeToSet(
    'catalog_product', 'Default', 'General', 'CUSTOM_ATTRIBUTE_CODE'
); //Default = attribute set, General = attribute group

To remove the product attribute using the setup file, use below code instead:

1
2
$model = Mage::getResourceModel('catalog/setup','catalog_setup');
$model->removeAttribute('catalog_product','CUSTOM_ATTRIBUTE_CODE');
Aug 13, 2013
kalpesh

Magento convert attribute type from TEXT to DROPDOWN

Magento convert attribute type from TEXT (varchar) to DROPDOWN / SELECT (int) in Backend.
Magento doesn’t have this in-built so we will have to do it in our own way. We will convert our existing attribute which is in TEXT type, to DROPDOWN (select). Let’s say our attribute code is “vendor”, you will need to replace it with your own attribute code in all the code below.

WARNING: All the coupon codes that uses this attribute for discounts/promotions will GO AWAY! Please note down all the coupon code Conditions and Actions (which uses this attribute) before converting the attribute.

Step 1.) Backup your database.

1
mysqldump -u mysqluser -p mysqldbname > mysqldbname_backup.sql

Step 2.) Export all the values of the attribute you want to convert in a CSV file. For that, create a file vendor.php (your_attribute.php) in your magento root with following code inside it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
require "app/Mage.php";
umask(0);
Mage::app();

$collection = Mage::getModel('catalog/product')
                        ->getCollection()
                        ->addAttributeToSelect('sku')
                        ->addAttributeToSelect('vendor'); //replace vendor with your attribute code

$data = $collection->getData();

foreach ($data as $product) { 
    echo $product['entity_id'] . "," . $product['vendor']."\n";
}

After saving the above code, run it from command line:

1
php vendor.php > vendor.csv

You should now have all the attribute’s data with product ID in the vendor.csv (your_attribute.csv) file.

Step 3.) Now get all the unique values you have in the attribute’s data, which we will be filling them as options of select dropdown.
Run following mysql command to get all the unique values and copy them in a new file:

1
select distinct value from catalog_product_entity_varchar where attribute_id in (select attribute_id from eav_attribute where attribute_code='vendor'); //replace vendor with your own attribute code

After this step, we have all the attribute’s data in CSV file, and all the UNIQUE attribute values.
Continue reading »

Jun 15, 2013
kalpesh

Magento get attribute options

Get attribute options list i.e. label and value, if the attribute type is dropdown.

This post will show you how to get all the options of attribute with dropdown type in Magento. If your attribute has options, below code will give all the attribute options labels and values in an array format.

1
2
3
4
5
6
7
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'shirt_size'); //change shirt_size to your attribute code
if ($attribute->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
    foreach($options as $option) {
        print_r($option);
    }
}

In the above code, first line will initialize the attribute. Then we are checking if the attribute is using source model or not, if using then get all the options of that attribute.

It will output this for attribute shirt_size:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Array
(
    [value] => 100
    [label] => Small
)
Array
(
    [value] => 99
    [label] => Medium
)
Array
(
    [value] => 98
    [label] => Large
)

If you are here to get attribute option’s label from value OR get attribute option’s value from label, just check out this post: http://ka.lpe.sh/2012/09/13/magento-get-product-attribute-select-option-idlabelvalue/

Jun 15, 2013
kalpesh

Magento get attribute value

To get product attribute value in Magento is very common. You will need it everytime when dealing with catalog products in your development. Attributes have different types, which can be any of Text Field, Text Area, Date, Yes/No, Multiple Select, Dropdown, Price, Gallery, Media Image, Fixed Product Tax (as you can see in backend Manage Attributes > New Attribute > Catalog Input Type for Store Owner). To get the value for these different types of attributes there is no one straight line of code, you will need to use different code for getting values from plain attributes, while different code to get values from select and multiselect attributes.

Get attribute value for PLAIN TEXT, TEXTAREA or DATE type attribute:

1
$attribute_value = $product->getShirtSize(); //for shirt_size attribute

Get value from SELECT, MULTISELECT, DROPDOWN or YES/NO attributes:

1
$attribute_value = $product->getAttributeText($attribute_code);

OR

1
$attribute_value = $product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);

Get value from PRICE attributes:

1
$attribute_value = $product->getNew_price(); //for attribute code "new_price"

and in product list page,

1
$attribute_value = $product->getNewPrice();

Get attribute value by attribute code and productID WITHOUT loading whole product

1
$attribute_value = Mage::getResourceModel('catalog/product')->getAttributeRawValue($product_id, $attribute_code, $store_id);
Pages:123»

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications