TransWikia.com

Request Response JSON in magento 2

Magento Asked on December 22, 2021

I have modified form review as popup modal and have overrides the controller in my custom module, the review saved successfully in DB. I need to pass the response to the to JS in JSON format , Help me how to solve it.
Here I have given my custom controller but it shows error while compiling

Errors during compilation: XXXYYYControllerIndexIndex Missed
required argument coreRegistry in parent::__construct call. File:
/var/www/html/magento220/app/code/XXX/YYY/Controller/Index/Index.php
Total Errors Count: 1

                                          [MagentoFrameworkValidatorException]     Error during compilation

Controller

<?php
namespace XXXYYYControllerIndex;


use MagentoReviewModelReview;
use MagentoFrameworkControllerResultJsonFactory;

class Index extends MagentoReviewControllerProductPost
{

    protected $_resultJsonFactory;

    public function __construct(
    MagentoFrameworkAppActionContext $context, JsonFactory $resultJsonFactory
    )
    {
        $this->_resultJsonFactory = $resultJsonFactory;

        parent::__construct($context);
    }

    public function execute()
    {
        $message = null;
        $status = null;
        if (!$this->formKeyValidator->validate($this->getRequest())) {
            $message[] = "Invalid form key";
            $status = 'failed';
        } else {
            $data = $this->reviewSession->getFormData(true);


            if ($data) {
                $rating = [];
                if (isset($data['ratings']) && is_array($data['ratings'])) {
                    $rating = $data['ratings'];
                }
            } else {
                $data = $this->getRequest()->getPostValue();
                $rating = $this->getRequest()->getParam('ratings', []);
            }
            echo 'sdfsdfsdfsdfsd';
            print_r($data);

            if (($product = $this->initProduct()) && !empty($data)) {
                echo 'InsideLoops';
                /** @var MagentoReviewModelReview $review */
                $review = $this->reviewFactory->create()->setData($data);

                $review->unsetData('review_id');

                $validate = $review->validate();
                if ($validate === true) {
                    try {

                        $review->setEntityId($review->getEntityIdByCode(Review::ENTITY_PRODUCT_CODE))
                                ->setEntityPkValue($product->getId())
                                ->setStatusId(Review::STATUS_PENDING)
                                ->setCustomerId($this->customerSession->getCustomerId())
                                ->setStoreId($this->storeManager->getStore()->getId())
                                ->setStores([$this->storeManager->getStore()->getId()])
                                ->save();


                        foreach ($rating as $ratingId => $optionId) {
                            $this->ratingFactory->create()
                                    ->setRatingId($ratingId)
                                    ->setReviewId($review->getId())
                                    ->setCustomerId($this->customerSession->getCustomerId())
                                    ->addOptionVote($optionId, $product->getId());
                        }
                        print_r($optionId);
                        $review->aggregate();
                        $message[] = __('You submitted your review for moderation.');
                        $status = 'success';
                    } catch (Exception $e) {
                        $this->reviewSession->setFormData($data);
                        $message[] = __('We can't post your review right now.');
                        $status = 'failed';
                    }
                } else {
                    $this->reviewSession->setFormData($data);
                    if (is_array($validate)) {
                        foreach ($validate as $errorMessage) {
                            $message[] = $errorMessage;
                        }
                    } else {
                        $message[] = __('We can't post your review right now.');
                    }
                    $status = 'failed';
                }
            }
        }

        $resultJson = $this->_resultJsonFactory->create()
                ->setData([
            'messages' => $message,
            'status' => $status,
        ]);


        return $resultJson;

    }

One Answer

Use this construct method in your controller.

  public function __construct(
    MagentoFrameworkAppActionContext $context, JsonFactory $resultJsonFactory
    )
    {
        $this->_resultJsonFactory = $resultJsonFactory;

    }

Answered by D Singh on December 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP