magento – custom attribute type, file upload

Not sure where to start in this and what the best solution would be. I need to have a product attribute that supports file upload, fir the purpose of an image.

I am not looking for code, simply some advice on the best way to achieve this

4 thoughts on “magento – custom attribute type, file upload”

  1. I think you will want to create your own “tab” in the Manage->Products page in the admin app and then inside of that you will want to use a technique such as referenced here for the uploader:

    http://www.magentocommerce.com/boards/viewthread/11667/P0/

    The actual process of storing the link as an attribute on the product could be either a custom attribute you define on the product and then egt/set on this new admin tab or you could look to extend the existing product model by creating your own model class and managing the save of the new model by hooking up to the product save events.

    For adding a custom tab to the Manage->Product page I started out with this example:

    http://fishpig.co.uk/custom-tabs-magento-product-admin/

  2. if you want to add file upload for customer/product. just create related attribute for that and in admin you will find the option and work properly and for frontend just create the file input. upload it via magento file uploader in any directory and just save the file path in attribute. My code for customer file attribute for reference is as under:

    <label for="certificate"><?php echo $this->__('Re-Sale Certificate') ?></label>
                        <div class="input-box">
                            <input type="file" name="designer_certificate" title="<?php echo $this->__('certificate') ?>" id="designer_certificate" />
                        </div>  
    

    installer for file attribute

    $installer = $this;
    $installer->startSetup();
    
    //$installer->getConnection()->addColumn($installer->getTable('customer/entity'), 'certificate', 'varchar(100)');
    $installer->removeAttribute('customer', 'designer_certificate');
    $installer->addAttribute("customer", "designer_certificate",  array(
        "type"     => "varchar",
        "backend"  => "",
        "label"    => "Designer Certificate",
        "input"    => "file",
        "source"   => "",
        "visible"  => true,
        "required" => false,
        "default" => "",
        "frontend" => "",
        "unique"     => false,
        "note"       => ""
        ));
    
            $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "designer_certificate");
    
    
    $used_in_forms=array();
    
    $used_in_forms[]="adminhtml_customer";
    $used_in_forms[]="checkout_register";
    $used_in_forms[]="customer_account_create";
    $used_in_forms[]="customer_account_edit";
    $used_in_forms[]="adminhtml_checkout";
            $attribute->setData("used_in_forms", $used_in_forms)
            ->setData("is_used_for_customer_segment", true)
            ->setData("is_system", 0)
            ->setData("is_user_defined", 1)
            ->setData("is_visible", 1)
            ->setData("sort_order", 100)
            ;
            $attribute->save();
    
    $installer->endSetup();
    

    config:

    <global>
    ....
    <resources>
          <designercertificate_setup>
            <setup>
              <module>Renegade_Account</module>
              <class>Mage_Customer_Model_Entity_Setup</class>
            </setup>
            <connection>
              <use>core_setup</use>
            </connection>
          </designercertificate_setup>
          <designercertificate_write>
            <connection>
              <use>core_write</use>
            </connection>
          </designercertificate_write>
          <designercertificate_read>
            <connection>
              <use>core_read</use>
            </connection>
          </designercertificate_read>
        </resources>
    ....
        </global>
    

    Now upload file and just save like

        .....
            $path = Mage::getBaseDir('media') . DS .'customer'. DS . 'designer-certificates' . DS;
                    if (!is_dir($path)) {
                        mkdir($path, 0777, true);
                    }
                    $filename = str_replace(' ', '_', trim($_FILES['designer_certificate']['name']));
    Add a comment to this line
                    $uploader->save($path, $filename);
                    $file = "/designer-certificates/" . $filename;
                    $customer->setDesignerCertificate($file);
        .....
    

    file should be save in media,customer folder for customer file attribute if you want to use builtin functionality.

  3. Read this blog.

    From the above blog:

    Whan of the most enticipated and needed things in Magento is File upload Custom Option. As discussed last year at Magento Forum, it is not completed nor tested.

    Now, Magento already have frontend and admin part of file upload option implemented in themes. Since backend part is still missing, understand that this still doesn’t work, however, if you’re interested how it looks, read on ..

    Frontend html is found in:

    app/design/frontend/default/default/template/catalog/product/view/options/type/file.phtml

    Frontend javascript code in:

    app/design/frontend/default/default/template/catalog/product/view/options.phtml
    (optionFileUpload js object = asynchronous iframe upload)

    You can enable file upload custom option if you add:

    <file translate="label" module="adminhtml">
        <label>File</label>
        <render>adminhtml/catalog_product_edit_tab_options_type_file</render>
        <types>
            <file translate="label" module="adminhtml">
                <label>File Upload</label>
            </file>
        </types>
    </file>
    

    inside node:

    config->global->catalog->product->options->custom->groups

    in:

    app/code/core/Mage/Catalog/etc/config.xml

    Now you have new File Upload option that you can add to product!

    We are coding module of similar functionality for one of our clients as we speak, but we have our fingers crossed to see this option in next Magento version!

Leave a Reply

Your email address will not be published. Required fields are marked *