1

How do I override “Mage_Customer_Model_Group” in Magento 1.4.1.1 ?

I've made a new module named Lpf_ModulePendingApproval. This module contains a class named "Lpf_ModulePendingApproval_Model_Group". This class only defines a new customer group. I heard that modifying app/code/core/Mage/Customer/Model/Group.php is not the right way...

I'd like new modules being able to use this new class.

Could you tell me how I should configure Lpf_ModulePendingApproval.xml, Group.php and config.xml.

There are my files.

Group.php

class Lpf_ModulePendingApproval_Model_Group extends Mage_Customer_Model_Group
{
    const PENDIND_APPROVAL_ID       = 4;
}

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Lpf_ModulePendingApproval>
            <version>0.2.0</version>
        </Lpf_ModulePendingApproval>
    </modules>
    <global>
        <models>
            <customer>
                <rewrite>
                    <session>Lpf_ModulePendingApproval_Model_Group</session>
                </rewrite>
            </customer>
        </models>
    </global>
</config>

Lpf_ModulePendingApproval.xml

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

AccountController.php

<?php

//include_once "Mage/Customer/controllers/AccountController.php";
require_once(Mage::getModuleDir('controllers','Mage_Customer').DS.'AccountController.php');
class Lpf_ModuleUrlBackAfterLogin_AccountController extends Mage_Customer_AccountController
{   
    public function indexAction()
    {
        //Mage::log(__METHOD__ . 'surcharge urlBeforeLoginSuccess ');
        parent::indexAction();
    }

    /**
     * Customer logout action
     */
    public function logoutPendingApprovalAction()
    {
        $this->_getSession()->logout()->setBeforeAuthUrl(Mage::getUrl());
    }

    # surcharge de _loginPostRedirect
    /**
     * Define target URL and redirect customer after logging in
     */
    protected function _loginPostRedirect()
    {
        $session = Mage::getSingleton("core/session");
        $urlBeforeLoginSuccess = $session->getData("urlBeforeLoginSuccess");
        $session = $this->_getSession();

        if (!$session->getBeforeAuthUrl() || $session->getBeforeAuthUrl() == Mage::getBaseUrl() ) {

            // Set default URL to redirect customer to
            $session->setBeforeAuthUrl(Mage::helper('customer')->getAccountUrl());

            // Redirect customer to the last page visited after logging in
            if ($session->isLoggedIn())
            {
                if (!Mage::getStoreConfigFlag('customer/startup/redirect_dashboard')) {
                    if ($referer = $this->getRequest()->getParam(Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME)) {
                        $referer = Mage::helper('core')->urlDecode($referer);
                        if ($this->_isUrlInternal($referer)) {
                            $session->setBeforeAuthUrl($referer);
                        }
                    }
                }
                else if ($session->getAfterAuthUrl()) {
                    $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
                }
            } else {
                $session->setBeforeAuthUrl(Mage::helper('customer')->getLoginUrl());
            }
        } else if ($session->getBeforeAuthUrl() == Mage::helper('customer')->getLogoutUrl()) {
            $session->setBeforeAuthUrl(Mage::helper('customer')->getDashboardUrl());
        }
        else {
            if (!$session->getAfterAuthUrl()) {
                //Removed $session->setAfterAuthUrl($session->getBeforeAuthUrl());
                $session->setAfterAuthUrl($urlBeforeLoginSuccess);
            }
            if ($session->isLoggedIn()) {
                $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
            }
        }

        $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
        if($groupId==Mage_Customer_Model_Group::PENDIND_APPROVAL_ID)
        /*{ 
            // Pending Approval Process
            $this->logoutPendingApprovalAction();
            $this->_redirectUrl(Mage::helper('customer')->getLoginUrl());
             Mage::getSingleton('customer/session')->addNotice(Mage::helper('core')->__('Your account needs to be approved by our team...'));
        }
        else
        {*/
            $this->_redirectUrl($session->getBeforeAuthUrl(true));
        //}
    }
}
1

Magento has a factory system:

Mage::getModel('beforeSlash/afterSlash'), where beforeSlash is called namespace or module and afterSlash class.

What you want to do is:

<global>
    <models>
        <beforeSlash>
            <rewrite>
                <afterSlash>Lpf_ModulePendingApproval_Model_Group</afterSlash>
            </rewrite>
        </beforeSlash>
    </models>
</global>

So in your case:

<global>
    <models>
        <customer>
            <rewrite>
                <group>Lpf_ModulePendingApproval_Model_Group</group>
            </rewrite>
        </customer>
    </models>
</global>

Should work.

  • Thanks for your help, but it seems not to be working. The new constant is not found, so kraches the website. This might come from my configuration files. – jolyqr Jun 3 '15 at 11:50
  • please update your question and add new informations. what do you mean by crash? if you don't have more informations, please read magento.stackexchange.com/questions/428/… – Fabian Blechschmidt Jun 3 '15 at 11:55
  • I'm using the new constante in a class nammed "AccountController" located in a new module nammed "Lpf_ModuleUrlBackAfterLogin" I had created. The wesite crashes on line 67 ===> if($groupId==Mage_Customer_Model_Group::PENDIND_APPROVAL_ID) <== from "AccountController.php". When I comment that line, the wesite doesn't crash. – jolyqr Jun 4 '15 at 18:53

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.