Magento2 Add custom field to review form












1















I am trying to add a custom field in review form in the product page. Below is my code:



InstallData.php



namespace NamespaceMymoduleSetup;

use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
Class InstallData implements InstallDataInterface
{
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$setup->startSetup();
// $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$reviewTable = 'review_detail';

$setup->getConnection()
->addColumn(
$setup->getTable($reviewTable),
'refer_field',
[
'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
'label' => 'Would you recommend this product to a friend',
'comment' =>'Would you recommend this product to a friend',
'input' => 'select',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 101,
'position' => 101,
'system' => 0,
'option' =>
array (
'values' =>
array (
0 => 'Yes',
1 => 'No',
),
),
]
);
$setup->endSetup();
}
}


app/code/Namespace/Mymodule/view/frontend



<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="review_product_form_component"/>
<body>
<referenceBlock name="product.info.details">
<block class="MagentoReviewBlockProductReview" name="reviews_tab" as="reviews" template="Namespace_Mymodule::review.phtml" group="detailed_info">
<block class="MagentoReviewBlockForm" name="review_form" template="Namespace_Mymodule::form.phtml" as="review_form">
<container name="product.review.form.fields.before" as="form_fields_before" label="Review Form Fields Before"/>
</block>
</block>
</referenceBlock>
</body>
</page>


app/code/Namespace/Mymodule/view/frontend/templates



<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

// @codingStandardsIgnoreFile

/**
* @var $block MagentoReviewBlockForm
*/
?>
<div class="block review-add">
<div class="block-title"><strong><?php /* @escapeNotVerified */ echo __('Write Your Own Review') ?></strong></div>
<div class="block-content">
<?php if ($block->getAllowWriteReviewFlag()): ?>
<form action="<?php /* @escapeNotVerified */ echo $block->getAction() ?>" class="review-form" method="post" id="review-form" data-role="product-review-form" data-bind="scope: 'review-form'">
<?php echo $block->getBlockHtml('formkey'); ?>
<?php echo $block->getChildHtml('form_fields_before')?>
<fieldset class="fieldset review-fieldset" data-hasrequired="<?php __('* Required Fields'); ?>">
<legend class="legend review-legend"><span><?php /* @escapeNotVerified */ echo __("You're reviewing:"); ?></span><strong><?php echo $block->escapeHtml($block->getProductInfo()->getName()) ?></strong></legend><br />
<?php if ($block->getRatings() && $block->getRatings()->getSize()): ?>
<span id="input-message-box"></span>
<fieldset class="field required review-field-ratings">
<legend class="label"><span><?php /* @escapeNotVerified */ echo __('Your Rating') ?><span></legend><br/>
<div class="control">
<div class="nested" id="product-review-table">
<?php foreach ($block->getRatings() as $_rating): ?>
<div class="field choice review-field-rating">
<label class="label" id="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_rating_label"><span><?php echo $block->escapeHtml($_rating->getRatingCode()) ?></span></label>
<div class="control review-control-vote">
<?php $options = $_rating->getOptions();?>
<?php $iterator = 1; foreach ($options as $_option): ?>
<input
type="radio"
name="ratings[<?php /* @escapeNotVerified */ echo $_rating->getId() ?>]"
id="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>"
value="<?php /* @escapeNotVerified */ echo $_option->getId() ?>"
class="radio"
data-validate="{required:true, messages:{required:'Please select one of each of the ratings above.'}}"
aria-labelledby="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_rating_label <?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>_label" />
<label
class="rating-<?php /* @escapeNotVerified */ echo $iterator; ?>"
for="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>"
title="<?php /* @escapeNotVerified */ echo __('%1 %2', $iterator, $iterator > 1 ? 'stars' : 'star') ?>"
id="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>_label">
<span><?php /*@escapeNotVerified*/ echo __('%1 %2', $iterator, $iterator > 1 ? 'stars' : 'star') ?></span>
</label>
<?php $iterator++; ?>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<input type="hidden" name="validate_rating" class="validate-rating" value="" />
</div>
</fieldset>
<?php endif ?>
<div class="field review-field-nickname required">
<label for="nickname_field" class="label"><span><?php /*@escapeNotVerified*/ echo __('Nickname') ?></span></label>
<div class="control">
<input type="text" name="nickname" id="nickname_field" class="input-text" data-validate="{required:true}" data-bind="value: nickname()" />
</div>
</div>
<div class="field review-field-summary required">
<label for="summary_field" class="label"><span><?php /*@escapeNotVerified*/ echo __('Summary') ?></span></label>
<div class="control">
<input type="text" name="title" id="summary_field" class="input-text" data-validate="{required:true}" data-bind="value: review().title" />
</div>
</div>
<div class="field review-field-text required">
<label for="review_field" class="label"><span><?php /*@escapeNotVerified*/ echo __('Review') ?></span></label>
<div class="control">
<textarea name="detail" id="review_field" cols="5" rows="3" data-validate="{required:true}" data-bind="value: review().detail"></textarea>
</div>
</div>
<div class="field review-field-refer required">
<label for="review_field" class="label"><span><?php/*@escapeNotVerified*/ echo __('Would you recommend this product to a friend') ?></span></label>
<div class="control">
<select name="refer_field">
<option name="yes" value="yes">Yes</option>
<option name="no" value="no">No</option>
</select>
</div>
</div>
</fieldset>
<div class="actions-toolbar review-form-actions">
<div class="primary actions-primary">
<button type="submit" class="action submit primary"><span><?php/*@escapeNotVerified*/ echo __('Submit Review') ?></span></button>
</div>
</div>
</form>
<script type="text/x-magento-init">
{
"[data-role=product-review-form]": {
"Magento_Ui/js/core/app": <?php /* @escapeNotVerified */ echo $block->getJsLayout();?>
},
"#review-form": {
"Magento_Review/js/error-placement": {}
}
}
</script>
<?php else: ?>
<div class="message info notlogged" id="review-form">
<div>
<?php /* @escapeNotVerified */ echo __('Only registered users can write reviews. Please <a href="%1">Sign in</a> or <a href="%2">create an account</a>', $block->getLoginLink(), $block->getRegisterUrl()) ?>
</div>
</div>
<?php endif ?>
</div>
</div>


What is the proper way to save data in the database and how to show the same field in the admin panel where review form appears?



Thanks in advance!










share|improve this question

























  • you mean review appear in front-end right?

    – Ramesh S
    Apr 11 '17 at 12:42











  • that custom field should show in frontend and backend form and saved in database.

    – Keith
    Apr 11 '17 at 12:43











  • What template you are using..?

    – Ramesh S
    Apr 11 '17 at 12:48











  • I'm having the same issue.. I have created two custom fields in the registration form (by overriding the 'register.phtml')..I want to save their values in the database (attribute IDs have been successfully created in the DB through InstallData.php)..Please let me know if you have found any solution to this..

    – Kartik
    Apr 11 '17 at 13:25
















1















I am trying to add a custom field in review form in the product page. Below is my code:



InstallData.php



namespace NamespaceMymoduleSetup;

use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
Class InstallData implements InstallDataInterface
{
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$setup->startSetup();
// $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$reviewTable = 'review_detail';

$setup->getConnection()
->addColumn(
$setup->getTable($reviewTable),
'refer_field',
[
'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
'label' => 'Would you recommend this product to a friend',
'comment' =>'Would you recommend this product to a friend',
'input' => 'select',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 101,
'position' => 101,
'system' => 0,
'option' =>
array (
'values' =>
array (
0 => 'Yes',
1 => 'No',
),
),
]
);
$setup->endSetup();
}
}


app/code/Namespace/Mymodule/view/frontend



<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="review_product_form_component"/>
<body>
<referenceBlock name="product.info.details">
<block class="MagentoReviewBlockProductReview" name="reviews_tab" as="reviews" template="Namespace_Mymodule::review.phtml" group="detailed_info">
<block class="MagentoReviewBlockForm" name="review_form" template="Namespace_Mymodule::form.phtml" as="review_form">
<container name="product.review.form.fields.before" as="form_fields_before" label="Review Form Fields Before"/>
</block>
</block>
</referenceBlock>
</body>
</page>


app/code/Namespace/Mymodule/view/frontend/templates



<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

// @codingStandardsIgnoreFile

/**
* @var $block MagentoReviewBlockForm
*/
?>
<div class="block review-add">
<div class="block-title"><strong><?php /* @escapeNotVerified */ echo __('Write Your Own Review') ?></strong></div>
<div class="block-content">
<?php if ($block->getAllowWriteReviewFlag()): ?>
<form action="<?php /* @escapeNotVerified */ echo $block->getAction() ?>" class="review-form" method="post" id="review-form" data-role="product-review-form" data-bind="scope: 'review-form'">
<?php echo $block->getBlockHtml('formkey'); ?>
<?php echo $block->getChildHtml('form_fields_before')?>
<fieldset class="fieldset review-fieldset" data-hasrequired="<?php __('* Required Fields'); ?>">
<legend class="legend review-legend"><span><?php /* @escapeNotVerified */ echo __("You're reviewing:"); ?></span><strong><?php echo $block->escapeHtml($block->getProductInfo()->getName()) ?></strong></legend><br />
<?php if ($block->getRatings() && $block->getRatings()->getSize()): ?>
<span id="input-message-box"></span>
<fieldset class="field required review-field-ratings">
<legend class="label"><span><?php /* @escapeNotVerified */ echo __('Your Rating') ?><span></legend><br/>
<div class="control">
<div class="nested" id="product-review-table">
<?php foreach ($block->getRatings() as $_rating): ?>
<div class="field choice review-field-rating">
<label class="label" id="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_rating_label"><span><?php echo $block->escapeHtml($_rating->getRatingCode()) ?></span></label>
<div class="control review-control-vote">
<?php $options = $_rating->getOptions();?>
<?php $iterator = 1; foreach ($options as $_option): ?>
<input
type="radio"
name="ratings[<?php /* @escapeNotVerified */ echo $_rating->getId() ?>]"
id="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>"
value="<?php /* @escapeNotVerified */ echo $_option->getId() ?>"
class="radio"
data-validate="{required:true, messages:{required:'Please select one of each of the ratings above.'}}"
aria-labelledby="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_rating_label <?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>_label" />
<label
class="rating-<?php /* @escapeNotVerified */ echo $iterator; ?>"
for="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>"
title="<?php /* @escapeNotVerified */ echo __('%1 %2', $iterator, $iterator > 1 ? 'stars' : 'star') ?>"
id="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>_label">
<span><?php /*@escapeNotVerified*/ echo __('%1 %2', $iterator, $iterator > 1 ? 'stars' : 'star') ?></span>
</label>
<?php $iterator++; ?>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<input type="hidden" name="validate_rating" class="validate-rating" value="" />
</div>
</fieldset>
<?php endif ?>
<div class="field review-field-nickname required">
<label for="nickname_field" class="label"><span><?php /*@escapeNotVerified*/ echo __('Nickname') ?></span></label>
<div class="control">
<input type="text" name="nickname" id="nickname_field" class="input-text" data-validate="{required:true}" data-bind="value: nickname()" />
</div>
</div>
<div class="field review-field-summary required">
<label for="summary_field" class="label"><span><?php /*@escapeNotVerified*/ echo __('Summary') ?></span></label>
<div class="control">
<input type="text" name="title" id="summary_field" class="input-text" data-validate="{required:true}" data-bind="value: review().title" />
</div>
</div>
<div class="field review-field-text required">
<label for="review_field" class="label"><span><?php /*@escapeNotVerified*/ echo __('Review') ?></span></label>
<div class="control">
<textarea name="detail" id="review_field" cols="5" rows="3" data-validate="{required:true}" data-bind="value: review().detail"></textarea>
</div>
</div>
<div class="field review-field-refer required">
<label for="review_field" class="label"><span><?php/*@escapeNotVerified*/ echo __('Would you recommend this product to a friend') ?></span></label>
<div class="control">
<select name="refer_field">
<option name="yes" value="yes">Yes</option>
<option name="no" value="no">No</option>
</select>
</div>
</div>
</fieldset>
<div class="actions-toolbar review-form-actions">
<div class="primary actions-primary">
<button type="submit" class="action submit primary"><span><?php/*@escapeNotVerified*/ echo __('Submit Review') ?></span></button>
</div>
</div>
</form>
<script type="text/x-magento-init">
{
"[data-role=product-review-form]": {
"Magento_Ui/js/core/app": <?php /* @escapeNotVerified */ echo $block->getJsLayout();?>
},
"#review-form": {
"Magento_Review/js/error-placement": {}
}
}
</script>
<?php else: ?>
<div class="message info notlogged" id="review-form">
<div>
<?php /* @escapeNotVerified */ echo __('Only registered users can write reviews. Please <a href="%1">Sign in</a> or <a href="%2">create an account</a>', $block->getLoginLink(), $block->getRegisterUrl()) ?>
</div>
</div>
<?php endif ?>
</div>
</div>


What is the proper way to save data in the database and how to show the same field in the admin panel where review form appears?



Thanks in advance!










share|improve this question

























  • you mean review appear in front-end right?

    – Ramesh S
    Apr 11 '17 at 12:42











  • that custom field should show in frontend and backend form and saved in database.

    – Keith
    Apr 11 '17 at 12:43











  • What template you are using..?

    – Ramesh S
    Apr 11 '17 at 12:48











  • I'm having the same issue.. I have created two custom fields in the registration form (by overriding the 'register.phtml')..I want to save their values in the database (attribute IDs have been successfully created in the DB through InstallData.php)..Please let me know if you have found any solution to this..

    – Kartik
    Apr 11 '17 at 13:25














1












1








1


1






I am trying to add a custom field in review form in the product page. Below is my code:



InstallData.php



namespace NamespaceMymoduleSetup;

use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
Class InstallData implements InstallDataInterface
{
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$setup->startSetup();
// $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$reviewTable = 'review_detail';

$setup->getConnection()
->addColumn(
$setup->getTable($reviewTable),
'refer_field',
[
'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
'label' => 'Would you recommend this product to a friend',
'comment' =>'Would you recommend this product to a friend',
'input' => 'select',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 101,
'position' => 101,
'system' => 0,
'option' =>
array (
'values' =>
array (
0 => 'Yes',
1 => 'No',
),
),
]
);
$setup->endSetup();
}
}


app/code/Namespace/Mymodule/view/frontend



<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="review_product_form_component"/>
<body>
<referenceBlock name="product.info.details">
<block class="MagentoReviewBlockProductReview" name="reviews_tab" as="reviews" template="Namespace_Mymodule::review.phtml" group="detailed_info">
<block class="MagentoReviewBlockForm" name="review_form" template="Namespace_Mymodule::form.phtml" as="review_form">
<container name="product.review.form.fields.before" as="form_fields_before" label="Review Form Fields Before"/>
</block>
</block>
</referenceBlock>
</body>
</page>


app/code/Namespace/Mymodule/view/frontend/templates



<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

// @codingStandardsIgnoreFile

/**
* @var $block MagentoReviewBlockForm
*/
?>
<div class="block review-add">
<div class="block-title"><strong><?php /* @escapeNotVerified */ echo __('Write Your Own Review') ?></strong></div>
<div class="block-content">
<?php if ($block->getAllowWriteReviewFlag()): ?>
<form action="<?php /* @escapeNotVerified */ echo $block->getAction() ?>" class="review-form" method="post" id="review-form" data-role="product-review-form" data-bind="scope: 'review-form'">
<?php echo $block->getBlockHtml('formkey'); ?>
<?php echo $block->getChildHtml('form_fields_before')?>
<fieldset class="fieldset review-fieldset" data-hasrequired="<?php __('* Required Fields'); ?>">
<legend class="legend review-legend"><span><?php /* @escapeNotVerified */ echo __("You're reviewing:"); ?></span><strong><?php echo $block->escapeHtml($block->getProductInfo()->getName()) ?></strong></legend><br />
<?php if ($block->getRatings() && $block->getRatings()->getSize()): ?>
<span id="input-message-box"></span>
<fieldset class="field required review-field-ratings">
<legend class="label"><span><?php /* @escapeNotVerified */ echo __('Your Rating') ?><span></legend><br/>
<div class="control">
<div class="nested" id="product-review-table">
<?php foreach ($block->getRatings() as $_rating): ?>
<div class="field choice review-field-rating">
<label class="label" id="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_rating_label"><span><?php echo $block->escapeHtml($_rating->getRatingCode()) ?></span></label>
<div class="control review-control-vote">
<?php $options = $_rating->getOptions();?>
<?php $iterator = 1; foreach ($options as $_option): ?>
<input
type="radio"
name="ratings[<?php /* @escapeNotVerified */ echo $_rating->getId() ?>]"
id="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>"
value="<?php /* @escapeNotVerified */ echo $_option->getId() ?>"
class="radio"
data-validate="{required:true, messages:{required:'Please select one of each of the ratings above.'}}"
aria-labelledby="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_rating_label <?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>_label" />
<label
class="rating-<?php /* @escapeNotVerified */ echo $iterator; ?>"
for="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>"
title="<?php /* @escapeNotVerified */ echo __('%1 %2', $iterator, $iterator > 1 ? 'stars' : 'star') ?>"
id="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>_label">
<span><?php /*@escapeNotVerified*/ echo __('%1 %2', $iterator, $iterator > 1 ? 'stars' : 'star') ?></span>
</label>
<?php $iterator++; ?>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<input type="hidden" name="validate_rating" class="validate-rating" value="" />
</div>
</fieldset>
<?php endif ?>
<div class="field review-field-nickname required">
<label for="nickname_field" class="label"><span><?php /*@escapeNotVerified*/ echo __('Nickname') ?></span></label>
<div class="control">
<input type="text" name="nickname" id="nickname_field" class="input-text" data-validate="{required:true}" data-bind="value: nickname()" />
</div>
</div>
<div class="field review-field-summary required">
<label for="summary_field" class="label"><span><?php /*@escapeNotVerified*/ echo __('Summary') ?></span></label>
<div class="control">
<input type="text" name="title" id="summary_field" class="input-text" data-validate="{required:true}" data-bind="value: review().title" />
</div>
</div>
<div class="field review-field-text required">
<label for="review_field" class="label"><span><?php /*@escapeNotVerified*/ echo __('Review') ?></span></label>
<div class="control">
<textarea name="detail" id="review_field" cols="5" rows="3" data-validate="{required:true}" data-bind="value: review().detail"></textarea>
</div>
</div>
<div class="field review-field-refer required">
<label for="review_field" class="label"><span><?php/*@escapeNotVerified*/ echo __('Would you recommend this product to a friend') ?></span></label>
<div class="control">
<select name="refer_field">
<option name="yes" value="yes">Yes</option>
<option name="no" value="no">No</option>
</select>
</div>
</div>
</fieldset>
<div class="actions-toolbar review-form-actions">
<div class="primary actions-primary">
<button type="submit" class="action submit primary"><span><?php/*@escapeNotVerified*/ echo __('Submit Review') ?></span></button>
</div>
</div>
</form>
<script type="text/x-magento-init">
{
"[data-role=product-review-form]": {
"Magento_Ui/js/core/app": <?php /* @escapeNotVerified */ echo $block->getJsLayout();?>
},
"#review-form": {
"Magento_Review/js/error-placement": {}
}
}
</script>
<?php else: ?>
<div class="message info notlogged" id="review-form">
<div>
<?php /* @escapeNotVerified */ echo __('Only registered users can write reviews. Please <a href="%1">Sign in</a> or <a href="%2">create an account</a>', $block->getLoginLink(), $block->getRegisterUrl()) ?>
</div>
</div>
<?php endif ?>
</div>
</div>


What is the proper way to save data in the database and how to show the same field in the admin panel where review form appears?



Thanks in advance!










share|improve this question
















I am trying to add a custom field in review form in the product page. Below is my code:



InstallData.php



namespace NamespaceMymoduleSetup;

use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
Class InstallData implements InstallDataInterface
{
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$setup->startSetup();
// $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$reviewTable = 'review_detail';

$setup->getConnection()
->addColumn(
$setup->getTable($reviewTable),
'refer_field',
[
'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
'label' => 'Would you recommend this product to a friend',
'comment' =>'Would you recommend this product to a friend',
'input' => 'select',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 101,
'position' => 101,
'system' => 0,
'option' =>
array (
'values' =>
array (
0 => 'Yes',
1 => 'No',
),
),
]
);
$setup->endSetup();
}
}


app/code/Namespace/Mymodule/view/frontend



<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="review_product_form_component"/>
<body>
<referenceBlock name="product.info.details">
<block class="MagentoReviewBlockProductReview" name="reviews_tab" as="reviews" template="Namespace_Mymodule::review.phtml" group="detailed_info">
<block class="MagentoReviewBlockForm" name="review_form" template="Namespace_Mymodule::form.phtml" as="review_form">
<container name="product.review.form.fields.before" as="form_fields_before" label="Review Form Fields Before"/>
</block>
</block>
</referenceBlock>
</body>
</page>


app/code/Namespace/Mymodule/view/frontend/templates



<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

// @codingStandardsIgnoreFile

/**
* @var $block MagentoReviewBlockForm
*/
?>
<div class="block review-add">
<div class="block-title"><strong><?php /* @escapeNotVerified */ echo __('Write Your Own Review') ?></strong></div>
<div class="block-content">
<?php if ($block->getAllowWriteReviewFlag()): ?>
<form action="<?php /* @escapeNotVerified */ echo $block->getAction() ?>" class="review-form" method="post" id="review-form" data-role="product-review-form" data-bind="scope: 'review-form'">
<?php echo $block->getBlockHtml('formkey'); ?>
<?php echo $block->getChildHtml('form_fields_before')?>
<fieldset class="fieldset review-fieldset" data-hasrequired="<?php __('* Required Fields'); ?>">
<legend class="legend review-legend"><span><?php /* @escapeNotVerified */ echo __("You're reviewing:"); ?></span><strong><?php echo $block->escapeHtml($block->getProductInfo()->getName()) ?></strong></legend><br />
<?php if ($block->getRatings() && $block->getRatings()->getSize()): ?>
<span id="input-message-box"></span>
<fieldset class="field required review-field-ratings">
<legend class="label"><span><?php /* @escapeNotVerified */ echo __('Your Rating') ?><span></legend><br/>
<div class="control">
<div class="nested" id="product-review-table">
<?php foreach ($block->getRatings() as $_rating): ?>
<div class="field choice review-field-rating">
<label class="label" id="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_rating_label"><span><?php echo $block->escapeHtml($_rating->getRatingCode()) ?></span></label>
<div class="control review-control-vote">
<?php $options = $_rating->getOptions();?>
<?php $iterator = 1; foreach ($options as $_option): ?>
<input
type="radio"
name="ratings[<?php /* @escapeNotVerified */ echo $_rating->getId() ?>]"
id="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>"
value="<?php /* @escapeNotVerified */ echo $_option->getId() ?>"
class="radio"
data-validate="{required:true, messages:{required:'Please select one of each of the ratings above.'}}"
aria-labelledby="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_rating_label <?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>_label" />
<label
class="rating-<?php /* @escapeNotVerified */ echo $iterator; ?>"
for="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>"
title="<?php /* @escapeNotVerified */ echo __('%1 %2', $iterator, $iterator > 1 ? 'stars' : 'star') ?>"
id="<?php echo $block->escapeHtml($_rating->getRatingCode()) ?>_<?php /* @escapeNotVerified */ echo $_option->getValue() ?>_label">
<span><?php /*@escapeNotVerified*/ echo __('%1 %2', $iterator, $iterator > 1 ? 'stars' : 'star') ?></span>
</label>
<?php $iterator++; ?>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<input type="hidden" name="validate_rating" class="validate-rating" value="" />
</div>
</fieldset>
<?php endif ?>
<div class="field review-field-nickname required">
<label for="nickname_field" class="label"><span><?php /*@escapeNotVerified*/ echo __('Nickname') ?></span></label>
<div class="control">
<input type="text" name="nickname" id="nickname_field" class="input-text" data-validate="{required:true}" data-bind="value: nickname()" />
</div>
</div>
<div class="field review-field-summary required">
<label for="summary_field" class="label"><span><?php /*@escapeNotVerified*/ echo __('Summary') ?></span></label>
<div class="control">
<input type="text" name="title" id="summary_field" class="input-text" data-validate="{required:true}" data-bind="value: review().title" />
</div>
</div>
<div class="field review-field-text required">
<label for="review_field" class="label"><span><?php /*@escapeNotVerified*/ echo __('Review') ?></span></label>
<div class="control">
<textarea name="detail" id="review_field" cols="5" rows="3" data-validate="{required:true}" data-bind="value: review().detail"></textarea>
</div>
</div>
<div class="field review-field-refer required">
<label for="review_field" class="label"><span><?php/*@escapeNotVerified*/ echo __('Would you recommend this product to a friend') ?></span></label>
<div class="control">
<select name="refer_field">
<option name="yes" value="yes">Yes</option>
<option name="no" value="no">No</option>
</select>
</div>
</div>
</fieldset>
<div class="actions-toolbar review-form-actions">
<div class="primary actions-primary">
<button type="submit" class="action submit primary"><span><?php/*@escapeNotVerified*/ echo __('Submit Review') ?></span></button>
</div>
</div>
</form>
<script type="text/x-magento-init">
{
"[data-role=product-review-form]": {
"Magento_Ui/js/core/app": <?php /* @escapeNotVerified */ echo $block->getJsLayout();?>
},
"#review-form": {
"Magento_Review/js/error-placement": {}
}
}
</script>
<?php else: ?>
<div class="message info notlogged" id="review-form">
<div>
<?php /* @escapeNotVerified */ echo __('Only registered users can write reviews. Please <a href="%1">Sign in</a> or <a href="%2">create an account</a>', $block->getLoginLink(), $block->getRegisterUrl()) ?>
</div>
</div>
<?php endif ?>
</div>
</div>


What is the proper way to save data in the database and how to show the same field in the admin panel where review form appears?



Thanks in advance!







magento2 magento-2.1 magento-2.1.3 review product-review






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 19 '17 at 9:48









Mohit Kumar Arora

6,33841532




6,33841532










asked Apr 11 '17 at 12:30









KeithKeith

347215




347215













  • you mean review appear in front-end right?

    – Ramesh S
    Apr 11 '17 at 12:42











  • that custom field should show in frontend and backend form and saved in database.

    – Keith
    Apr 11 '17 at 12:43











  • What template you are using..?

    – Ramesh S
    Apr 11 '17 at 12:48











  • I'm having the same issue.. I have created two custom fields in the registration form (by overriding the 'register.phtml')..I want to save their values in the database (attribute IDs have been successfully created in the DB through InstallData.php)..Please let me know if you have found any solution to this..

    – Kartik
    Apr 11 '17 at 13:25



















  • you mean review appear in front-end right?

    – Ramesh S
    Apr 11 '17 at 12:42











  • that custom field should show in frontend and backend form and saved in database.

    – Keith
    Apr 11 '17 at 12:43











  • What template you are using..?

    – Ramesh S
    Apr 11 '17 at 12:48











  • I'm having the same issue.. I have created two custom fields in the registration form (by overriding the 'register.phtml')..I want to save their values in the database (attribute IDs have been successfully created in the DB through InstallData.php)..Please let me know if you have found any solution to this..

    – Kartik
    Apr 11 '17 at 13:25

















you mean review appear in front-end right?

– Ramesh S
Apr 11 '17 at 12:42





you mean review appear in front-end right?

– Ramesh S
Apr 11 '17 at 12:42













that custom field should show in frontend and backend form and saved in database.

– Keith
Apr 11 '17 at 12:43





that custom field should show in frontend and backend form and saved in database.

– Keith
Apr 11 '17 at 12:43













What template you are using..?

– Ramesh S
Apr 11 '17 at 12:48





What template you are using..?

– Ramesh S
Apr 11 '17 at 12:48













I'm having the same issue.. I have created two custom fields in the registration form (by overriding the 'register.phtml')..I want to save their values in the database (attribute IDs have been successfully created in the DB through InstallData.php)..Please let me know if you have found any solution to this..

– Kartik
Apr 11 '17 at 13:25





I'm having the same issue.. I have created two custom fields in the registration form (by overriding the 'register.phtml')..I want to save their values in the database (attribute IDs have been successfully created in the DB through InstallData.php)..Please let me know if you have found any solution to this..

– Kartik
Apr 11 '17 at 13:25










2 Answers
2






active

oldest

votes


















1














You need to overwrite the ResourceModel. At 4 am in morning I am able to figure it now :-)



Add the below XML:



<preference for="MagentoReviewModelResourceModelReview" type="NSModuleModelResourceModelReview" />


and add the following php code:



<?php

namespace NSModuleModelResourceModel;

use MagentoFrameworkModelAbstractModel;

/**
* Review resource model
*/
class Review extends MagentoReviewModelResourceModelReview
{
/**
* Perform actions after object save
*
* @param MagentoFrameworkModelAbstractModel $object
* @return $this
*/
protected function _afterSave(AbstractModel $object)
{
$connection = $this->getConnection();
/**
* save detail
*/
$detail = [
'title' => $object->getTitle(),
'detail' => $object->getDetail(),
'nickname' => $object->getNickname(),
'refer_field' => $object->getReferField(),
];
.
.
.


Just overwrite the function "_afterSave" not the whole file.






share|improve this answer































    0














    First thing, you add new column to table so you should create file InstallSchema.php, not InstallData.php in your description.



    And some code is unused, I think the code below is enough.



    namespace NamespaceMymoduleSetup;

    use MagentoFrameworkSetupInstallDataInterface;
    use MagentoFrameworkSetupModuleContextInterface;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    Class InstallSchema implements InstallSchemaInterface
    {
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
    $setup->startSetup();

    $reviewTable = 'review_detail';

    $setup->getConnection()
    ->addColumn(
    $setup->getTable($reviewTable),
    'refer_field',
    [
    'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
    'nullable' => true,
    'comment' =>'Would you recommend this product to a friend'
    ]
    );
    $setup->endSetup();
    }
    }




    share























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "479"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f169643%2fmagento2-add-custom-field-to-review-form%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You need to overwrite the ResourceModel. At 4 am in morning I am able to figure it now :-)



      Add the below XML:



      <preference for="MagentoReviewModelResourceModelReview" type="NSModuleModelResourceModelReview" />


      and add the following php code:



      <?php

      namespace NSModuleModelResourceModel;

      use MagentoFrameworkModelAbstractModel;

      /**
      * Review resource model
      */
      class Review extends MagentoReviewModelResourceModelReview
      {
      /**
      * Perform actions after object save
      *
      * @param MagentoFrameworkModelAbstractModel $object
      * @return $this
      */
      protected function _afterSave(AbstractModel $object)
      {
      $connection = $this->getConnection();
      /**
      * save detail
      */
      $detail = [
      'title' => $object->getTitle(),
      'detail' => $object->getDetail(),
      'nickname' => $object->getNickname(),
      'refer_field' => $object->getReferField(),
      ];
      .
      .
      .


      Just overwrite the function "_afterSave" not the whole file.






      share|improve this answer




























        1














        You need to overwrite the ResourceModel. At 4 am in morning I am able to figure it now :-)



        Add the below XML:



        <preference for="MagentoReviewModelResourceModelReview" type="NSModuleModelResourceModelReview" />


        and add the following php code:



        <?php

        namespace NSModuleModelResourceModel;

        use MagentoFrameworkModelAbstractModel;

        /**
        * Review resource model
        */
        class Review extends MagentoReviewModelResourceModelReview
        {
        /**
        * Perform actions after object save
        *
        * @param MagentoFrameworkModelAbstractModel $object
        * @return $this
        */
        protected function _afterSave(AbstractModel $object)
        {
        $connection = $this->getConnection();
        /**
        * save detail
        */
        $detail = [
        'title' => $object->getTitle(),
        'detail' => $object->getDetail(),
        'nickname' => $object->getNickname(),
        'refer_field' => $object->getReferField(),
        ];
        .
        .
        .


        Just overwrite the function "_afterSave" not the whole file.






        share|improve this answer


























          1












          1








          1







          You need to overwrite the ResourceModel. At 4 am in morning I am able to figure it now :-)



          Add the below XML:



          <preference for="MagentoReviewModelResourceModelReview" type="NSModuleModelResourceModelReview" />


          and add the following php code:



          <?php

          namespace NSModuleModelResourceModel;

          use MagentoFrameworkModelAbstractModel;

          /**
          * Review resource model
          */
          class Review extends MagentoReviewModelResourceModelReview
          {
          /**
          * Perform actions after object save
          *
          * @param MagentoFrameworkModelAbstractModel $object
          * @return $this
          */
          protected function _afterSave(AbstractModel $object)
          {
          $connection = $this->getConnection();
          /**
          * save detail
          */
          $detail = [
          'title' => $object->getTitle(),
          'detail' => $object->getDetail(),
          'nickname' => $object->getNickname(),
          'refer_field' => $object->getReferField(),
          ];
          .
          .
          .


          Just overwrite the function "_afterSave" not the whole file.






          share|improve this answer













          You need to overwrite the ResourceModel. At 4 am in morning I am able to figure it now :-)



          Add the below XML:



          <preference for="MagentoReviewModelResourceModelReview" type="NSModuleModelResourceModelReview" />


          and add the following php code:



          <?php

          namespace NSModuleModelResourceModel;

          use MagentoFrameworkModelAbstractModel;

          /**
          * Review resource model
          */
          class Review extends MagentoReviewModelResourceModelReview
          {
          /**
          * Perform actions after object save
          *
          * @param MagentoFrameworkModelAbstractModel $object
          * @return $this
          */
          protected function _afterSave(AbstractModel $object)
          {
          $connection = $this->getConnection();
          /**
          * save detail
          */
          $detail = [
          'title' => $object->getTitle(),
          'detail' => $object->getDetail(),
          'nickname' => $object->getNickname(),
          'refer_field' => $object->getReferField(),
          ];
          .
          .
          .


          Just overwrite the function "_afterSave" not the whole file.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 18 '17 at 19:53









          Arif AhmadArif Ahmad

          362




          362

























              0














              First thing, you add new column to table so you should create file InstallSchema.php, not InstallData.php in your description.



              And some code is unused, I think the code below is enough.



              namespace NamespaceMymoduleSetup;

              use MagentoFrameworkSetupInstallDataInterface;
              use MagentoFrameworkSetupModuleContextInterface;
              use MagentoFrameworkSetupModuleDataSetupInterface;
              Class InstallSchema implements InstallSchemaInterface
              {
              public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
              {
              $setup->startSetup();

              $reviewTable = 'review_detail';

              $setup->getConnection()
              ->addColumn(
              $setup->getTable($reviewTable),
              'refer_field',
              [
              'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
              'nullable' => true,
              'comment' =>'Would you recommend this product to a friend'
              ]
              );
              $setup->endSetup();
              }
              }




              share




























                0














                First thing, you add new column to table so you should create file InstallSchema.php, not InstallData.php in your description.



                And some code is unused, I think the code below is enough.



                namespace NamespaceMymoduleSetup;

                use MagentoFrameworkSetupInstallDataInterface;
                use MagentoFrameworkSetupModuleContextInterface;
                use MagentoFrameworkSetupModuleDataSetupInterface;
                Class InstallSchema implements InstallSchemaInterface
                {
                public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
                {
                $setup->startSetup();

                $reviewTable = 'review_detail';

                $setup->getConnection()
                ->addColumn(
                $setup->getTable($reviewTable),
                'refer_field',
                [
                'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
                'nullable' => true,
                'comment' =>'Would you recommend this product to a friend'
                ]
                );
                $setup->endSetup();
                }
                }




                share


























                  0












                  0








                  0







                  First thing, you add new column to table so you should create file InstallSchema.php, not InstallData.php in your description.



                  And some code is unused, I think the code below is enough.



                  namespace NamespaceMymoduleSetup;

                  use MagentoFrameworkSetupInstallDataInterface;
                  use MagentoFrameworkSetupModuleContextInterface;
                  use MagentoFrameworkSetupModuleDataSetupInterface;
                  Class InstallSchema implements InstallSchemaInterface
                  {
                  public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
                  {
                  $setup->startSetup();

                  $reviewTable = 'review_detail';

                  $setup->getConnection()
                  ->addColumn(
                  $setup->getTable($reviewTable),
                  'refer_field',
                  [
                  'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
                  'nullable' => true,
                  'comment' =>'Would you recommend this product to a friend'
                  ]
                  );
                  $setup->endSetup();
                  }
                  }




                  share













                  First thing, you add new column to table so you should create file InstallSchema.php, not InstallData.php in your description.



                  And some code is unused, I think the code below is enough.



                  namespace NamespaceMymoduleSetup;

                  use MagentoFrameworkSetupInstallDataInterface;
                  use MagentoFrameworkSetupModuleContextInterface;
                  use MagentoFrameworkSetupModuleDataSetupInterface;
                  Class InstallSchema implements InstallSchemaInterface
                  {
                  public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
                  {
                  $setup->startSetup();

                  $reviewTable = 'review_detail';

                  $setup->getConnection()
                  ->addColumn(
                  $setup->getTable($reviewTable),
                  'refer_field',
                  [
                  'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
                  'nullable' => true,
                  'comment' =>'Would you recommend this product to a friend'
                  ]
                  );
                  $setup->endSetup();
                  }
                  }





                  share











                  share


                  share










                  answered 3 mins ago









                  Tuan NgoTuan Ngo

                  63




                  63






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Magento Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f169643%2fmagento2-add-custom-field-to-review-form%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Polycentropodidae

                      Magento 2 Error message: Invalid state change requested

                      Paulmy