0

I have a plugin in my module that was working in Magento 2.3, but after upgrade to 2.4 i am getting the 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

This is the code :

use Magento\Customer\CustomerData\SectionPoolInterface;
use Magento\Framework\Serialize\Serializer\Json;
class RemoveItemPlugin
{
 public function __construct(
 SectionPoolInterface $sectionPool,
 Json $json
 ) {
 $this->sectionPool = $sectionPool;
 $this->json = $json;
 }
 public function afterExecute(
 \Magento\Checkout\Controller\Sidebar\RemoveItem $subject,
 \Magento\Framework\App\Response\Http $result
 ): \Magento\Framework\App\Response\Http {
 /* Get Cart Items */
 $sectionNames = "cart";
 $sectionNames = $sectionNames ? array_unique(\explode(',', $sectionNames)) : null;
 $forceNewSectionTimestamp = false;
 $response = $this->sectionPool->getSectionsData($sectionNames, (bool)$forceNewSectionTimestamp);
 /* Prepare Result */
 $content = $this->json->unserialize($result->getContent());
 $content['cartSection'] = $response;
 $content = $this->json->serialize($content);
 $result->setContent($content);
 return $result;
 }
}

I tried the answer in this question :

Magento 2.4 Sidebar\RemoveItem - afterExecute() must be an instance of Magento\Framework\App\Response\Http

but once I change $result to be type resultInterface, I can no longer use $result->getContent() to return the cart json. Is there a way to do this with resultInterface?

I have tried this also but i get the error invalid return type

 /* Prepare Result */
 $content = (array)$result;
 $content['cartSection'] = $response;
 $content2 = $this->json->serialize($content);
 return $content2;
asked Jan 5, 2022 at 12:43

1 Answer 1

0

Do -

public function afterExecute(
 \Magento\Checkout\Controller\Sidebar\RemoveItem $subject,
 $result
) { 
 // your code
 $result->setData(['key' => 'value']);
 return $result;
}

Whatever you are trying to return just pass as [] with key,value pair

Because - https://github.com/magento/magento2/blob/4c36116dcf878e127059d9be9566a119783583f2/app/code/Magento/Checkout/Controller/Sidebar/RemoveItem.php#L111 and https://github.com/magento/magento2/blob/4c36116dcf878e127059d9be9566a119783583f2/lib/internal/Magento/Framework/Controller/Result/Json.php#L59 will take care of data

answered Jan 6, 2022 at 5:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.