Magento2 : Overriding Model file not working
-
21-01-2021 - |
سؤال
I want to override Magento\Payment\Model\Info::setAdditionalInformation()
function my code is as below
<preference for="Magento\Payment\Model\Info" type="Xxx\Yyy\Model\Payment\Info" />
and file is app\code\Xxx\Yyy\Model\Payment\Info.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Xxx\Yyy\Model\Payment;
/**
* Payment information model
*
* @api
* @since 100.0.2
*/
class Info extends \Magento\Payment\Model\Info
{
/**
* Additional information container
*
* @var array
*/
protected $_additionalInformation = [];
/**
* @param \Magento\Framework\Model\Context $context
* @param array $data
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
\Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
\Magento\Payment\Helper\Data $paymentData,
\Magento\Framework\Encryption\EncryptorInterface $encryptor,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct(
$context,
$registry,
$extensionFactory,
$customAttributeFactory,
$paymentData,
$encryptor,
$resource,
$$resourceCollection,
$data
);
}
public function setAdditionalInformation($key, $value = null)
{
if (is_object($value)) {
throw new \Magento\Framework\Exception\LocalizedException(__('The payment disallows storing object XXXXXXXXXXXX'));
}
$this->_initAdditionalInformation();
if (is_array($key) && $value === null) {
$this->_additionalInformation = $key;
} else {
$this->_additionalInformation[$key] = $value;
}
return $this->setData('additional_information', $this->_additionalInformation);
}
}
When I compiled not getting any error,but unfortunately I cant override this function.
EDIT
my di.xml file code
<?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\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Advanced" type="Tecksky\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Advanced" />
<preference for="Magento\Catalog\Helper\Product\Compare" type="Xxx\Yyy\Helper\Product\Compare" />
<preference for="Magento\Catalog\CustomerData\CompareProducts" type="Xxx\Yyy\CustomerData\CompareProducts" />
<preference for="Magento\Customer\Block\Account\Customer" type="Xxx\Yyy\Block\Account\Customer" />
<preference for="Magento\CatalogSearch\Controller\Advanced\Index" type="Xxx\Yyy\Controller\CatalogSearch" />
<preference for="Magento\Search\Controller\Term\Popular" type="Xxx\Yyy\Controller\Searchterm" />
<preference for="Magento\Sales\Controller\Guest\Form" type="Xxx\Yyy\Controller\Guestform" />
<preference for="Magento\Payment\Model\Info" type="Xxx\Yyy\Model\Payment\Info" />
<type name="Magento\Framework\View\Result\Page">
<plugin name="addBodyclass" type="Xxx\Yyy\Plugin\Result\Page"/>
</type>
<type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
<plugin name="ts_checkout_layout_processor" type="Xxx\Yyy\Plugin\Block\LayoutProcessor" sortOrder="1"/>
</type>
<type name="Magento\Framework\Api\DataObjectHelper">
<plugin name="checkout_cart_total_ajax_error" type="Xxx\Yyy\Plugin\Framework\Api\DataObjectHelper" sortOrder="100" disabled="false"/>
</type>
</config>
المحلول
In your modules di.xml file you will have to declare the tag preference to override a file:
<preference for="Magento\Payment\Model\Info" type="Xxx\Yyy\Model\Payment\Info" />
You can override the file depending on the area or for both areas i.e adminhtml and frontend.
نصائح أخرى
please try replacing your constructor with below code:
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
\Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
\Magento\Payment\Helper\Data $paymentData,
\Magento\Framework\Encryption\EncryptorInterface $encryptor,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct(
$context,
$registry,
$extensionFactory,
$customAttributeFactory,
$paymentData,
$encryptor
);
}
IF it not work please let me know which error are you facing?
لا تنتمي إلى magento.stackexchange