官术网_书友最值得收藏!

Displaying comments

The last step will be to display comments filled in by customers on the front office, and we will also use the configuration values we created in the previous chapter. Let's begin by creating a method named assignProductTabContent, where we will retrieve the concerned id_product parameter (which is in the GET value):

$id_product = Tools::getValue('id_product');

Then make the SQL request to retrieve all comments concerning the products of the page you are on:

$comments = Db::getInstance()->executeS('SELECT * FROM '._DB_PREFIX_.'mymod_comment WHERE id_product = '.(int)$id_product);

Note

You should have noticed that I used the _DB_PREFIX_ constant, which corresponds to the prefix you chose when you installed your shop. In most cases, it will be equal to ps_. We didn't use it for the insert method since the insert and update methods automatically add the prefix.

Then retrieve the configuration values and assign all these variables to Smarty. You should end up with something like this:

public function assignProductTabContent()
{
  $enable_grades = Configuration::get('MYMOD_GRADES');
  $enable_comments = Configuration::get('MYMOD_COMMENTS');

  $id_product = Tools::getValue('id_product');
  $comments = Db::getInstance()->executeS('SELECT * FROM '._DB_PREFIX_.'mymod_comment WHERE id_product = '.(int)$id_product);
  $this->context->smarty->assign('enable_grades', $enable_grades);
  $this->context->smarty->assign('enable_comments', $enable_comments);
  $this->context->smarty->assign('comments', $comments);
}
public function hookDisplayProductTabContent($params)
{
  $this->processProductTabContent();
  $this->assignProductTabContent();
  return $this->display(__FILE__, 'displayProductTabContent.tpl');
}

Now, go to your displayProductTabContent.tpl template and add a foreach Smarty in your div markup, just below the h3 title, to display the comments:

<div class="rte">
  {foreach from=$comments item=comment}
    <p>
      <strong>Comment #{$comment.id_mymod_comment}:</strong> {$comment.comment}<br>
      <strong>Grade:</strong> {$comment.grade}/5<br>
    </p><br>
  {/foreach}
</div>

Note

If you're not familiar with the foreach Smarty, please read the official Smarty documentation at http://www.smarty.net/docsv2/en/language.function.foreach.

Now, go to your front office, and look at the result. You should see all the comments you had left about this product:

Even though it needs a lot of improvement (CSS, comment administration, and so on), you now have a fully working comments module now!

Maybe we could make one final upgrade on this chapter. We have assigned configuration values but we still don't use them. So, let's add Smarty conditions to display fields depending on the configuration:

{if $enable_grades eq 1}
  <div class="form-group">
    <label for="grade">Grade:</label>
    <div class="row">
      <div class="col-xs-4">
        <select id="grade" class="form-control" name="grade">
          [...]
        </select>
      </div>
    </div>
  </div>
{/if}
{if $enable_comments eq 1}
  <div class="form-group">
    <label for="comment">Comment:</label>
    <textarea name="comment" id="comment" class="form-control"></textarea>
  </div>
{/if}

And you might also add this condition around the form:

{if $enable_grades eq 1 OR $enable_comments eq 1}
{/if}

This way, if none of the fields are enabled, then the submit button won't appear.

Note

If you uninstall and reinstall your module (to register on a new hook, for example), do not forget to go to your module configuration page to enable the fields.

Congratulations! You have finished the developments on this chapter. The end of this chapter is more for your information, but you should definitely read it before moving on to the next chapter.

主站蜘蛛池模板: 瓦房店市| 隆安县| 山西省| 芮城县| 盈江县| 牡丹江市| 景洪市| 哈密市| 璧山县| 沙雅县| 砀山县| 舟曲县| 金华市| 兖州市| 汽车| 庆元县| 南丰县| 分宜县| 宁波市| 集贤县| 嘉祥县| 宝清县| 张北县| 邹平县| 临邑县| 鹰潭市| 乌审旗| 黄大仙区| 永寿县| 广南县| 常山县| 天峻县| 洞口县| 鄂托克前旗| 东乌珠穆沁旗| 荣成市| 中超| 桂东县| 苍南县| 龙门县| 夏津县|