1

I get this error:

TypeError: Argument 2 passed to ..\Plugin\Controller\Checkout\Sidebar\RemoveItemPlugin::afterExecute() must be an instance of Magento\Framework\App\Response\Http, instance of Magento\Framework\Controller\Result\Json\Interceptor given

It was working fine on Magento 2.3 but after the upgrade to 2.4, I get the above error. My code:

<?php
namespace Vendor\Vendor\Plugin\Controller\Checkout\Sidebar;

class RemoveItemPlugin
{
    public function afterExecute(
        \Magento\Checkout\Controller\Sidebar\RemoveItem $subject,
        \Magento\Framework\App\Response\Http $result
    ): \Magento\Framework\App\Response\Http {
        /* Do something */
    }
}

1 Answer 1

2

In scope of implementation Decomposition of Magento Controllers this controller refactored to returning instance of \Magento\Framework\Controller\Result\Json (which implements \Magento\Framework\Controller\ResultInterface).

Previously this controller used \Magento\Framework\App\Response\Http as return type of execute method.

Anyway, both types are correct according phpdoc of \Magento\Framework\App\ActionInterface::execute method.

/**
 * Execute action based on request and return result
 *
 * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
 * @throws \Magento\Framework\Exception\NotFoundException
 */
public function execute();

To fix this issue for 2.4 version, you should change type of $result argument to \Magento\Framework\Controller\ResultInterface. To have a fix, which will be compatible with two versions of magento (2.3 and 2.4), just skip defining type from method signature and check type of result using instance of inside of you plugin, if this is required for your case.

2
  • I also had this issue but wasn't able to return the cart like it did in the previous class. The issue is $content = $this->json->unserialize($result->getContent()); where getContent no longer exists in $result as its now ResultInterface. Do you know how to render the cart JSON from ResultInterface?
    – gazZz
    Jan 5, 2022 at 9:19
  • I also had this issue but wasn't able to return the cart like it did in the previous class. The issue is $content = $this->json->unserialize($result->getContent()); where getContent no longer exists in $result as its now ResultInterface. Do you know how to render the cart JSON from ResultInterface?
    – gazZz
    Jan 5, 2022 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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