Skip to content
Advertisement

Overriding Magento_SalesRule ResourceModel by preferences throws error: Type Error occurred when creating object

Error Message in debug.log:

[2020-11-10 07:57:17] main.CRITICAL: Type Error occurred when creating object: MagentoSalesRuleModelResourceModelRule, Argument 3 passed to MagentoSalesRuleModelResourceModelRule::__construct() must be an instance of MagentoSalesRuleModelResourceModelCoupon, instance of RMSameCouponMultipleDiscountsModelResourceModelCoupon given, called in /home/rehan/lab/testrm/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 116 [] []

I am trying to override: MagentoSalesRuleModelResourceModelCoupon.php to remove ‘unique coupon’ restriction:

$this->addUniqueField(['field' => 'code', 'title' => __('Coupon with the same code')]);

from construct() method.

My app/code/Vendor/Module/Model/ResourceModel/Coupon.php looks like:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace VendorModuleModelResourceModel;

use MagentoFrameworkModelAbstractModel;

/**
 * SalesRule Resource Coupon
 *
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Coupon extends MagentoFrameworkModelResourceModelDbAbstractDb implements
    MagentoSalesRuleModelSpiCouponResourceInterface
{
    /**
     * Constructor adds unique fields
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('salesrule_coupon', 'coupon_id');
    }
}

my di.xml file looks like:

<?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="MagentoSalesRuleModelResourceModelCoupon" type="VendorModuleModelResourceModelCoupon" />
</config>

I’ve tried clearing cache, generated folder and did setup:di:compile but I am still receiving the same error.

Advertisement

Answer

You should make your overridden class extends the original class so the other native classes that dependency injection original class can recognize your overridden one.

Alter

class Coupon extends MagentoFrameworkModelResourceModelDbAbstractDb implements
    MagentoSalesRuleModelSpiCouponResourceInterface

into

class Coupon extends MagentoSalesRuleModelResourceModelCoupon

But using preferences aren’t recommended, you can try to find a better way.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement