Posted by: james | September 16, 2009

Check it..

for adding attribute means like form elemnt at admin panel…
additional image for category at admin panel..

go to app/etc/modules/Form_CategoryImage.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<config>
<modules>
<Form_CategoryImage>
<active>true</active>
<codePool>local</codePool>
</Form_CategoryImage>
</modules>
</config>

go to app/code/local/Form/CategoryImage/etc/config.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<config>
<modules>
<Form_CategoryImage>
<version>0.1.0</version>
</Form_CategoryImage>
</modules>
<global>
<models>
<categoryimage>
<class>Form_CategoryImage_Model</class>
</categoryimage>
</models>
<resources>
<categoryimage_setup>
<setup>
<module>Form_CategoryImage</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class> // here use class which file contain

add attribute method means use

setup.php but its on different path for

different module.
</setup>
<connection>
<use>core_setup</use>
</connection>
</categoryimage_setup>
<categoryimage_write>
<connection>
<use>core_write</use>
</connection>
</categoryimage_write>
<categoryimage_read>
<connection>
<use>core_read</use>
</connection>
</categoryimage_read>
</resources>
<blocks>
<categoryimage>
<class>Form_CategoryImage_Block</class>
</categoryimage>
</blocks>

<helpers>
<categoryimage>
<class>Form_CategoryImage_Helper</class>
</categoryimage>
</helpers>
</global>

<frontend>
<routers>
<categoryimage>
<use>standard</use>
<args>
<module>Form_CategoryImage</module>
<frontName>categoryimage</frontName>
</args>
</categoryimage>
</routers>
</frontend>
</config>

go to app/code/local/Form/CategoryImage/sql/categoryimage_setup/mysql4-install-0.1.0.php

<?php
$installer = $this;

$installer->startSetup();

$installer->addAttribute(‘catalog_category’, ‘featured_image’, array(
‘type’              => ‘varchar’,
‘label’             => ‘Featured Image’,
‘visible’           => true,
‘required’          => false,
‘input’             => ‘image’,
‘global’            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
‘class’             => ‘input-file’,
‘visible_on_front’  => true,
‘backend’           => ‘catalog/category_attribute_backend_image’,
));

$installer->endSetup();

now for adding custome attribute with custome backend model in i porting product, while importing product you can

also import any file like .txt,.doc,.pdf or any other…

go to app/etc/modules/Form_ProductSpec.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<config>
<modules>
<Form_ProductSpec>
<active>true</active>
<codePool>local</codePool>
</Form_ProductSpec>
</modules>
</config>

now go to app/code/local/Form/ProductSpec/etc/config.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<config>
<modules>
<Form_ProductSpec>
<version>0.1.0</version>
</Form_ProductSpec>
</modules>
<global>
<models>
<productspec>
<class>Form_ProductSpec_Model</class>
</productspec>
</models>
<resources>
<productspec_setup>
<setup>
<module>Form_ProductSpec</module>
<class>Mage_Eav_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</productspec_setup>
<productspec_write>
<connection>
<use>core_write</use>
</connection>
</productspec_write>
<productspec_read>
<connection>
<use>core_read</use>
</connection>
</productspec_read>
</resources>
<blocks>
<productspec>
<class>Form_ProductSpec_Block</class>
</productspec>
</blocks>

<helpers>
<productspec>
<class>Form_ProductSpec_Helper</class>
</productspec>
</helpers>
</global>

<frontend>
<routers>
<productspec>
<use>standard</use>
<args>
<module>Form_ProductSpec</module>
<frontName>productspec</frontName>
</args>
</productspec>
</routers>
</frontend>
</config>

go to app/code/Form?ProductSpec/Model/Attribute/Backend.php

<?php
class Form_ProductSpec_Model_Attribute_Backend extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{
public function beforeSave($object)
{
$attrCode = $this->getAttribute()->getAttributeCode();
//$value = $object->getData($attrCode);

$s = $_FILES[‘product’][‘tmp_name’];
$n = $_FILES[‘product’][‘name’];

$name = $n[‘product_spec’];
$src = $s[‘product_spec’];

$dst = Mage::getBaseDir(‘media’). DS .’product_spec’;
$dst_new = Mage::getBaseDir(‘media’). DS .’product_spec’. DS .$name;

if(!file_exists($dst))
{
mkdir($dst,0777);
}
else
{
if(!file_exists($dst_new))
{
copy($src,$dst_new);
}
else
{
$randvalue = rand(1,10);
$file_new_name = $dst. DS .$randvalue.’_’.$name;
rename($dst_new,$file_new_name);
}
}

$object->setData($attrCode,$name);

/*$uploader = new Varien_File_Uploader($this->getAttribute()->getName());
$uploader->setAllowedExtensions(array(‘txt’,’doc’,’pdf’));
$uploader->setAllowRenameFiles(true);
$uploader->save($dst_new);*/

/*$object->setData($this->getAttribute()->getName(), $dst_new);
$this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName());*/

parent::beforeSave($object);
//$value = $object->getData($this->getAttribute()->getName());
}
}

app/code/loacl/Form/ProductSpec/sql/productspec_setup/mysql4-install-0.1.0.php

<?php
$installer = $this;

$installer->startSetup();

$installer->addAttribute(‘catalog_product’, ‘product_spec’, array(
‘type’              => ‘varchar’,
‘label’             => ‘Upload File’,
‘visible’           => true,
‘required’          => false,
‘input’             => ‘file’,
‘class’             => ”,
‘visible_on_front’  => true,
‘backend’              => ‘productspec/attribute_backend’,
));

$installer->endSetup();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: