Posted by: james | September 19, 2009

product_spec

First of all 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 Block folder and create element.php and write in that this code.

app/code/local/Form/ProductSpec/Block/element.php
<?php
// use here extend class as you have added attribute i have added attribute file so i use..
class form_ProductSpec_Block_Product_Element extends Varien_Data_Form_Element_File
{
public function getElementHtml()
{
$html = parent::getElementHtml();

$value = $this->getValue();

if($value == ‘checked’)
{
$val = ”;
}
else
{
$val = $value;
}

$collection = Mage::getModel(‘catalog/product’)->getCollection();
$collection->addAttributeToSelect(‘product_spec’);

foreach($collection as $product)
{
$res = $product->getId();
$data = Mage::getModel(‘catalog/product’)->load($res);

if($value)
{
if($data[‘product_spec’])
{
$htmlId =  $this->getHtmlId();
$html .= “<br>”;
$html .= ‘<input name=”product[product_spec]” id=”product[product_spec]” value=”checked”‘;
$html .= ‘class=”checkbox” type=”checkbox”/>’;

$html .= ‘<label for=”product[product_spec]”>’;
$html .= ‘Delete’;
$html .= ‘</label>’;

$html .= ‘&nbsp;&nbsp;&nbsp;’;

$html .= ‘<span id=”file”>’;
$html .= ‘<b>’. $val . ‘</b>’;
$html .= ‘</span>’;
}
}
else
{

}
}
return $html;
}
}

now to make configuration go to config.xml in etc folder and write this code.

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>

here we have create one custom attribute so for that we have create model

local/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)
{
if(isset($_POST[‘product’][‘product_spec’]))
{
$object->delete();
}
else
{
$attrCode = $this->getAttribute()->getAttributeCode();

$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);
$object->setData($attrCode,$name);
}
else
{
$randvalue = rand(1,10);
$file_new_name = $dst. DS .$randvalue.’_’.$name;
copy($src,$file_new_name);
//rename($dst_new,$file_new_name);
$object->setData($attrCode,$randvalue.’_’.$name);
}
}
}

//$v = $object->getProductSpec(); // to get file name

//         // here to get any attribute value you have to just use camel case letter and if you have underscore then also after
//         //under_score the name will be in upper case.
parent::beforeSave($object);
}
}

local/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’,
‘input_renderer’          => ‘productspec/product_element’,
‘user_defined’            => true,
));

$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: