JNDPNT
JNDPNT

Reputation: 7465

Override Magento Controller_Abstract

We have modified Magento's history.phtml to show all orders, without any limitation for a restricted website. This is working, but we ran into a problem when we are trying to print or view an order. I've debugged and the problem lays with Magento's _canViewOrder (located in Mage_Sales_Controller_Abstract). In that function, an extra and ofcourse logical check is implemented to check if the user that is logged in can view/print the order that is requested:

if ($order->getId() && $order->getCustomerId() && ($order->getCustomerId() == $customerId)
            && in_array($order->getState(), $availableStates, $strict = true)

As we have some custom development here, we need to override this method to remove the restriction. Unfortunately We can't find anywhere how to override a Controller class.

Please note we do know how to override routers/controllers, but this one is located in the Controller folder and is an abstract class.

Can anyone point me in the right direction or provide me with a sample config.xml to override this class?

Upvotes: 3

Views: 1578

Answers (2)

JNDPNT
JNDPNT

Reputation: 7465

I've decided to use the quick way after all. I've overridden the Mage_Sales_OrderController:

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Sales') . DS . 'OrderController.php';
class MyCompany_MyModule_OrderController extends Mage_Sales_OrderController
{   
    /**
     * Check order view availability
     * Overridden from Mage_Sales_Controller_Abstract to
     * remove the customer restriction. We want to show all
     * orders to all customers on any time. 
     * 
     * TODO: Maybe in the future add a customer group restriction
     *
     * @param   Mage_Sales_Model_Order $order
     * @return  bool
     */
    protected function _canViewOrder($order)
    {
        $availableStates = Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates();
        if ($order->getId() && in_array($order->getState(), $availableStates, $strict = true)) {
            return true;
        }

        return false;
    }
}

In my config.xml I'm telling Magento to use my controller first and use the OrderController as back-up. As I've only overridden one function, all the existing code will not be touched.

<?xml version="1.0"?>    
...

<frontend>
...
    <routers>
...
        <sales>
            <args>
                <modules>
                    <MyCompany_MyModule before="Mage_Sales_OrderController">MyCompany_MyModule_Sales</MyCompany_MyModule>
                </modules>
            </args>
        </sales>
    </routers>
...
</frontend>
...

Upvotes: 3

james
james

Reputation: 792

You can override any class by placing it in app/code/local. When loading a class, Magento looks first in local, then community, then core. To achieve what you want, copy the file to app/code/local/Mage/Sales/Controller/Abstract.php and make your amendments.

However, try and avoid this approach where possible as it can make things a little messy when it comes to upgrades. One option you might want to consider is modifying the controller that is extending Mage_Sales_Controller_Abstract, and overriding the _canViewOrder method of it. See here: http://drupal.org/project/magento

Upvotes: 2

Related Questions