I am trying to override Magento\Wishlist\Controller\Index\Add
controller's execute() method to return a JSON response.
I have created a after plugin for it.
Vendor\Company\etc\frontend\di.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Wishlist\Controller\Index\Add">
<plugin name="vendorCompanyExtension" type="Vendor\Company\Plugin\Index\Add" sortOrder="10" disabled="false"/>
</type>
</config>
Vendor\Company\Plugin\Index\Add.php:
<?php
namespace Vendor\Company\Plugin\Index;
class Add
{
/**
* @var \Magento\Framework\Json\Helper\Data
*/
protected $_jsonHelper;
public function __construct(
\Magento\Framework\Json\Helper\Data $jsonHelper
) {
$this->_jsonHelper = $jsonHelper;
}
public function afterExecute(\Magento\Wishlist\Controller\Index\Add $subject, $result)
{
$newResult = [];
$newResult["message"] = "Hello";
$subject->getResponse()->representJson(
$this->_jsonHelper->jsonEncode($newResult)
);
return $result;
}
}
In the original Magento\Wishlist\Controller\Index\Add.php
, there is a code:
$resultRedirect->setPath('*', ['wishlist_id' => $wishlist->getId()]);
return $resultRedirect;
I am trying to return a JSON response instead of redirecting to a Wishlist page after a product is added to the wishlist.
Any help would be appreciated.