0

I'm trying to make a few amends to the core code, so I copied the relevant classes and placed them in the app/code/local/mage folder, which works a treat. However, as this is bad practice, I'm trying to turn it into a module. I have registered the module in app/etc/modules/Mymodule_Login.xml with the following code:

<?xml version="1.0"?>
<config>
    <modules>
        <Mymodule_Login>
            <active>true</active>
            <codePool>local</codePool>
        </Mymodule_Login>
    </modules>
</config>

I have the following file structure to hold my module

app/code/local/Mymodule/Login/etc/config.xml
app/code/local/Mymodule/Login/Model/Customer.php
app/code/local/Mymodule/Login/Resource/Customer.php

The config looks like:

    <?xml version="1.0"?>
<config>
    <global>
        <models>
            <customer>
                <rewrite>
                    <customer>Mymodule_Login_Model_Customer</customer>
                </rewrite>
            </customer>
        </models>
        <models>
            <customer_resource>
                <rewrite>
                    <customer>Mymodule_Login_Model_Resource_Customer</customer>
                </rewrite>
            </customer_resource>
        </models>
    </global>
</config>

My model looks like:

class Mymodule_Login_Model_Customer extends Mage_Customer_Model_Customer
{
    public function authenticate($login, $password)
    {
        $this->loadByUsername($login);
        if ($this->getConfirmation() && $this->isConfirmationRequired()) {
            throw Mage::exception('Mage_Core', Mage::helper('customer')->__('This account is not confirmed.'),
                self::EXCEPTION_EMAIL_NOT_CONFIRMED
            );
        }
        if (!$this->validatePassword($password)) {
            throw Mage::exception('Mage_Core', Mage::helper('customer')->__('Invalid login or password.'),
                self::EXCEPTION_INVALID_EMAIL_OR_PASSWORD
            );
        }
        Mage::dispatchEvent('customer_customer_authenticated', array(
           'model'    => $this,
           'password' => $password,
        ));

        return true;
    }

    /**
     * Load customer by username
     *
     * @param   string $customerEmail
     * @return  Mage_Customer_Model_Customer
     */
    public function loadByUsername($username)
    {
        $this->_getResource()->loadByUsername($this, $username);
        return $this;
    }
}

My resouce code looks like:

    class Mymodule_Login_Model_Resource_Customer extends Mage_Customer_Model_Resource_Customer
{
    /**
     * Load customer by username
     *
     * @throws Mage_Core_Exception
     *
     * @param Mage_Customer_Model_Customer $customer
     * @param string $username
     * @param bool $testOnly
     * @return Mage_Customer_Model_Resource_Customer
     */
    public function loadByUsername(Mage_Customer_Model_Customer $customer, $username, $testOnly = false)
    {
        $adapter = $this->_getReadAdapter();
        $bind    = array('customer_username' => $username);
        $select  = $adapter->select()
            ->from($this->getEntityTable(), array($this->getEntityIdField()))
            ->where('username = :customer_username');

        if ($customer->getSharingConfig()->isWebsiteScope()) {
            if (!$customer->hasData('website_id')) {
                Mage::throwException(
                    Mage::helper('customer')->__('Customer website ID must be specified when using the website scope')
                );
            }
            $bind['website_id'] = (int)$customer->getWebsiteId();
            $select->where('website_id = :website_id');
        }

        $customerId = $adapter->fetchOne($select, $bind);
        if ($customerId) {
            $this->load($customer, $customerId);
        } else {
            $customer->setData(array());
        }

        return $this;
    }
}

When I try to login with the username, it fails and I get nothing in the error logs. Any help would be greatly appreciated!

JPK
  • 157
  • 6

1 Answers1

1

You are rewriting something that does not exist.
This xml :

    <models>
        <customer_resource>
            <rewrite>
                <login>Mymodule_Login_Model_Resource_Login</login>
            </rewrite>
        </customer_resource>
    </models>

Means that you are rewriting the class Mage_Customer_Model_Resource_Login that does not exist.

and this

    <models>
        <customer>
            <rewrite>
                <login>Mymodule_Login_Model_Login</login>
            </rewrite>
        </customer>
    </models>

means you are rewriting Mage_Customer_Model_Login that also does not exist.
I would take a wild guess and say that you are trying to rewrite: Mage_Customer_Model_Customer and Mage_Customer_Model_Resource_Customer.
For this your config.xml should look like this:

<?xml version="1.0"?>
<config>
    <global>
        <models>
            <customer>
                <rewrite>
                    <customer>Mymodule_Login_Model_Customer</customer>
                </rewrite>
            </customer>
        </models>
        <models>
            <customer_resource>
                <rewrite>
                    <customer>Mymodule_Login_Model_Resource_Customer</customer>
                </rewrite>
            </customer_resource>
        </models>
    </global>
</config>

and your new classes should be placed in

app/code/local/Mymodule/Login/Model/Customer.php
app/code/local/Mymodule/Login/Model/Resource/Customer.php

Also change your class names according to the file names:

Mymodule_Login_Model_Customer
Mymodule_Login_Model_Resource_Customer
Marius
  • 197,939
  • 53
  • 422
  • 830
  • Hi Marius, thanks for your comments, makes more sense now! I am indeed trying to rewrite those classes, I'm basically trying to change the login to use usernames instead of emails. I have copied over all of your suggestions, but it is still producing the same results and not letting me in, there isn't even anything in the logs. – JPK Feb 17 '14 at 14:39
  • @JPK. Make sure your functions are called. Also make sure the logs are enabled. Also this might help with the debugging: http://magento.stackexchange.com/a/429/146 – Marius Feb 17 '14 at 14:44
  • Thanks Marius, I will look into this now. I have updated my code above with your recommendations, will post back shortly. – JPK Feb 17 '14 at 14:48
  • Thanks for the link Marius, very helpful, I can now see that the files are failing to load and i'm getting a php error. It seems Magento is having difficulty finding and opening the file and i'm getting 'No such file or directory' – JPK Feb 17 '14 at 15:09
  • @JPK. I had an error in my answer. The resource model file should be app/code/local/Mymodule/Login/Model/Resource/Customer.php not app/code/local/Mymodule/Login/Resource/Customer.php. I've updated the answer. – Marius Feb 17 '14 at 15:11
  • Ah ha, that was it!! Works like a treat now, many thanks. – JPK Feb 17 '14 at 15:20