【问题标题】:Magento 2 module to extend Customer issueMagento 2 模块扩展客户问题
【发布时间】:2015-11-30 19:52:50
【问题描述】:

我正在尝试创建一个 Magento 2 模块来扩展默认的 Customer::loadByEmail() 类和方法,并添加一些额外的逻辑。

由于我的模块包含几个不同的类/文件,我创建了一个包含代码的公共要点,而不是用大量代码污染这篇文章。

完整代码:https://gist.github.com/JasonMortonNZ/90ada76ad5511a37d2c6

所有代码都位于文件夹project-root/app/code/Jason/OCUsers 中以供参考。

什么工作:

  • 当我从命令行运行 magento module:status 命令时,我的 Magento 正在识别该模块。
  • 我可以成功启用和禁用该模块,尽管迁移(架构升级)似乎没有运行。

什么有效:

  • 安装和升级的架构更新似乎不起作用。没有数据库架构更新持续存在或生效。
  • 看起来 DI 不正确,因为我创建的新 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();
    }
}

【问题讨论】:

    标签: php magento magento2 magento-2.0


    【解决方案1】:

    您的di.xml 似乎不正确。请参阅 preference 标签上的 for 属性。见下文,我已将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>
    

    【讨论】:

    • 这似乎也不起作用。 loadByEmail() 方法仍在 Magento\Customer\Model\Customer 上调用,而不是我的班级? @georgeq
    猜你喜欢
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    相关资源
    最近更新 更多