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;

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

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.