0

我的模块中有一个在 Magento 2.3 中工作的插件,但是在升级到 2.4 后我收到了错误

TypeError:参数 2 传递给 ..\Plugin\Controller\Checkout\Sidebar\RemoveItemPlugin::afterExecute() 必须是 Magento\Framework\App\Response\Http 的实例,Magento\Framework\Controller\Result\Json\ 的实例给定的拦截器

这是代码:

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

我尝试了这个问题的答案:

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

但是一旦我将 $result 更改为 resultInterface 类型,我就不能再使用 $result->getContent() 来返回购物车 json。有没有办法用 resultInterface 做到这一点?

4

0 回答 0