Question

I have to deal with a third party module that enthusiastically uses preferences for everything and breaks stuff with it.

For example:

<preference for="Magento\Sales\Model\Order" type="Censored\Censored\Model\Order"/>

To reduce the damage, I need to undo some of the preferences. Unfortunately they don't set the preferences for interfaces, but for concrete classes.

I've tried, in an own module:

<preference for="Magento\Sales\Model\Order" type="Magento\Sales\Model\Order"/>

But this results in an "Circular type preference" error. The Magento object manager will try to resolve the type again, find the preference, and so on.

What I actually need is a way to remove that original preference entry, without touching the files of the third party module.

Is this possible somehow?

Was it helpful?

Solution

You can have your own class that extends Order class and does not make any changes. Like this:

<preference for="Censored\Censored\Model\Order" type="Vendor\Module\Model\Order"/>

In your Vendor\Module\Model\Order :

<?php
namespace Vendor\Module\Model;

class Order extends \Magento\Sales\Model\Order
{

    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
        \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Sales\Model\Order\Config $orderConfig,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory $orderItemCollectionFactory,
        \Magento\Catalog\Model\Product\Visibility $productVisibility,
        \Magento\Sales\Api\InvoiceManagementInterface $invoiceManagement,
        \Magento\Directory\Model\CurrencyFactory $currencyFactory,
        \Magento\Eav\Model\Config $eavConfig,
        \Magento\Sales\Model\Order\Status\HistoryFactory $orderHistoryFactory,
        \Magento\Sales\Model\ResourceModel\Order\Address\CollectionFactory $addressCollectionFactory,
        \Magento\Sales\Model\ResourceModel\Order\Payment\CollectionFactory $paymentCollectionFactory,
        \Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory $historyCollectionFactory,
        \Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory $invoiceCollectionFactory,
        \Magento\Sales\Model\ResourceModel\Order\Shipment\CollectionFactory $shipmentCollectionFactory,
        \Magento\Sales\Model\ResourceModel\Order\Creditmemo\CollectionFactory $memoCollectionFactory,
        \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\CollectionFactory $trackCollectionFactory,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $salesOrderCollectionFactory,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productListFactory,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
        array $data = []
    ) {
        parent::__construct($context, $registry, $extensionFactory, $customAttributeFactory, $timezone, $storeManager,
            $orderConfig, $productRepository, $orderItemCollectionFactory, $productVisibility, $invoiceManagement,
            $currencyFactory, $eavConfig, $orderHistoryFactory, $addressCollectionFactory, $paymentCollectionFactory,
            $historyCollectionFactory, $invoiceCollectionFactory, $shipmentCollectionFactory, $memoCollectionFactory,
            $trackCollectionFactory, $salesOrderCollectionFactory, $priceCurrency, $productListFactory, $resource,
            $resourceCollection, $data);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top