1

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?

  • The one time I encountered this I ended up doing what Prachi posted below. Even doing something like ObjectManager->configure() ends up with a circular preference error. So short of intercepting the merging process I believe it's either patching up the original di.xml from the extension or an empty class. – Kristof at Fooman Nov 8 '18 at 21:24
0

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

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.