Nous avons comme but de créer un module "ProductComment", c'est à dire un champs où le client peux laisser un message/commentaire sur le produit.
En clair : Le client laisse un message dans le champs, ajoute le produit au panier et retourne sur le produit avec normalement en base, dans notre table dédié aux commentaires.
Nous n’arrivons pas à récupérer le commentaire saisi dans le champ et l'ajouter en base.
Merci d'avance.
config.xml :
<loops>
<loop name="product.comment.comment" class="ProductComment\Loop\ProductCommentLoop"/>
<loop name="product.comment.session.comment" class="ProductComment\Loop\SessionProductCommentLoop"/>
</loops>
<forms>
<form name="product.comment.form" class="ProductComment\Form\ProductCommentForm" />
</forms>
<services>
<service id="product.comment.order.info" class="ProductComment\EventListeners\ProductCommentInfo" scope="request">
<argument type="service" id="request" />
<tag name="kernel.event_subscriber" />
</service>
</services>
<hooks>
<hook id="product.comment.hook.front" class="ProductComment\Hook\FrontHook" scope="request">
<tag name="hook.event_listener" type="front" event="product.details-bottom" method="onProducDetailstBottom"/>
<tag name="hook.event_listener" type="front" event="product.stylesheet" method="onProductIncludeCss"/>
<tag name="hook.event_listener" type="front" event="product.after-javascript-include" method="onProductIncludeJs"/>
</hook>
</hooks>
Notre event listener :
class ProductCommentInfo implements EventSubscriberInterface
{
protected $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public static function getSubscribedEvents()
{
return [TheliaEvents::CART_ITEM_UPDATE_BEFORE => 'saveComment'];
}
public function saveComment(CartEvent $cartEvent)
{
$comment = $this->request->getSession()->get('product-comment');
$cart = $cartEvent->getCart();
$cartId = $cart->getId();
if ($cartId != null && $comment != null) {
$productComment = new ProductComment();
$productComment->setOrderProductId($cartId);
$productComment->setComment($comment);
$productComment->save();
$this->request->getSession()->set('product-comment', '');
}
}
}
Notre controler :
class ProductCommentController extends BaseFrontController
{
public function setComment()
{
$message = false;
$commentForm = new ProductCommentForm($this->getRequest());
try {
$form = $this->validateForm($commentForm);
$data = $form->getData($form);
$comment = $data['comment'];;
if ($comment != null) {
return $this->getRequest()->getSession()->set('product-comment', $comment);
}
return $this->generateRedirectFromRoute("product.view");
} catch (FormValidationException $e) {
$message = Translator::getInstance()->trans("Please check your input: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN);
} catch (PropelException $e) {
$this->getParserContext()->setGeneralError($e->getMessage());
} catch (\Exception $e) {
$message = Translator::getInstance()->trans("Sorry, an error occurred: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN);
}
if ($message !== false) {
$commentForm->setErrorMessage($message);
return $this->getParserContext()
->addForm($commentForm)
->setGeneralError($message)
;
return $this->generateRedirectFromRoute("product.view");
}
}
}