Adding an Extra Field for PDF Upload in Magento Admin Panel
As a Magento developer, I often get requests from clients who want to add custom fields to their admin panel, especially for uploading files like PDFs. In this guide, I’ll show you how to create an additional field for PDF upload on the “Add Product” page of your Magento 1.7 store.
Understanding the Issue
By default, Magento does not provide a built-in field for uploading documents in the admin panel. To achieve this, we need to extend the existing catalog form and add our custom attribute there.
Step-by-Step Solution
1. Create a Custom Attribute
First, let’s create a new attribute that will store the path of the uploaded PDF file:
- Navigate to Catalog > Attributes > Manage Attributes.
- Click on “Add New Attribute.”
- Set the Attribute Code as
pdf_document. - Select “File” as the Data Type and save the attribute.
2. Create an EAV Configuration File
Create a new XML configuration file for your custom attribute:
app/design/adminhtml/default/default/layout/pdf_config.xml
Add the following content to this file:
<layout version="1.0">
<adminhtml_catalog_product_edit>
<reference name="product_tabs">
<action method="addTab",translate="label">
<tab_id>pdf_document</tab_id>
<class>ajax</class>
<block>adminhtml/catalog_product_helper_tab_pdf</block>
<label translate="true">PDF Document</label>
</action>
</reference>
</adminhtml_catalog_product_edit>
</layout>
This code adds a new tab named “PDF Document” to the product editing page.
3. Create the Custom Tab Block
Create the block file for your PDF upload:
app/code/local/Your_Company/Your_Module/Block/Adminhtml/Catalog/Product/Helper/Tab/Pdf.php
Add this content to the Pdf.php file:
class Your_Company_Your_Module_Block_Adminhtml_Catalog_Product_Helper_Tab_Pdf extends Mage_Adminhtml_Block_Widget implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
public function getTabLabel()
{
return $this->__('PDF Document');
}
public function getTabTitle()
{
return $this->__('PDF Document');
}
public function canShowTab()
{
return true;
}
public function isHidden()
{
return false;
}
}
4. Create the Form and Upload Field
Create the form for your PDF upload:
app/code/local/Your_Company/Your_Module/Block/Adminhtml/Catalog/Product/Helper/Tab/Pdf/Form.php
Add this content to the Form.php file:
class Your_Company_Your_Module_Block_Adminhtml_Catalog_Product_Helper_Tab_Pdf_Form extends Mage_Adminhtml_Block_Widget_Form implements Mage_Adminhtml_Block_Widget_TablerenderInterface
{
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$form->setHtmlIdPrefix('pdf_document');
$form->setFieldNameSuffix('product[pdf_document]');
$fieldset = $form->addFieldset('pdf_document_fieldset', array(
'legend' => $this->__('PDF Document')
));
$fieldset->addField('pdf_document', 'file', array(
'name' => 'pdf_document',
'label' => $this->__('PDF Document'),
'required' => false,
'note' => '<small '.$this->escapeHtml($this->__('Uploaded PDF will be attached to this product.')).'</small>'
));
return parent::_prepareForm();
}
}
5. Add the Product Save Action
Finally, you need to save the uploaded file when a product is saved:
app/code/local/Your_Company/Your_Module/Model/Mysql4/Catalog/Product.php
Add this code inside the save method of your Product model:
if (isset($data['pdf_document']) && $data['pdf_document'] != '') {
if ($this->getId()) {
$mediaApi = Mage::getModel('catalog/product_media_api');
$mediaApi->delete($productId, true);
}
$result = $this->_saveProductMediaFile($productId, 'pdf_document', $data['pdf_document']);
}
Conclusion
By following these steps, you can successfully add an extra field for PDF upload in the Magento admin panel. This customization allows your clients to easily attach relevant documents to their products, enhancing the information available to customers.
Remember, as a developer, it’s crucial to maintain clean and organized code structures. Always comment your code for better readability, and follow Magento’s coding standards.
For more advanced customizations in Magento, I recommend exploring our blog at [https://laravelcompany.com/magento-articles] where we cover topics like theme development, performance optimization, and extension creation.