Magento Coding Solutions

Uploade file ,image etc.. in magento custom module




Managedesign is a custom module
1) add browser in module
/app/code/local/Managedesign/Managedesign/Block/Adminhtml/Managedesign/Edit/Tab/form.php

$fieldset->addField('uploade_file', 'image', array(
                        'label' => Mage::helper('managedesign')->__('Image'),
                        'name' => 'uploade_file',
                        'note' => '(*.dst)',
                        ));   
                       
                       
2) add a field "uploade_file" in module table(Managedesign module Table).
3)Uplode file code in module controler(Managedesign controllers)
    /app/code/local/Managedesign/Managedesign/controllers/Adminhtml/ManagedesignController.php
   
    add this code in "public function saveAction()"
   
   
    if ($post_data) {

               
                    try {
                   
                   
                        /*Uploade Dth File */
                       
                            print_r($post_data);
                           
                            try{

                                if((bool)$post_data['uploade_file']['delete']==1) {

                                            $post_data['uploade_file']='';

                                }else {

                                        unset($post_data['uploade_file']);

                                        if (isset($_FILES)){

                                            if ($_FILES['uploade_file']['name']) {

                                                if($this->getRequest()->getParam("id")){
                                                    $model = Mage::getModel("managedesign/managedesign")->load($this->getRequest()->getParam("id"));
                                                    if($model->getData('uploade_file')){
                                                            $io = new Varien_Io_File();
                                                            $io->rm(Mage::getBaseDir('media').DS.implode(DS,explode('/',$model->getData('uploade_file'))));   
                                                    }
                                                }
                                                            $path = Mage::getBaseDir('media') . DS . 'managedesign' . DS .'managedesign'.DS;
                                                            $uploader = new Varien_File_Uploader('uploade_file');
                                                            $uploader->setAllowedExtensions(array('dst'));
                                                            $uploader->setAllowRenameFiles(false);
                                                            $uploader->setFilesDispersion(false);
                                                           
                                                            $extension = strtolower(pathinfo($_FILES['uploade_file']['name'], PATHINFO_EXTENSION));
                                                            $file_name = $time.".".$extension;
                                                            $_FILES['uploade_file']['name'] = $file_name;
                                                            $destFile = $path.$_FILES['uploade_file']['name'];
                                                            $filename = $uploader->getNewFileName($destFile);
                                                            $uploader->save($path, $filename);                                   
                                                            $post_data['uploade_file']='managedesign/managedesign/'.$filename;
                                                            $filepath = $destFile;   
                                                            list($width, $height, $type, $attr) = getimagesize($filepath);   
                                                            $post_data['height'] = $height;
                                                            $post_data['width']    = $width;
                                                            $newfile = Mage::getBaseDir() . DS .'dst-files'. DS .$_FILES['uploade_file']['name'];    
                                                            copy($destFile, $newfile);
                                                           
                                                       
                                                           
                                            }
                                        }
                                    }

                            } catch (Exception $e) {
                                    Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                                    $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                                    return;
                            }
                        //save image
                       
                       
                        /* End Code */
    4) create source and destination folder and give the read/write permission
   
   
   

Magento How to get attribute name and value?

Magento How to get attribute name and value?


Get Attribute Id from Option Id

$_attributeId = Mage::getModel(‘eav/entity_attribute_option’)->load($optionId)->getAttributeId();
$_attribute = Mage::getModel(‘eav/entity_attribute’)->load($_attributeId);
echo $attribute->getFrontendLabel();


—————————————————

Get attribute’s name, value, type, and other parameters

/*** get attribute collection */

$attribute = $_product->getResource()->getAttribute(‘my_attribute’);

/*** get attribute type */

$attribute->getAttributeType();

/*** get attribute Label */

$attribute->getFrontendLabel();

/*** get attribute default value */

$attribute->getDefaultValue();

/*** check if the attribute is visible */

$attribute->getIsVisible();

/*** check if the attribute is required */

$attribute->getIsRequired();

/*** get attribute value*/

$attributeValue = Mage::getModel(‘catalog/product’)->load($_product->getId())->getMyAttribute();

Get value from a select box attribute

The attribute code is supposed to be ‘my_attribute‘

$attributeValue = Mage::getModel(‘catalog/product’)
->load($_product->getId())
->getAttributeText(‘my_attribute’);

Load any particular attribute by attribute code

$attributeInfo = Mage::getResourceModel(‘eav/entity_attribute_collection’)
->setCodeFilter(YOUR_ATTRIBUTE_CODE)
->getFirstItem();

// echo “<pre>”; print_r($attributeInfo->getData()); echo “</pre>”;

Get all option value list for the particular attribute

You can see above that I got attribute information by attribute code. My attribute information is stored as $attributeInfo. See code above.

Here is the code to get all option values for my attribute $attributeInfo.

$attributeOptions = $attributeInfo->getSource()->getAllOptions(false);

// echo “<pre>”; print_r($attributeOptions); echo “</pre>”;

Get all options of any attribute

Getting all options of attribute with attribute-code “color“.

$attribute = Mage::getSingleton(‘eav/config’)->getAttribute(‘catalog_product’, ‘color’);
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}

Get attribute’s option information by option id

I have my attribute as $attributeInfo.
I have my attribute’s option value array as $attributeOptions.
See code above.
Suppose, I want to get detail information of any option listed in strong>$attributeOptions array. Here is the code to do so:-

$attributeId = $attributeInfo->getAttributeId();
$optionId = YOUR_ATTRIBUTE_OPTION_ID;
$attributeOptionSingle = Mage::getResourceModel(‘eav/entity_attribute_option_collection’)
->setPositionOrder(‘asc’)
->setAttributeFilter($attributeId)
->setIdFilter($optionId)
->setStoreFilter()
->load()
->getFirstItem();

// echo “<pre>”; print_r($attributeOptionSingle); echo “</pre>”;

Get attribute of particular entity type

Here, I am going to get information about ‘order_id‘ attribute of ‘invoice‘ entity type.

$entityType = Mage::getModel(‘eav/config’)->getEntityType(‘invoice’);
$entityTypeId = $entityType->getEntityTypeId();
$attribute = Mage::getResourceModel(‘eav/entity_attribute_collection’)
->setCodeFilter(‘order_id’)
->setEntityTypeFilter($entityTypeId)
->getFirstItem();

Get attribute options of Configurable product

$confAttributes = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);