我正在尝试创建一个Magento 2模块,以使用一些额外的逻辑来扩展默认的Customer::loadByEmail()
类和方法。
由于我的模块包含一些不同的类/文件,我创建了一个包含代码的公开要点,而不是用大量代码污染这篇文章。
完整代码:https://gist.github.com/JasonMortonNZ/90ada76ad5511a37d2c6
另外,作为参考,所有代码都位于project-root/app/code/Jason/OCUsers
文件夹中。
什么在起作用:
magento module:status
命令时,模块被识别为我的Magento。什么是不工作:
Customer
类和loadByEmail
方法没有被击中。对于为什么会出现这两个问题的任何帮助或建议都将非常感激:)
module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Jason_OCUser" setup_version="2.0.0"/>
<sequence>
<module name="Magento_Customer"/>
</sequence>
</config>
di.xml
<?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\Customer\Api\Data\CustomerInterface" type="Jason\OCUser\Model\Customer" />
<type name="Magento\Framework\Model\ActionValidator\RemoveAction">
<arguments>
<argument name="protectedModels" xsi:type="array">
<item name="customer" xsi:type="string">Jason\OCUser\Model\Customer</item>
</argument>
</arguments>
</type>
</config>
Customer.php
<?php
namespace Jason\OCUser\Model;
use Magento\Customer\Model\Customer as MCustomer;
class Customer extends MCustomer
{
/**
* Load customer by email
*
* @param string $customerEmail
* @return $this
*/
public function loadByEmail($customerEmail)
{
die('Not reaching this :( ');
}
}
InstallSchema.php
<?php
namespace Jason\OCUser\Setup;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\InstallSchemaInterface;
class InstallSchema implements InstallSchemaInterface
{
/**
* Installs DB schema for a module
*
* @param SchemaSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
/**
* Add salt column and oc user status
*/
$installer->getConnection()->addColumn(
'customer_entity',
'oc_salt',
[
'type' => Table::TYPE_TEXT,
'nullable' => true,
'default' => null,
'length' => 9,
'comment' => ''
]
);
$installer->getConnection()->addColumn(
'customer_entity',
'oc_user',
[
'type' => Table::TYPE_BOOLEAN,
'nullable' => false,
'default' => 0,
'comment' => ''
]
);
$installer->endSetup();
}
}
答案 0 :(得分:0)
您的di.xml
似乎不正确。请参阅for
标记上的preference
属性。请参阅下文,我已将Magento\Customer\Api\Data\CustomerInterface
替换为Magento\Customer\Model\Customer
。
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Model\Customer" type="Jason\OCUser\Model\Customer" />
<type name="Magento\Framework\Model\ActionValidator\RemoveAction">
<arguments>
<argument name="protectedModels" xsi:type="array">
<item name="customer" xsi:type="string">Jason\OCUser\Model\Customer</item>
</argument>
</arguments>
</type>
</config>