Hi I am creating an extension which needs to override magento store model (\Magento\Store\Model\Store). For this I have used di.xml in etc folder. I'm getting error when I call my custom helper in overriding model. This is the error.
---
Recoverable Error: Argument 1 passed to Abc\Xyz\Model\Store::__construct() must be an instance of Abc\Xyz\Helper\Data, instance of Magento\Framework\Model\Context given, called in /home/zento/development/magento2/var/generation/Abc/Xyz/Model/Store/Interceptor.php on line 14 and defined in /home/zento/development/magento2/app/code/Abc/Xyz/Model/Store.php on line 14
My di.xml file in etc folder
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Store\Model\Store" type="Abc\Xyz\Model\Store" />
</config>My overriding model
namespace Abc\Xyz\Model;
class Store extends \Magento\Store\Model\Store
{
protected $_helper;
public function __construct(\Abc\Xyz\Helper\Data $moduleHelper) {
$this->_helper = $moduleHelper;
}
public function getDefaultCurrencyCode()
{
if ($this->_helper->isEnabled()) {
$result = $this->getConfig(Currency::XML_PATH_CURRENCY_DEFAULT);
return $this->getCurrencyCodeByIp($result);
} else {
return parent::getDefaultCurrencyCode();
}
}
}and it is my helper
namespace Abc\Xyz\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
protected $_scopeConfig;
protected $_dir;
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\Filesystem\DirectoryList $dir,
array $data = []
)
{
$this->_scopeConfig = $scopeConfig;
$this->_dir = $dir;
}
public function isEnabled()
{
return $this->_scopeConfig->getValue('xxx/xxx/enable_disable', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
}
}I need help to resolve this isue, I want to call my helper functions in my overriding store model class.
I am very thankful for any assistance.
I recommend not override the standard classes at all, because it will break other extensions which override the same classes or which depend on a standard class impementation.
Use plugins instead: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html
Hi,
I could not understand what you have suggested because I am already making a separate extension which is being used to override the core store model.
Do you suggest that I should not mention in di.xml file that I am overriding a store model & I should not extend my custom model from \Magento\Store\Model\Store
Would you please explain little bit how I should manage that MAGENTO call my custom module function getDefaultCurrencyCode
instead to its core function.
Thanks for you help
Your method will have one of the names:
beforeGetDefaultCurrencyCode
afterGetDefaultCurrencyCode
aroundGetDefaultCurrencyCode
See the documentation about "before-", "after-", and "around-" plugins: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html