2 years ago

#14218

test-img

gazZz

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

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 :

https://magento.stackexchange.com/questions/351344/magento-2-4-sidebar-removeitem-afterexecute-must-be-an-instance-of-magento-f

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?

magento2

magento2.4

0 Answers

Your Answer

Accepted video resources