magento 添加产品上传附件(上)

最近有个需求,要在产品详情页tab添加一个可供客户下载文件的模块,后台可以在产品里添加上传附件的功能。后台上传文件的格式要限制,每个产品只可以上传5个文件。


寻思一下,我看看有没有现成的插件可供参考,上官网插件一搜,还真有一款插件比较符合我的要求,叫Upload_pdf。该插件只可以上传pdf文件,但是修改一下就可以达到我的目的了

先看看前端产品下载效果图:

后端产品上传效果图:

不多说,直接撸代码,下面是完整的代码,你可以去官网下载该插件,然后看我代码来修改,也可以直接按照我的步骤来,从零到有完成该功能,此代码适合1.9.x版本,2.0没有测试过

先创建核心code下的文件夹和相关文件

app\code\local\Iverve\Uploadpdf\Block
app\code\local\Iverve\Uploadpdf\Block\Uploadpdf.php
app\code\local\Iverve\Uploadpdf\Block\Adminhtml\Catalog\Product\Edit
app\code\local\Iverve\Uploadpdf\Block\Adminhtml\Catalog\Product\Edit\Form.php
app\code\local\Iverve\Uploadpdf\Block\Adminhtml\Catalog\Product\Edit\Tabs.php
app\code\local\Iverve\Uploadpdf\Block\Adminhtml\Catalog\Product\Edit\Tab
app\code\local\Iverve\Uploadpdf\Block\Adminhtml\Catalog\Product\Edit\Tab\Uploadpdf.php
app\code\local\Iverve\Uploadpdf\controllers
app\code\local\Iverve\Uploadpdf\controllers\IndexController.php
app\code\local\Iverve\Uploadpdf\controllers\Adminhtml
app\code\local\Iverve\Uploadpdf\controllers\Adminhtml\UploadpdfController.php
app\code\local\Iverve\Uploadpdf\etc
app\code\local\Iverve\Uploadpdf\etc\config.xml
app\code\local\Iverve\Uploadpdf\Helper
app\code\local\Iverve\Uploadpdf\Helper\Data.php
app\code\local\Iverve\Uploadpdf\Model
app\code\local\Iverve\Uploadpdf\Model\Extensioncheck.php
app\code\local\Iverve\Uploadpdf\Model\Observer.php
app\code\local\Iverve\Uploadpdf\Model\Uploadpdf.php
app\code\local\Iverve\Uploadpdf\Model\Mysql4
app\code\local\Iverve\Uploadpdf\Model\Mysql4\Extensioncheck.php
app\code\local\Iverve\Uploadpdf\Model\Mysql4\Uploadpdf.php
app\code\local\Iverve\Uploadpdf\Model\Mysql4\Extensioncheck
app\code\local\Iverve\Uploadpdf\Model\Mysql4\Extensioncheck\Collection.php
app\code\local\Iverve\Uploadpdf\Model\Mysql4\Uploadpdf\Collection.php
app\code\local\Iverve\Uploadpdf\sql\uploadpdf_setup
app\code\local\Iverve\Uploadpdf\sql\uploadpdf_setup\mysql4-install-0.1.0.php

创建design下的文件夹和相关文件

app\design\adminhtml\default\default\template\iverve\uploadpdf\catalog\product\tab
app\design\adminhtml\default\default\template\iverve\uploadpdf\catalog\product\tab\uploadpdf.phtml
app\design\frontend\base\default\layout\iverve\uploadpdf
app\design\frontend\base\default\layout\iverve\uploadpdf\uploadpdf.xml
app\design\frontend\base\default\template\iverve\uploadpdf
app\design\frontend\base\default\template\iverve\uploadpdf\uploadpdf.phtml

创建etc下的文件夹和相关文件

app\etc\modules\Iverve_Uploadpdf.xml

先开始写block块,前端block

app\code\local\Iverve\Uploadpdf\Block
app\code\local\Iverve\Uploadpdf\Block\Uploadpdf.php

<?php
/**
* @category    Iverve
* @package     Iverve_Uploadpdf
*/

class Iverve_Uploadpdf_Block_Uploadpdf extends Mage_Core_Block_Template
{
public function getPdfDetails()
{
return Mage::getModel('uploadpdf/uploadpdf')->getCollection()->addFieldToFilter('product_id', Mage::registry('current_product')->getEntityId());
}
}

后端产品from表单提交block

app\code\local\Iverve\Uploadpdf\Block\Adminhtml\Catalog\Product\Edit
app\code\local\Iverve\Uploadpdf\Block\Adminhtml\Catalog\Product\Edit\Form.php

class Iverve_Uploadpdf_Block_Adminhtml_Uploadpdf_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
  protected function _prepareForm()
   {
    $form = new Varien_Data_Form(array('id' => 'edit_form','action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post','enctype' => 'multipart/form-data'));	  
    $form->setUseContainer(true);
    $this->setForm($form);
    return parent::_prepareForm();
   }
}

后端产品tab位置block

app\code\local\Iverve\Uploadpdf\Block\Adminhtml\Catalog\Product\Edit\Tabs.php

require_once 'Mage/Adminhtml/Block/Catalog/Product/Edit/Tabs.php';

class Iverve_Uploadpdf_Block_Adminhtml_Catalog_Product_Edit_Tabs
extends Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs
{
protected function _prepareLayout()
{
$product = $this->getProduct();
if (!($setId = $product->getAttributeSetId())) 
{
$setId = $this->getRequest()->getParam('set', null);
}
if ($setId) 
{
$groupCollection = Mage::getResourceModel('eav/entity_attribute_group_collection')
->setAttributeSetFilter($setId)
->setSortOrder()
->load();

foreach ($groupCollection as $group)
 {
       $attributes = $product->getAttributes($group->getId(), true);
          foreach ($attributes as $key => $attribute) {
            if(!$attribute->getIsVisible()) 
             {
              unset($attributes[$key]);
            }
         }

if (count($attributes)==0) 
{
continue;
}

$this->addTab('group_'.$group->getId(), array(
'label'     => Mage::helper('catalog')->__($group->getAttributeGroupName()),
'content'   => $this->_translateHtml($this->getLayout()->createBlock($this->getAttributeTabBlock(),
'adminhtml.catalog.product.edit.tab.attributes')->setGroup($group)
->setGroupAttributes($attributes)
->toHtml()),
));
}

$geteditid = Mage::app()->getRequest()->getParam('editid');

if (Mage::helper('core')->isModuleEnabled('Mage_CatalogInventory')) 
{
$this->addTab('inventory', array(
'label'     => Mage::helper('catalog')->__('Inventory'),
'content'   => $this->_translateHtml($this->getLayout()
->createBlock('adminhtml/catalog_product_edit_tab_inventory')->toHtml()),
));


if(Mage::getSingleton('admin/session')->isAllowed('system/config/uploadpdf') != NULL)
{
if($geteditid != '')
{
$this->addTab('Upload PDF', array(
'label'     => Mage::helper('catalog')->__('Upload PDF'),
'active'	=> true,
'content'   => $this->_translateHtml($this->getLayout()
->createBlock('uploadpdf/adminhtml_catalog_product_edit_tab_uploadpdf')->toHtml()),
));
}
else
{
$this->addTab('Upload PDF', array(
'label'     => Mage::helper('catalog')->__('Upload PDF'),
'content'   => $this->_translateHtml($this->getLayout()
->createBlock('uploadpdf/adminhtml_catalog_product_edit_tab_uploadpdf')->toHtml()),
));
}
}

}
if (!Mage::app()->isSingleStoreMode()) 
{
$this->addTab('websites', array(
'label'     => Mage::helper('catalog')->__('Websites'),
'content'   => $this->_translateHtml($this->getLayout()
->createBlock('adminhtml/catalog_product_edit_tab_websites')->toHtml()),
));
}

$this->addTab('categories', array(
'label'     => Mage::helper('catalog')->__('Categories'),
'url'       => $this->getUrl('*/*/categories', array('_current' => true)),
'class'     => 'ajax',
));

$this->addTab('related', array(
'label'     => Mage::helper('catalog')->__('Related Products'),
'url'       => $this->getUrl('*/*/related', array('_current' => true)),
'class'     => 'ajax',
));

$this->addTab('upsell', array(
'label'     => Mage::helper('catalog')->__('Up-sells'),
'url'       => $this->getUrl('*/*/upsell', array('_current' => true)),
'class'     => 'ajax',
));

$this->addTab('crosssell', array(
'label'     => Mage::helper('catalog')->__('Cross-sells'),
'url'       => $this->getUrl('*/*/crosssell', array('_current' => true)),
'class'     => 'ajax',
));
$storeId = 0;
if ($this->getRequest()->getParam('store'))
 {
$storeId = Mage::app()->getStore($this->getRequest()->getParam('store'))->getId();
}

$alertPriceAllow = Mage::getStoreConfig('catalog/productalert/allow_price');
$alertStockAllow = Mage::getStoreConfig('catalog/productalert/allow_stock');

if(($alertPriceAllow || $alertStockAllow) && !$product->isGrouped()) 
{
$this->addTab('productalert', array(
'label'     => Mage::helper('catalog')->__('Product Alerts'),
'content'   => $this->_translateHtml($this->getLayout()
->createBlock('adminhtml/catalog_product_edit_tab_alerts', 'admin.alerts.products')->toHtml())
));
}

if($this->getRequest()->getParam('id', false))
 {
if(Mage::helper('catalog')->isModuleEnabled('Mage_Review'))
 {
	if(Mage::getSingleton('admin/session')->isAllowed('admin/catalog/reviews_ratings'))
	{
$this->addTab('reviews', array(
'label' => Mage::helper('catalog')->__('Product Reviews'),
'url'   => $this->getUrl('*/*/reviews', array('_current' => true)),
'class' => 'ajax',
));
}
}
if(Mage::helper('catalog')->isModuleEnabled('Mage_Tag'))
{
if(Mage::getSingleton('admin/session')->isAllowed('admin/catalog/tag'))
{
$this->addTab('tags', array(
'label'     => Mage::helper('catalog')->__('Product Tags'),
'url'   => $this->getUrl('*/*/tagGrid', array('_current' => true)),
'class' => 'ajax',
));

$this->addTab('customers_tags', array(
'label'     => Mage::helper('catalog')->__('Customers Tagged Product'),
'url'   => $this->getUrl('*/*/tagCustomerGrid', array('_current' => true)),
'class' => 'ajax',
));
}
}
}
if(!$product->isGrouped()) 
{
$this->addTab('customer_options', array(
'label' => Mage::helper('catalog')->__('Custom Options'),
'url'   => $this->getUrl('*/*/options', array('_current' => true)),
'class' => 'ajax',
));
}
}
else 
{
$this->addTab('set', array(
'label'     => Mage::helper('catalog')->__('Settings'),
'content'   => $this->_translateHtml($this->getLayout()
->createBlock('adminhtml/catalog_product_edit_tab_settings')->toHtml()),
'active'    => true
));
}
return parent::_prepareLayout();
}   
}

后端产品tab显示模板block

app\code\local\Iverve\Uploadpdf\Block\Adminhtml\Catalog\Product\Edit\Tab
app\code\local\Iverve\Uploadpdf\Block\Adminhtml\Catalog\Product\Edit\Tab\Uploadpdf.php

class Iverve_Uploadpdf_Block_Adminhtml_Catalog_Product_Edit_Tab_Uploadpdf extends Mage_Adminhtml_Block_Widget
{
public function __construct()
{
parent::__construct();
    $this->setTemplate('iverve/uploadpdf/catalog/product/tab/uploadpdf.phtml');
}
public function getEditPdfDetail($_pdfId)
{
    return Mage::getModel('uploadpdf/uploadpdf')->getCollection()->addFieldToFilter('pdf_id', $_pdfId);
}

public function getPdfDetails($_productId)
{
    return Mage::getModel('uploadpdf/uploadpdf')->getCollection()->addFieldToFilter('product_id', $_productId);
}
}

下篇文章讲解控制器和model

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论