By 3 years ago in Development

The Magento Strikes Back

After walking you through adding CMS pages in my first post – Magento: First Class – I thought I’d walk you through importing attributes into Magento.

Recently, we’ve been working on a few Magento sites where the client already has an existing e-commerce site. Rather than making the client spend time setting up all the attributes that are required for the products, we can build an import script to easily import the attributes from the existing site.

The input script makes use of the dataflow model by creating new EAV attributes for Magento.

As per the previous script I shared with you, the example below uses a CSV file as its input.

The format of this CSV should be the following:

attribute name, type of attribute

The type of attribute can be one of the following: text, textarea, date, boolean, multiselect, select, price, media_image, weee (fixed product tax) depending on how you want the attribute to behave in the Magento backend.

The main section of code is this:

 * Function to create attributes
 * @param string $code - seo safe URL string
 * @param string $label - nice label for the backend
 * @param string $attribute_type - textbox/select etc
 * @param array $product_type - what product types should this attribute be available to? simple/grouped etc
 * @return none
private function createAttribute($code, $label, $attribute_type, $product_type)
	// attribute data
	$_attribute_data = array(
		'attribute_code' => $code,
		'is_global' => '1',
		'frontend_input' => $attribute_type,
		'default_value_text' => '',
		'default_value_yesno' => '0',
		'default_value_date' => '',
		'default_value_textarea' => '',
		'is_unique' => '0',
		'is_required' => '0',
		'apply_to' => $product_type,
		'is_configurable' => '0',
		'is_searchable' => '0',
		'is_visible_in_advanced_search' => '0',
		'is_comparable' => '0',
		'is_used_for_price_rules' => '0',
		'is_wysiwyg_enabled' => '0',
		'is_html_allowed_on_front' => '1',
		'is_visible_on_front' => '0',
		'used_in_product_listing' => '0',
		'used_for_sort_by' => '0',
		'is_filterable_in_search' => '0',
		'is_filterable' => '0',
		'frontend_label' => array($label)
	// grab the Magento model for attributes
	$model = Mage::getModel('catalog/resource_eav_attribute');
	if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
		$_attribute_data['backend_type'] = $model->getBackendTypeByInput($_attribute_data['frontend_input']);
	// add data to the attribute from the array above
	// set type
	// set user defined
	try {
		$model->save(); echo '<p>Saved: ' . $label . '</p>';
	catch (Exception $e) {
		echo '<p>Sorry, error occured while trying to save the attribute "' . $label . '" Error: '.$e->getMessage().'</p>';

Let’s break this down a little.

The $_attribute_data array contains all the information required for the product attribute, and we just throw this at the resource_eav_attribute Mage model.

Any number of these array items could be added to the CSV to create a more rounded import, however, depending on the product, these default values should be acceptable.

By Douglas Radburn. at 1:17PM on Tuesday, 16 Aug 2011

Doug is our Senior Open Source Web Developer. Having gained some informative insight and technical experience at two major digital agencies after graduating; Doug brought his knowledge and skills to Branded3 in 2009, and has been solving our development dilemmas ever since. Follow Douglas Radburn on Twitter.


Comments are closed.