Exception
Exception #0 (InvalidArgumentException): Invalid return type
#0 /var/www/html/magento/folder/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
#1 /var/www/html/magento/folder/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#2 {main}
Controller
protected $wishlist;
public function __construct(
\Magento\Wishlist\Model\Wishlist $wishlist,
\Magento\Framework\App\Request\Http $request,
\Magento\Customer\Model\Session $customerSession,
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
) {
$this->wishlist = $wishlist;
$this->request = $request;
$this->customerSession = $customerSession;
$this->resultJsonFactory = $resultJsonFactory;
parent::__construct($context);
}
public function execute()
{
$productId = $this->request->getPostValue('productId');
$resultJson = $this->resultJsonFactory->create();
$resultJson->setData(['productId' => $productId]);
$customerId = $this->customerSession->getCustomer()->getId();
$wish = $this->wishlist->loadByCustomerId($customerId);
$items = $wish->getItemCollection();
/** @var \Magento\Wishlist\Model\Item $item */
foreach ($items as $item) {
if ($item->getProductId() == $productId) {
$item->delete();
$wish->save();
}
}
}
}
Ajax
var pdtId = $(this).attr('data-pdtId');
$.ajax({
type: "POST",
url: customRemoveurl,
data: {
productId: pdtId,
},
success: function(response) {
console.log(response);
},
error: function(error) {
// console.log(error);
}
});