Tag Archives: development

Programatically create attribute in Magento, useful for the “on the fly” import system

Programatically create attribute in Magento, useful for the “on the fly” import system

Sometimes when doing an import in Magento from old shop system you are faced with lot of issues related to attributes. For instance your old shop system might have several product attributes that simply do not fit into the default Magento attributes. In such cases you need to create these attributes manually in the Magento admin, assign them to the appropriate attribute set and then construct a valid CSV file with valid column names for these new attributes.

When you are dealing with large shop systems, with several thousands of products, then actions like these need to be handled programatically. Below is a just a fraction of the code from the full import script I have been working on last few days.

private function createAttribute($code, $label, $attribute_type, $product_type)
{
	$_attribute_data = array(
		'attribute_code' => 'old_site_attribute_'.(($product_type) ? $product_type : 'joint').'_'.$code,
		'is_global' => '1',
		'frontend_input' => $attribute_type, //'boolean',
		'default_value_text' => '',
		'default_value_yesno' => '0',
		'default_value_date' => '',
		'default_value_textarea' => '',
		'is_unique' => '0',
		'is_required' => '0',
		'apply_to' => array($product_type), //array('grouped')
		'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',
		'frontend_label' => array('Old Site Attribute '.(($product_type) ? $product_type : 'joint').' '.$label)
	);

	$model = Mage::getModel('catalog/resource_eav_attribute');

	if (!isset($_attribute_data['is_configurable'])) {
		$_attribute_data['is_configurable'] = 0;
	}
	if (!isset($_attribute_data['is_filterable'])) {
		$_attribute_data['is_filterable'] = 0;
	}
	if (!isset($_attribute_data['is_filterable_in_search'])) {
		$_attribute_data['is_filterable_in_search'] = 0;
	}

	if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
		$_attribute_data['backend_type'] = $model->getBackendTypeByInput($_attribute_data['frontend_input']);
	}

	$defaultValueField = $model->getDefaultValueByInput($_attribute_data['frontend_input']);
	if ($defaultValueField) {
		$_attribute_data['default_value'] = $this->getRequest()->getParam($defaultValueField);
	}

	$model->addData($_attribute_data);

	$model->setEntityTypeId(Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId());
	$model->setIsUserDefined(1);

	try {
		$model->save();
	} catch (Exception $e) { echo '<p>Sorry, error occured while trying to save the attribute. Error: '.$e->getMessage().'</p>'; }
}

The import I was running had to deal with grouped and simple products (that go on that grouped products). Besides special attributes that only grouped products had, and some special attributes that only simple products have, system also had some “joint” attributes that both of the product types had. Examples below show how I called the above method to generate such specific or “joint” attributes.

//My "grouped product only" attribute
$this->createAttribute(strtolower("ShirtType"), "ShirtType", "select", "grouped");

//My "simple product only" attribute
$this->createAttribute(strtolower("Swatch"), "Swatch", "text", "simple");

//My "joint" attribute, the one that I had both on simple and on grouped products
$this->createAttribute(strtolower("InventoryMsg"), "InventoryMsg", "text", "");
$this->createAttribute(strtolower("Complete"), "Complete", "boolean", "");

Hope that above example help someone. Cheers.

3

How to limit the number of allowed images per single product in Magento

How to limit the number of allowed images per single product in Magento

Recently I have been playing around with Magento with the concept of limiting the number of images that admin can upload per single product. There are several reasons why you might need this kind of functionality. I will not go into them. Solution for this request is pretty straightforward and easy. It can be done in just a few lines of code. Read more

0

How to activate/deactivate Magento module per a website level

How to activate/deactivate Magento module per a website level

First off let me just clearly say that I am not talking about just activating/deactivating a module output per a website level. Module output merely disables toHtml() method that certain module block class implements. In this article I am talking about fully deactivating the module like when we open the modules config xml file and place <active>true</active>. Read more

4

Magento module with model that reads and writes in separate database

Magento module with model that reads and writes in separate database

As a part of a greater concept I have been checking out Magento module possibilities in terms of having a module whose model reads and writes from separate database. For instance, if your Magento shop is installed on server called Server1.com in database called shop1, you can have Magento module in your system installed and running with module Resource set to connect to Server2.com and some db1 database. Read more

1

Git ignore (.gitignore) file for Magento project

Git ignore (.gitignore) file for Magento project

Yesterday I was determined to write a detailed walk-trough on setting up the SVN for Magento development. After some minor frustrations I decided to let go of the SVN and make my self learn Git. Read more

1

CoolDash – Blank Magento extension for building admin system configuration area

Featured Image

In order to speed things up with building admin sections under System > Configuration area in Magento I wrote a little blank extension. Hopefully its a step or two beyond “Hello world” level. I named the extension “CoolDash”, short from CoolDashboard. Name holds no special meaning, just something I popped out of my head. Read more

17

What’s up with web developers and coffee?

Featured Image

If you are a web developer or have had an honor to work with one of these creatures, you probably noticed their strange addiction to coffee. Developers surrounding me start their day by drinking the morning coffee before the working hours start, then, at the beginning of their working day they make one in the office. They drink numerous cups of coffee during the working day, and when they go out for a break they order… guess what… coffee. Read more

15

CSSEdit 2.6 review

Featured Image

While coding CSS on a PC based text editor can be a normal thing, doing it on a Mac using CSSEdit is an extraordinary experience, really! This article is based on how this little/big app made in Belgium by Macrabbit made me not give up coding and started getting things done.

Read more

3

Improve your development time

Featured Image

If you are developer working on a open source technologies then most likely you are using open source or some freeware tools. Since I’m a PHP developer this post will show examples in context of PHP. Developing for PHP can be quite a fun, or sometimes quite painful. Read more

3

Moving Magento site from development to live server

Moving Magento site from development to live server

This article has been closed for comments on March 17th, 2009. Approach below has been shown to work. If you experience unexpected results following this approach, then please revise the search/replace string you provided while going trough .sql file. Keep in mind real root url path to dev/live Magento installation.

Maybe this post should have been named Magento MySQL table export, import, and 1452 error. Here’s why. For the last hour and a half I’ve been trying to transfer Magento database from one server to another. I’ve been getting MySQL 1452 error during the import all the time. My export gone pretty smoothly, no errors at all. I’ve exported using default settings of my WAMP servers phpMyAdmin application. Moving a database should be consisting of a two simple step. Step 1: Export your database to a file. Step 2: Import your database from a file to new MySQL server. Sounds easy, right. Well it is. At least it should be. Read more

4