0

I would like to add a function in Mage_Sales_Helper_Data without editing or copy of the original file. So I try to extend it, but this code doesn't work

I have created a config.xml in this folder app\code\local\ML\etc

<?xml version="1.0"?>

<config>
    <modules>
       <ML_Sales>
            <version>0.0.1</version>
       </ML_Sales>
   </modules>
   <global>
      <helpers>
       <sales>
         <rewrite>
           <data>ML_Sales_Helper_Data</data>
         </rewrite>
        </sales>
      </helpers>
   </global>
</config>

And then in app\code\local\ML\Sales\Helper I have created Data.php

<?php 
require_once('Mage/Sales/Helper/Data.php'); 
class ML_Sales_Helper_Data extends Mage_Core_Helper_Data {
    public function getOrderHistoryUrl()
    {
        return $this-> _getUrl('sales/order/history');

    }  

}
?>

Finally into my app\design\frontend\my_package\default\layout\local.xml I wrote this code for get the link of "My Order" on my account navigation

 <reference name="top.links">
            <action method="addLink" translate="label" module="sales">
                <label>Orders</label>
                <url helper="sales/getOrderHistoryUrl"/>
                <title>My Orders</title><prepare/>
                <urlParams/>
                <position>50</position>
            </action>        
</reference>

and also in app\etc\modules

 <ML_Sales>
        <active>true</active>
        <codePool>local</codePool>
     </ML_Sales>        

What is wrong? Can someone please help me?

3 Answers 3

1

first of all declare the helper for your module in config.xml as below

<global>
    <helpers>
        <ml_sales>
            <class>ML_Sales_Helper</class>
        </ml_sales>
        <sales>
            <rewrite>
                <data>ML_Sales_Helper_Data</data>
            </rewrite>
        </sales>
    </helpers>
</global>

Now in your app\code\local\ML\Sales\Helper\Data.php the content goes like this

<?php 
class ML_Sales_Helper_Data extends Mage_Sales_Helper_Data {
    public function getOrderHistoryUrl()
    {
        echo 'hello'; exit;
        return $this-> _getUrl('sales/order/history');

    }  

}
?>

in your app\design\frontend\my_package\default\layout\local.xml

<reference name="top.links">
    <action method="addLink" translate="label" module="ml_sales">
        <label>Orders</label>
        <url helper="ml_sales/getOrderHistoryUrl"/>
        <title>My Orders</title>
        <prepare/>
        <urlParams/>
        <position>50</position>
    </action>        
</reference>

after doing this clear the cache and check

Note:- Please check in the xml file there should not be any spaces in the xml file.otherwise this will give error.

10
  • this give me a Fatal error: Class 'Mage_Ml_Sales_Helper_Data' not found in C:\xampp\htdocs\magento\italianamacchi\app\Mage.php on line 547
    – laura
    Commented Nov 2, 2015 at 9:07
  • clear the cache and check Commented Nov 2, 2015 at 9:09
  • delete cache folder from the var/ folder Commented Nov 2, 2015 at 9:09
  • I do it, but i get anyway the error
    – laura
    Commented Nov 2, 2015 at 9:17
  • Mage_Ml_Sales_Helper_Data its ML or MI ? Commented Nov 2, 2015 at 9:20
1

You don't need this line in your helper require_once('Mage/Sales/Helper/Data.php');. The class will be autoloaded.

Also, your new class should extend the original helper

 class ML_Sales_Helper_Data extends Mage_Sales_Helper_Data

And remember to add the app/etc/modules/ML_Sales.xml file that tells magento to pick up your module

3
  • I've add the code of my app/etc/modules/ML_Sales.xml and make the change you suggested but nothing is changed... :(
    – laura
    Commented Nov 2, 2015 at 8:53
  • did you clear the cache?
    – Marius
    Commented Nov 2, 2015 at 9:01
  • yes on magento is disabled and i cleared also the browser cahce but nothing...
    – laura
    Commented Nov 2, 2015 at 9:06
1

Follow this steps and it should work

1. app/code/local/ML/Sales/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <ML_Sales>
            <version>0.1.0</version>
        </ML_Sales>
    </modules>
    <global>
        <helpers>
            <sales>
                <class>ML_Sales_Helper</class>
            </sales>
            <sales>
                <rewrite>
                    <data>ML_Sales_Helper_Sales_Data</data>
                </rewrite>
            </sales>
        </helpers>
    </global>
</config> 

2. app/code/local/ML/Sales/Helper/Data.php

<?php
class ML_Sales_Helper_Data extends Mage_Core_Helper_Abstract
{
}

3. app/code/local/ML/Sales/Helper/Sales/Data.php

<?php
class ML_Sales_Helper_Sales_Data extends Mage_Sales_Helper_Data
{
    protected function getOrderHistoryUrl()
    {
        return $this-> _getUrl('sales/order/history');
    }  
}

4. app/etc/modules/ML_Sales.xml

<?xml version="1.0"?>
<config>
  <modules>
    <ML_Sales>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </ML_Sales>
  </modules>
</config>
1
  • Same Fatal error: Class 'Mage_ML_Sales_Helper_Data' not found in C:\xampp\htdocs\magento\italianamacchi\app\Mage.php on line 547 but with ML(uppercase) of the Pradeep Sanku answer
    – laura
    Commented Nov 2, 2015 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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