我正在尝试在magento产品评论中添加额外的字段。 1.关于产品的好事 2.关于产品的坏事
我遵循此link。我已将核心文件覆盖到我的本地文件中。 这是我的代码:
模板/评论/ form.phtml
<li>
<label for="goodthings" class="required"><?php echo $this->__('Good Things') ?></label>
<div class="input-box">
<textarea name="goodthings" id="goodthings" cols="5" rows="3" ><?php echo $this->escapeHtml($data->getGoodthings()) ?></textarea>
</div>
</li>
<li>
<label for="badthings" class="required"><?php echo $this->__('Bad Things') ?></label>
<div class="input-box">
<textarea name="badthings" id="badthings" cols="5" rows="3" ><?php echo $this->escapeHtml($data->getBadthings()) ?></textarea>
</div></li>
和
/local/Mage/Review/Model/Resource/Review.php
$detail = array(
'title' => $object->getTitle(),
'detail' => $object->getDetail(),
'nickname' => $object->getNickname(),
'goodthing' => $object->getGoodthings(),
'badthing' => $object->getBadthings(),
);
数据库查询:
ALTER TABLE `review_detail` ADD `goodthing` TEXT NOT NULL COMMENT 'review goodthing' AFTER `nickname` ,
ADD `badthing` TEXT NOT NULL COMMENT 'review badthing' AFTER `goodthing` ;
/local/Mage/Review/controllers/ProductController.php
class Mage_Review_ProductController extends Mage_Core_Controller_Front_Action
{
/**
* Action list where need check enabled cookie
*
* @var array
*/
protected $_cookieCheckActions = array('post');
public function preDispatch()
{
parent::preDispatch();
$allowGuest = Mage::helper('review')->getIsGuestAllowToWrite();
if (!$this->getRequest()->isDispatched()) {
return;
}
$action = strtolower($this->getRequest()->getActionName());
if (!$allowGuest && $action == 'post' && $this->getRequest()->isPost()) {
if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
$this->setFlag('', self::FLAG_NO_DISPATCH, true);
Mage::getSingleton('customer/session')->setBeforeAuthUrl(Mage::getUrl('*/*/*', array('_current' => true)));
Mage::getSingleton('review/session')->setFormData($this->getRequest()->getPost())
->setRedirectUrl($this->_getRefererUrl());
$this->_redirectUrl(Mage::helper('customer')->getLoginUrl());
}
}
return $this;
}
/**
* Initialize and check product
*
* @return Mage_Catalog_Model_Product
*/
protected function _initProduct()
{
Mage::dispatchEvent('review_controller_product_init_before', array('controller_action'=>$this));
$categoryId = (int) $this->getRequest()->getParam('category', false);
$productId = (int) $this->getRequest()->getParam('id');
$product = $this->_loadProduct($productId);
if (!$product) {
return false;
}
if ($categoryId) {
$category = Mage::getModel('catalog/category')->load($categoryId);
Mage::register('current_category', $category);
}
try {
Mage::dispatchEvent('review_controller_product_init', array('product'=>$product));
Mage::dispatchEvent('review_controller_product_init_after', array(
'product' => $product,
'controller_action' => $this
));
} catch (Mage_Core_Exception $e) {
Mage::logException($e);
return false;
}
return $product;
}
/**
* Load product model with data by passed id.
* Return false if product was not loaded or has incorrect status.
*
* @param int $productId
* @return bool|Mage_Catalog_Model_Product
*/
protected function _loadProduct($productId)
{
if (!$productId) {
return false;
}
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load($productId);
/* @var $product Mage_Catalog_Model_Product */
if (!$product->getId() || !$product->isVisibleInCatalog() || !$product->isVisibleInSiteVisibility()) {
return false;
}
Mage::register('current_product', $product);
Mage::register('product', $product);
return $product;
}
/**
* Load review model with data by passed id.
* Return false if review was not loaded or review is not approved.
*
* @param int $productId
* @return bool|Mage_Review_Model_Review
*/
protected function _loadReview($reviewId)
{
if (!$reviewId) {
return false;
}
$review = Mage::getModel('review/review')->load($reviewId);
/* @var $review Mage_Review_Model_Review */
if (!$review->getId() || !$review->isApproved() || !$review->isAvailableOnStore(Mage::app()->getStore())) {
return false;
}
Mage::register('current_review', $review);
return $review;
}
/**
* Submit new review action
*
*/
public function postAction()
{
if (!$this->_validateFormKey()) {
// returns to the product item page
$this->_redirectReferer();
return;
}
if ($data = Mage::getSingleton('review/session')->getFormData(true)) {
$rating = array();
if (isset($data['ratings']) && is_array($data['ratings'])) {
$rating = $data['ratings'];
}
} else {
$data = $this->getRequest()->getPost();
$rating = $this->getRequest()->getParam('ratings', array());
}
if (($product = $this->_initProduct()) && !empty($data)) {
$session = Mage::getSingleton('core/session');
/* @var $session Mage_Core_Model_Session */
$review = Mage::getModel('review/review')->setData($this->_cropReviewData($data));
/* @var $review Mage_Review_Model_Review */
$validate = $review->validate();
if ($validate === true) {
try {
$review->setEntityId($review->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE))
->setEntityPkValue($product->getId())
->setStatusId(Mage_Review_Model_Review::STATUS_PENDING)
->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
->setStoreId(Mage::app()->getStore()->getId())
->setStores(array(Mage::app()->getStore()->getId()))
->save();
foreach ($rating as $ratingId => $optionId) {
Mage::getModel('rating/rating')
->setRatingId($ratingId)
->setReviewId($review->getId())
->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
->addOptionVote($optionId, $product->getId());
}
$review->aggregate();
$session->addSuccess($this->__('Your review has been accepted for moderation.'));
}
catch (Exception $e) {
$session->setFormData($data);
$session->addError($this->__('Unable to post the review.'));
}
}
else {
$session->setFormData($data);
if (is_array($validate)) {
foreach ($validate as $errorMessage) {
$session->addError($errorMessage);
}
}
else {
$session->addError($this->__('Unable to post the review.'));
}
}
}
if ($redirectUrl = Mage::getSingleton('review/session')->getRedirectUrl(true)) {
$this->_redirectUrl($redirectUrl);
return;
}
$this->_redirectReferer();
}
/**
* Show list of product's reviews
*
*/
public function listAction()
{
if ($product = $this->_initProduct()) {
Mage::register('productId', $product->getId());
$design = Mage::getSingleton('catalog/design');
$settings = $design->getDesignSettings($product);
if ($settings->getCustomDesign()) {
$design->applyCustomDesign($settings->getCustomDesign());
}
$this->_initProductLayout($product);
// update breadcrumbs
if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) {
$breadcrumbsBlock->addCrumb('product', array(
'label' => $product->getName(),
'link' => $product->getProductUrl(),
'readonly' => true,
));
$breadcrumbsBlock->addCrumb('reviews', array('label' => Mage::helper('review')->__('Product Reviews')));
}
$this->renderLayout();
} elseif (!$this->getResponse()->isRedirect()) {
$this->_forward('noRoute');
}
}
/**
* Show details of one review
*
*/
public function viewAction()
{
$review = $this->_loadReview((int) $this->getRequest()->getParam('id'));
if (!$review) {
$this->_forward('noroute');
return;
}
$product = $this->_loadProduct($review->getEntityPkValue());
if (!$product) {
$this->_forward('noroute');
return;
}
$this->loadLayout();
$this->_initLayoutMessages('review/session');
$this->_initLayoutMessages('catalog/session');
$this->renderLayout();
}
/**
* Load specific layout handles by product type id
*
*/
protected function _initProductLayout($product)
{
$update = $this->getLayout()->getUpdate();
$update->addHandle('default');
$this->addActionLayoutHandles();
$update->addHandle('PRODUCT_TYPE_'.$product->getTypeId());
if ($product->getPageLayout()) {
$this->getLayout()->helper('page/layout')
->applyHandle($product->getPageLayout());
}
$this->loadLayoutUpdates();
if ($product->getPageLayout()) {
$this->getLayout()->helper('page/layout')
->applyTemplate($product->getPageLayout());
}
$update->addUpdate($product->getCustomLayoutUpdate());
$this->generateLayoutXml()->generateLayoutBlocks();
}
/**
* Crops POST values
* @param array $reviewData
* @return array
*/
protected function _cropReviewData(array $reviewData)
{
$croppedValues = array();
$allowedKeys = array_fill_keys(array('detail', 'title', 'nickname','goodthings','badthings'), true);
foreach ($reviewData as $key => $value) {
if (isset($allowedKeys[$key])) {
$croppedValues[$key] = $value;
}
}
return $croppedValues;
}
}
表单提交后没有保存自定义字段的值,但默认字段正在保存到数据库,我已尝试使用Mage::log();
进行调试,我正在获取控制器中所有字段的值,但是没有进入模型和我的模型中没有日志打印。
答案 0 :(得分:1)
我遇到了同样的问题,但能够解决它。以下是步骤:
1)我的phtml文件:添加了自定义字段'电子邮件'
<li>
<label for="email_field" class="required"><?php echo $this->__('Email Address') ?> :</label>
<div class="input-box">
<input type="text" name="email" id="email_field" value="<?php echo $email; ?>" class="form-control validate-email" placeholder="Enter your Email" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter your Email'" />
</div>
</li>
2)创建sql脚本以向 review_detail 表添加新字段
$installer = $this;
$installer->startSetup();
$installer->getConnection()
->addColumn(
$installer->getTable('review/review_detail'),
'customer_email',array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => false,
'length' => 255,
'after' => 'detail',
'comment' => 'Customer Email'
)
);
$installer->endSetup();
3)覆盖 Mage / Review / controllers / ProductController.php 并编辑方法 _cropReviewData
protected function _cropReviewData(array $reviewData)
{
$croppedValues = array();
$allowedKeys = array_fill_keys(array('detail', 'title', 'nickname','customer_email'), true);
foreach ($reviewData as $key => $value) {
if (isset($allowedKeys[$key])) {
$croppedValues[$key] = $value;
}
}
return $croppedValues;
}
我们已将自定义字段 customer_email 添加到数组
3)覆盖 Mage_Review_Model_Resource_Review.php 并编辑方法 _afterSave 并将自定义字段添加到 $ detail 数组
$detail = array(
'title' => $object->getTitle(),
'detail' => $object->getDetail(),
'nickname' => $object->getNickname(),
'customer_email' => $object->getCustomerEmail(),
);
完成上述更改后,自定义字段的值将保存到数据库
答案 1 :(得分:0)
请尝试此操作,如果没有解决,请告知我们保存评论的控制器代码