1

I have created module to post comments. Basically I have added a new tab called Comments to product page view (just like reviews tab). So far I have managed to output comments based on product ID. Now I am creating post controller, it should save the form (name, email, text) and also add to database current product Id and date(created at). How can I achieve this?

This is my post controller:

    <?php

namespace Vendor\ProductComments\Controller\Post;

use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;



class Index extends Action
{
    public function execute()
    {
        $data = $this->getRequest()->getPostValue();
        $item = $this->_objectManager->create("Vendor\ProductComments\Model\Comments");
        $item->setData($data);
        $item->save();
        $redirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $redirect->setUrl($this->_redirect->getRefererUrl());
        return $redirect;
    }
}

And this is my form.phtml:

<form action="<?php echo $this->getUrl("comments/post/index")?>" method="post">


<div class="field comment-field-name required">
    <label for="name_field" class="label"><span>Name</span></label>
    <div class="control">
        <input type="text" class="input" name="name" id="name_field">
    </div>
</div>
<div class="field comment-field-email required">
    <label for="email_field" class="label"><span>Email</span></label>
    <div class="control">
        <input type="email" class="input" name="email" id="email_field">
    </div>
</div>
<div class="field comment-field-text required">
    <label for="comment_field" class="label"><span>Comment</span></label>
    <div class="control">
        <textarea name="comment" id="comment_field" cols="5" rows="3"></textarea>
    </div>
</div>

<div class="primary actions-primary">
    <button type="submit" class="action submit primary"><span>Submit</span></button>
</div>
</form>
6
  • What do you mean by current product and created time? At what page are you implementing this?
    – P S
    Dec 29, 2017 at 14:12
  • I have added 1 more tab Comments to product page (like review, details). Inside that tab, is comment list and form for new comments. Very similar to reviews tab. Dec 29, 2017 at 14:27
  • 1
    Please add this to your question so that it helps others understand.
    – P S
    Dec 29, 2017 at 14:31
  • Ok, I edited it! Dec 29, 2017 at 14:39
  • Do you have column in your table in database to store product_id and date created if not you have to create those.
    – P S
    Dec 29, 2017 at 15:53

1 Answer 1

1

I assume that you have created column to store time and productid in your database and appropriate model for it. Apart from that you may proceed like this.

1. Create Block file

Since you have not shown your block file I assume that it is common file extending \Magento\Framework\View\Element\Template. Inject with it Magento\Catalog\Block\Product\View\Description class.

namespace Vendor\Module\Block\Index;

class YourBlockFile extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        ...
        Magento\Catalog\Block\Product\View\Description $description,
        ...
) 
    {
        ...
        $this->description = $description;
        ...
    }
    public function getProductId()
    {   //This gets us the current product from the registry.
        return $this->description->getProduct()->getData('entity_id');
    }
}

2. Create your form with two more hidden element.

<form action="<?php echo $this->getUrl("comments/post/index")?>" method="post">


<div class="field comment-field-name required">
    <label for="name_field" class="label"><span>Name</span></label>
    <div class="control">
        <input type="text" class="input" name="name" id="name_field">
    </div>
</div>
<div class="field comment-field-email required">
    <label for="email_field" class="label"><span>Email</span></label>
    <div class="control">
        <input type="email" class="input" name="email" id="email_field">
    </div>
</div>
<div class="field comment-field-text required">
    <label for="comment_field" class="label"><span>Comment</span></label>
    <div class="control">
        <textarea name="comment" id="comment_field" cols="5" rows="3"></textarea>
    </div>
</div>
        <input type="hidden" class="input" name="productid" id="productid" value="<?php echo $block->getProductId();?>" >
        <input type="hidden" class="input" name="timestamp" id="timestamp">
<div class="primary actions-primary">
    <button type="submit" class="action submit primary"><span>Submit</span></button>
</div>
</form>

3. Controller: Just change the value of time stamp here.

<?php

namespace Vendor\ProductComments\Controller\Post;

use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;



class Index extends Action
{
    protected $date;
    public function __construct(
        ....
        \Magento\Framework\Stdlib\DateTime\DateTime $date, // you must inject with this class to get correct store time.
        ....
    ) {
        ....
        $this->date = $date;
        ....
    }
    public function execute()
    {
        $timestamp= $this->date->gmtDate();
        $this->getRequest()->setPostValue('timestamp', $timestamp); //since this field is empty initially set timestamp.
        $data = $this->getRequest()->getPostValue();
        $item = $this->_objectManager->create("Vendor\ProductComments\Model\Comments");
        $item->setData($data);
        $item->save();
        $redirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $redirect->setUrl($this->_redirect->getRefererUrl());
        return $redirect;
    }
}

This is how I have done to some similar problem of mine. Hope works for you.

1
  • You are always welcomed.
    – P S
    Dec 30, 2017 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.