Magento 2 – Can’t Override Class, Must Be an Instance Of

dimagento2overridespluginpreference

I created a preference override for Magento\Quote\Model\Quote\Item\CartItemPersister in my own module. But I get this error in the browser developer console:

Total: Type Error occurred when creating object:
Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor,
Argument 3 passed to
Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor::__construct()
must be an instance of Magento\Quote\Model\Quote\Item\CartItemPersister

instance of
Company\Configurator\Rewrite\Magento\Quote\Model\Quote\Item\CartItemPersister given

app/code/Company/Configurator/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <preference for="Magento\Quote\Model\Quote\Item\CartItemPersister"
                type="Company\Configurator\Rewrite\Magento\Quote\Model\Quote\Item\CartItemPersister"/>
      ...

It seems like I also have to override Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor and swap to my rewrite class there, but this does not seem to be the official way?

UPDATE: I've completly overriden Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor but now I get

Total: Type Error occurred when creating object:
Magento\Quote\Model\QuoteRepository\LoadHandler\Interceptor, Argument
1 passed to
Magento\Quote\Model\QuoteRepository\LoadHandler\Interceptor::__construct()
must be an instance of
Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor,
instance of
Company\Configurator\Rewrite\Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor
given, called in
/var/www/company/m2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php
on line 121

Whats the best way to solve this?

Best Answer

I was able to solve it with a plugin instead of an override

Related Topic