2

I try to override the Customer model. I created code/Tc/Name/etc/di.xml, code/Tc/Name/Model/Customer/Customer.php .

In di.xml

<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="Tc\Name\Model\Customer\Customer" />
</config>

In Customer.php

<?php

namespace Tc\Name\Model\Customer;

class Customer extends \Magento\Customer\Model\Customer
{

   public function getName()
    {              
        $name = '';

        if ($this->_config->getAttribute('customer', 'prefix')->getIsVisible() && $this->getPrefix()) {
            $name .= $this->getPrefix() . ' ';
        }
        $name .= ' ' . $this->getLastname();

        if ($this->_config->getAttribute('customer', 'middlename')->getIsVisible() && $this->getMiddlename()) {
            $name .= ' ' . $this->getMiddlename();
        }
        $name .= $this->getFirstname();
        if ($this->_config->getAttribute('customer', 'suffix')->getIsVisible() && $this->getSuffix()) {
            $name .= ' ' . $this->getSuffix();
        }
        return $name;
    }
}

Nothing changed after setup:upgrade and setup:static-content:deploy. I tried to add a code to dump log in getName() but also nothing logged.

  • Have you tried to remove var/generation folder, you need to do that after make change in di.xml file – Quan Le Jun 30 '17 at 3:04
  • Thanks. Just tried to do rm -rf var/di/* var/generation/* var/cache/* var/page_cache/* var/view_preprocessed/*. Then setup:upgrade, setup:di:compile, setup:static-content:deploy. But not working. No error and no different – user631607 Jun 30 '17 at 4:52
  • I also added etc/module.xml and registration.php. The module added success and enable it in admin panel. However, it is still not working. – user631607 Jun 30 '17 at 5:01
  • Where are you check getName() after override model? – Prince Patel Jun 30 '17 at 5:11
  • I use fopen and fwrite in getName() to dump a log file. I also tried to use \Magento\Framework\App\ObjectManager::getInstance()->get('\Psr\Log\LoggerInterface') to dump the debug log file. But seems it was not trigged – user631607 Jun 30 '17 at 6:11
0

I have same problem before, I added __construct and variables, its works fine,

Try this code:

<?php

namespace Tc\Name\Model\Customer;

use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Api\GroupRepositoryInterface;
use Magento\Customer\Model\Config\Share;
use Magento\Customer\Model\ResourceModel\Address\CollectionFactory;
use Magento\Customer\Model\ResourceModel\Customer as ResourceCustomer;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Reflection\DataObjectProcessor;
use Magento\Framework\Exception\EmailNotConfirmedException;
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
use Magento\Framework\Exception\AuthenticationException;
use Magento\Framework\Indexer\StateInterface;
use Magento\Store\Model\ScopeInterface;


class Customer extends \Magento\Customer\Model\Customer
{

 /**
     * Configuration paths for email templates and identities
     */
    const XML_PATH_REGISTER_EMAIL_TEMPLATE = 'customer/create_account/email_template';

    const XML_PATH_REGISTER_EMAIL_IDENTITY = 'customer/create_account/email_identity';

    const XML_PATH_REMIND_EMAIL_TEMPLATE = 'customer/password/remind_email_template';

    const XML_PATH_FORGOT_EMAIL_TEMPLATE = 'customer/password/forgot_email_template';

    const XML_PATH_FORGOT_EMAIL_IDENTITY = 'customer/password/forgot_email_identity';

    const XML_PATH_RESET_PASSWORD_TEMPLATE = 'customer/password/reset_password_template';

    const XML_PATH_IS_CONFIRM = 'customer/create_account/confirm';

    const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'customer/create_account/email_confirmation_template';

    const XML_PATH_CONFIRMED_EMAIL_TEMPLATE = 'customer/create_account/email_confirmed_template';

    const XML_PATH_GENERATE_HUMAN_FRIENDLY_ID = 'customer/create_account/generate_human_friendly_id';

    const SUBSCRIBED_YES = 'yes';

    const SUBSCRIBED_NO = 'no';

    const ENTITY = 'customer';

    const CUSTOMER_GRID_INDEXER_ID = 'customer_grid';

    /**
     * Configuration path to expiration period of reset password link
     */
    const XML_PATH_CUSTOMER_RESET_PASSWORD_LINK_EXPIRATION_PERIOD = 'customer/password/reset_link_expiration_period';

    /**
     * Model event prefix
     *
     * @var string
     */
    protected $_eventPrefix = 'customer';

    /**
     * Name of the event object
     *
     * @var string
     */
    protected $_eventObject = 'customer';

    /**
     * List of errors
     *
     * @var array
     */
    protected $_errors = [];

    /**
     * Assoc array of customer attributes
     *
     * @var array
     */
    protected $_attributes;

    /**
     * Customer addresses collection
     *
     * @var \Magento\Customer\Model\ResourceModel\Address\Collection
     */
    protected $_addressesCollection;

    /**
     * Is model deletable
     *
     * @var boolean
     */
    protected $_isDeleteable = true;

    /**
     * Is model readonly
     *
     * @var boolean
     */
    protected $_isReadonly = false;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @var \Magento\Eav\Model\Config
     */
    protected $_config;

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $_scopeConfig;

    /**
     * @var Share
     */
    protected $_configShare;

    /**
     * @var AddressFactory
     */
    protected $_addressFactory;

    /**
     * @var CollectionFactory
     */
    protected $_addressesFactory;

    /**
     * @var \Magento\Framework\Mail\Template\TransportBuilder
     */
    protected $_transportBuilder;

    /**
     * @var GroupRepositoryInterface
     */
    protected $_groupRepository;

    /**
     * @var \Magento\Framework\Encryption\EncryptorInterface
     */
    protected $_encryptor;

    /**
     * @var \Magento\Framework\Math\Random
     */
    protected $mathRandom;

    /**
     * @var \Magento\Framework\Stdlib\DateTime
     */
    protected $dateTime;

    /**
     * @var CustomerInterfaceFactory
     */
    protected $customerDataFactory;

    /**
     * @var DataObjectProcessor
     */
    protected $dataObjectProcessor;

    /**
     * @var \Magento\Framework\Api\DataObjectHelper
     */
    protected $dataObjectHelper;

    /**
     * @var \Magento\Customer\Api\CustomerMetadataInterface
     */
    protected $metadataService;

    /**
     * @var \Magento\Framework\Indexer\IndexerRegistry
     */
    protected $indexerRegistry;


  public function __construct(
    \Magento\Framework\Model\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Eav\Model\Config $config,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Customer\Model\ResourceModel\Customer $resource,
    \Magento\Customer\Model\Config\Share $configShare,
    \Magento\Customer\Model\AddressFactory $addressFactory,
    \Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressesFactory,
    \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
    GroupRepositoryInterface $groupRepository,
    \Magento\Framework\Encryption\EncryptorInterface $encryptor,
    \Magento\Framework\Stdlib\DateTime $dateTime,
    CustomerInterfaceFactory $customerDataFactory,
    DataObjectProcessor $dataObjectProcessor,
    \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
    \Magento\Customer\Api\CustomerMetadataInterface $metadataService,
    \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
    \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
    array $data = []
    ) {
    $this->metadataService = $metadataService;
    $this->_scopeConfig = $scopeConfig;
    $this->_storeManager = $storeManager;
    $this->_config = $config;
    $this->_configShare = $configShare;
    $this->_addressFactory = $addressFactory;
    $this->_addressesFactory = $addressesFactory;
    $this->_transportBuilder = $transportBuilder;
    $this->dateTime = $dateTime;
    $this->_groupRepository = $groupRepository;
    $this->_encryptor = $encryptor;
    $this->customerDataFactory = $customerDataFactory;
    $this->dataObjectProcessor = $dataObjectProcessor;
    $this->dataObjectHelper = $dataObjectHelper;
    $this->indexerRegistry = $indexerRegistry;
    parent::__construct(
        $context,
        $registry,
        $storeManager,
        $resource,
        $resourceCollection,
        $data
    );
    }

   public function getName()
    {              
    $name = '';

    if ($this->_config->getAttribute('customer', 'prefix')->getIsVisible() && $this->getPrefix()) {
        $name .= $this->getPrefix() . ' ';
    }
    $name .= ' ' . $this->getLastname();

    if ($this->_config->getAttribute('customer', 'middlename')->getIsVisible() && $this->getMiddlename()) {
        $name .= ' ' . $this->getMiddlename();
    }
    $name .= $this->getFirstname();
    if ($this->_config->getAttribute('customer', 'suffix')->getIsVisible() && $this->getSuffix()) {
        $name .= ' ' . $this->getSuffix();
    }
    return $name;
    }
}

Let me know if you have error.

  • Good to see an error. At least I know that this model is called after adding the __construct and variables – user631607 Jun 30 '17 at 9:20
  • I have this error Uncaught TypeError: Argument 3 passed to Magento\\Customer\\Model\\Customer::__construct() must implement interface Magento\\Store\\Model\\StoreManagerInterface, instance of Magento\\Customer\\Model\\ResourceModel\\Customer\\Interceptor given, called in /var/www/html/app/code/Tc/Name/Model/Customer/Customer.php on line 250 and defined in /var/www/html/vendor/magento/module-customer/Model/Customer.php:234 – user631607 Jun 30 '17 at 9:27
  • @now try this let me know you have any error – Rajkumar .E Jun 30 '17 at 9:28
  • Please check the error on the above comment. I have double checked the parameters and implementations which are same as module-customer/Model/Customer.php – user631607 Jun 30 '17 at 9:36
  • you need to change some pareameters form core, Now check it, I have changed argument 3($resource argument need StoreManagerInterface) – Rajkumar .E Jun 30 '17 at 9:47
1

If you want to overwrite only getName() function you can create plugin instead of overwriting the entire customer Model. Any of the public function in model we can create plugin.

http://inchoo.net/magento-2/overriding-classes-magento-2/

  • Thanks. I am not sure the usage of plugin. I think it is adding methods before or after the core methods. For my case, should I add a plugin to rewrite the name after getName()? – user631607 Jun 30 '17 at 9:24
  • @user631607 , could you explain your case. – Iyappan Perumal Jun 30 '17 at 10:25
  • I want to show the name in Lastname Firstname format instead of firstname lastname for Japanese store. – user631607 Jul 1 '17 at 12:02
  • @user631607 you can use after method ( afterGetName ) plugin for your case. devdocs.magento.com/guides/v2.0/extension-dev-guide/… – Iyappan Perumal Jul 1 '17 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.