最新消息:爱T-blog 最新域名 blog.itiwin.cn !!! php技术博客 magento博客

magento 产品附件上传

magento 产品页面附件上传 分享可获取优惠券 并在发送的邮件 显示

uf

 

一:配置config.xml

[xml]

<?xml version=”1.0″?>
<config>
<modules>
<More_Productsmedia>
<version>0.1.0</version>
</More_Productsmedia>
</modules>
<frontend>
<routers>
<productsmedia>
<use>standard</use>
<args>
<module>More_Productsmedia</module>
<frontName>productsmedia</frontName>
</args>
</productsmedia>
</routers>
<layout>
<updates>
<productsmedia>
<file>productsmedia.xml</file>
</productsmedia>
</updates>
</layout>
</frontend>
<admin>
<routers>
<productsmedia>
<use>admin</use>
<args>
<module>More_Productsmedia</module>
<frontName>productsmedia</frontName>
</args>
</productsmedia>
</routers>
</admin>
<adminhtml>
<layout>
<updates>
<productsmedia>
<file>productsmedia.xml</file>
</productsmedia>
</updates>
</layout>
</adminhtml>
<global>
<models>
<productsmedia>
<class>More_Productsmedia_Model</class>
<resourceModel>productsmedia_mysql4</resourceModel>
</productsmedia>
<productsmedia_mysql4>
<class>More_Productsmedia_Model_Mysql4</class>
<entities>
<productsmedia>
<table>productsmedia</table>
</productsmedia>
<vote>
<table>productsmedia_vote</table>
</vote>
</entities>
</productsmedia_mysql4>
</models>
<resources>
<productsmedia_setup>
<setup>
<module>More_Productsmedia</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</productsmedia_setup>
<productsmedia_write>
<connection>
<use>core_write</use>
</connection>
</productsmedia_write>
<productsmedia_read>
<connection>
<use>core_read</use>
</connection>
</productsmedia_read>
</resources>
<blocks>
<productsmedia>
<class>More_Productsmedia_Block</class>
</productsmedia>
<!–adminhtml>
<rewrite>
<widget_grid_column>More_Productsmedia_Block_Widget_Grid_Column</widget_grid_column>
</rewrite>
</adminhtml–>
</blocks>
<helpers>
<productsmedia>
<class>More_Productsmedia_Helper</class>
</productsmedia>
</helpers>
<template>
<email>
<productsmedia_emails_new_file_email_template_to_customer translate=”label” module=”productsmedia”>
<label>New file email template to customer</label>
<file>productsmedia/new_file_email_template_to_customer.html</file>
<type>html</type>
</productsmedia_emails_new_file_email_template_to_customer>
<productsmedia_emails_new_file_email_template_to_admin translate=”label” module=”productsmedia”>
<label>New file email template to admin</label>
<file>productsmedia/new_file_email_template_to_admin.html</file>
<type>html</type>
</productsmedia_emails_new_file_email_template_to_admin>
<productsmedia_emails_approved_fle_email_template translate=”label” module=”productsmedia”>
<label>Approved file email template</label>
<file>productsmedia/approved_file_email_template_to_customer.html</file>
<type>html</type>
</productsmedia_emails_approved_fle_email_template>
<productsmedia_emails_cancel_file_email_template translate=”label” module=”productsmedia”>
<label>Cancel file email template</label>
<file>productsmedia/cancel_file_email_template_to_customer.html</file>
<type>html</type>
</productsmedia_emails_cancel_file_email_template>
</email>
</template>
</global>
<default>
<productsmedia>
<general>
<coupon_expiration_days>10</coupon_expiration_days>
<coupon_default_value>5</coupon_default_value>
</general>

<emails>
<sender_email>sales</sender_email>
<admin_email><![CDATA[admin@example.com]]></admin_email>
<new_file_email_template_to_customer>productsmedia_emails_new_file_email_template_to_customer</new_file_email_template_to_customer>
<new_file_email_template_to_admin>productsmedia_emails_new_file_email_template_to_admin</new_file_email_template_to_admin>
<approved_fle_email_template>productsmedia_emails_approved_fle_email_template</approved_fle_email_template>
<cancel_file_email_template>productsmedia_emails_cancel_file_email_template</cancel_file_email_template>
</emails>
</productsmedia>
</default>
</config>

[/xml]

二:后台产品编辑页面显示该tab

app\code\local\More\Productsmedia\Block\Adminhtml\Product  Grid.php

app\code\local\More\Productsmedia\Block\Adminhtml\Product Tab.php

[php]

<?php
class More_Productsmedia_Block_Adminhtml_Product_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId(‘productsmediatabGrid’);
$this->setDefaultSort(‘productsmedia_id’);
$this->setDefaultDir(‘DESC’);
$this->setUseAjax(true);
}

protected function _prepareCollection()
{
$product_id = $this->getRequest()->getParam(‘id’);
$collection = Mage::getModel(‘productsmedia/productsmedia’)->getCollection()->addFieldToFilter(‘product_id’, $product_id);
$this->setCollection($collection);
return parent::_prepareCollection();
}

protected function _prepareColumns()
{
$this->addColumn(‘productsmedia_id’, array(
‘header’ => Mage::helper(‘productsmedia’)->__(‘ID’),
‘align’ => ‘right’,
‘width’ => ’50px’,
‘index’ => ‘productsmedia_id’,
));

$this->addColumn(‘productsmedia_file’, array(
‘header’=> Mage::helper(‘productsmedia’)->__(‘Thumbnail’),
// ‘type’ => ‘image’,
‘width’ => ‘100px’,
‘index’ => ‘productsmedia_file’,
‘renderer’ => ‘productsmedia/adminhtml_renderer_image’,
));

$this->addColumn(‘productsmedia_title’, array(
‘header’ => Mage::helper(‘productsmedia’)->__(‘Media title’),
‘width’ => ‘300px’,
‘align’ =>’left’,
‘index’ => ‘productsmedia_title’,
));

$this->addColumn(‘productsmedia_type’, array(
‘header’ => Mage::helper(‘productsmedia’)->__(‘Media type’),
‘align’ => ‘left’,
‘width’ => ’80px’,
‘index’ => ‘productsmedia_type’,
‘type’ => ‘options’,
‘options’ => array(
1 => ‘Image’,
2 => ‘Video’,
),
));

$this->addColumn(‘productsmedia_customeremail’, array(
‘header’ => Mage::helper(‘productsmedia’)->__(‘Author Email’),
‘width’ => ‘200px’,
‘align’ =>’left’,
‘index’ => ‘productsmedia_customeremail’,
‘renderer’ => ‘productsmedia/adminhtml_renderer_customer’,
));

$this->addColumn(‘productsmedia_rate’, array(
‘header’ => Mage::helper(‘productsmedia’)->__(‘Rate’),
‘align’ =>’center’,
‘width’ => ’70px’,
‘type’ => ‘number’,
‘index’ => ‘productsmedia_rate’,
‘renderer’ => ‘productsmedia/adminhtml_renderer_rate’,
));

$this->addColumn(‘productsmedia_date’, array(
‘header’ => Mage::helper(‘productsmedia’)->__(‘Uploaded time’),
‘width’ => ‘200px’,
‘align’ => ‘left’,
‘type’ => ‘datetime’,
‘index’ => ‘productsmedia_date’,
));

$this->addColumn(‘productsmedia_status’, array(
‘header’ => Mage::helper(‘productsmedia’)->__(‘Status’),
‘align’ => ‘right’,
‘width’ => ’80px’,
‘index’ => ‘productsmedia_status’,
‘type’ => ‘options’,
‘options’ => array(
1 => ‘Approved’,
2 => ‘Canceled’,
3 => ‘Pending’,
),
));

return parent::_prepareColumns();
}

public function getGridUrl()
{
return $this->getData(‘grid_url’)
? $this->getData(‘grid_url’)
: $this->getUrl(‘*/*/producttabGrid’, array(‘_current’=>true,’id’=>$this->getRequest()->getParam(‘id’)));
}

public function getRowUrl($row)
{
return $this->getUrl(‘productsmedia/adminhtml_productsmedia/view’, array(‘id’ => $row->getId()));
}
}

[/php]

三:下面要做的是上传文件 做处理 保存 然后发送邮件

app\code\local\More\Productsmedia\controllers  IndexController.php

[php]

public function uploadAction()
{
$upload = false;
$file = “”;
$type = 2;
$data = $this->getRequest()->getPost();

$productsmedia_model = Mage::getModel(‘productsmedia/productsmedia’);

if ($data[‘media_type’] == “image”)
{
$type = 1;
$image = Mage::helper(‘productsmedia’)->uploadImage();
$file = Mage::getBaseUrl(‘media’) . ‘productsmedia’.str_replace(DS, “/”, $image);
if (isset($image)) $upload = true;
}
if ($data[‘media_type’] == “video”)
{
$type = 2;
if ($data[‘video_type’] == “video_url”)
{
$file = $data[‘file_url’];
if (isset($file)) $upload = true;
}
if ($data[‘video_type’] == “video_file”)
{
$video = Mage::helper(‘productsmedia’)->uploadYT($data[‘title’], $data[‘description’]);
$file = $video->getVideoWatchPageUrl();
if (isset($video)) $upload = true;
}
}

if ($upload)
{
Mage::getModel(‘core/session’)->addSuccess($this->__(“Your file has been uploaded successfully and is under our consideration. We’ll inform you by email soon. Thank you!”));
$productsmedia_model->setId(null)
->setData(‘productsmedia_title’, $data[‘title’])
->setData(‘productsmedia_description’, $data[‘description’])
->setData(‘productsmedia_type’, $type)
->setData(‘productsmedia_file’, $file)
->setData(‘productsmedia_customername’, $data[‘author_name’])
->setData(‘productsmedia_customeremail’, $data[‘author_email’])
->setData(‘productsmedia_date’, now())
->setData(‘product_id’, $data[‘product_id’])
->setData(‘product_name’, Mage::getModel(‘catalog/product’)->load($data[‘product_id’])->getName())
->setStoreId(Mage::app()->getStore()->getId())
->save()
;

$templateId = Mage::getStoreConfig(‘productsmedia/emails/new_file_email_template_to_customer’);
$email = $data[‘author_email’];
$name = $data[‘author_name’];
$type_list = Mage::helper(‘productsmedia’)->getTypeList();
$vars = array(
‘author_name’=> $name,
‘media_type’=> $type_list[$type],
‘product’=> Mage::getModel(‘catalog/product’)->load($data[‘product_id’])->getName(),
‘socialmedia’ => $productsmedia_model,
);
Mage::helper(‘productsmedia’)->sendMail($templateId,$email,$name,$vars);

$admin_templateId = Mage::getStoreConfig(‘productsmedia/emails/new_file_email_template_to_admin’);
$admin_email = Mage::getStoreConfig(‘productsmedia/emails/admin_email’);
$admin_name = “Admin”;
Mage::helper(‘productsmedia’)->sendMail($admin_templateId,$admin_email,$admin_name,$vars);
}

if (!$upload) Mage::getModel(‘core/session’)->addError($this->__(‘File has been uploaded fail!’));
$back_url = Mage::helper(‘core/http’)->getHttpReferer(true);
$this->getResponse()->setRedirect($back_url);
}

[/php]

四:helper助手类 ,自动发送邮件方法

[php]

public function sendMail($templateId,$email,$name,$vars)
{
$translate = Mage::getSingleton(‘core/translate’);
$translate->setTranslateInline(false);
$storeId = $vars[‘socialmedia’]->getStoreId();
$sender = Mage::getStoreConfig(self::SENDER_EMAIL);
$mailTemplate = Mage::getModel(‘core/email_template’);
$mailTemplate->setDesignConfig(array(‘area’=>’frontend’, ‘store’=>$storeId))
->sendTransactional($templateId, $sender, $email, $name, $vars);

$translate->setTranslateInline(true);
return $this;
}

[/php]

转载请注明:爱T-blog » magento 产品附件上传

与本文相关文章

发表我的评论

取消评论
表情 插代码

Hi,您需要填写昵称和邮箱!

  • 必填项
  • 必填项