0

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);
                    }
                });

2 Answers 2

2

Pass data with json format and replace your success ajax console :

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();
    return $resultJson->setData(['productId' => $productId]);
}

Ajax :

var pdtId = $(this).attr('data-pdtId');
$.ajax({
    type: "POST",
    url: customRemoveurl,
    data: {
        productId: pdtId,
    },
    success: function(response) {
        console.log(response.productId);
    },
    error: function(error) {
        // console.log(error);
    }
});
7
  • Thanks for your support , its works for me, nd please check my updated controller please ! Jul 23, 2019 at 5:13
  • what should I need to check in your Updated controller? Jul 23, 2019 at 5:15
  • please check execute() function i need to get the product id and remove product from the logged user wishlist Jul 23, 2019 at 5:17
  • It's not related to this question. I think you should ask separate question for that. Jul 23, 2019 at 5:21
  • For your 2nd question, you can follow this answer. magento.stackexchange.com/a/166263/51810 Jul 23, 2019 at 5:22
0

Do it like this

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()
{
  $resultRedirect = $this->resultRedirectFactory->create();
  $productId = $this->request->getPostValue('productId');

        return $productId;
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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