7

I'm working on a module where I need to modify the behavior of the getTracking() method in a shipping method carrier model. For example let's take this default one: Mage_Usa_Model_Shipping_Carrier_Dhl

My requirement is to modify the tracking result returned by this method.

I have two solutions in mind but I'm not sure if one is better or the other (or maybe there's a 3rd solution even better )

Solution 1: classic rewrite

Rewrite the model and the method and add my custom modification before the return statement.

The code would look like this:

public function getTracking()
{
    $result = parent::getTracking();
    // My modifications here
    return $result;
}

Solution 2: rewrite and event dispatched

Rewrite the model and the method. Get the original result with parent::getTracking, dispatch a custom event and return the result. Use an observer to modify the results.

The code would look like this:

public function getTracking()
{
    $result = parent::getTracking();
    Mage::dispatchEvent('my_custom_event', array('result' => $result));
    return $result;
}

To me, the second solution is better but I don't feel good using rewrites.

So my questions are:

  • Which one of those solutions is the best ?
  • Is there a dynamic event I'm not aware of I could observe instead of doing all this ?
  • Any other suggestions ?
9

IMHO it depends on what you are going to do:

1. One time quick and dirty code:

If your code remains inside a single project and you will be the only one handling it, then a single rewrite can be a good idea since I do not see any good reason to increase code complexity with an observer.

2. Reusable code:

If you are going to create something like a community extension, then adding an observer could be a good idea since it gives the possibility to other people ti interact with it.

6

In a reusable extension I'd prefer the event approach. Advantages: users of the extension can add their own modifications without altering your code; solving potential rewrite conflicts is easy

In a project the additional indirection usually is not worth the effort. I'd rather keep it simple, but write the code in a way that refactoring to an event based solution is still possible if you need to add more modifications or want to publish the module.

  • Thanks for that, I appreciate the input, I think Imma go with #1 as I don't plan on reusing it nor making it a community extension – Raphael at Digital Pianism Apr 19 '17 at 10:00
5

Being Mage_Usa_Model_Shipping_Carrier_Dhl::getTracking() a public method, a third viable option is using an interceptor through https://github.com/danslo/Danslo_Aop.

As far as I know, it's the only option that allows you to avoid a rewrite.

My best.

4

I like the idea of Solution 2: rewrite and event dispatched

If the event is best idea but some case,for our requirements,it doesn't effective that times we need to rewrite.Then it is true that class rewrite may be have conflict issues.

First rewrite and then create event is best solution for manipulate the result.

Bcoz,

less code write at your rewrite class

You can use this custom event in other business cases where you want

4

Well Raphael very good question .

this is depend on situation you are the only person who working on that or team working on that.

1) Solution 1 applicable here

if you are the only one who working then you can rewrite that method as per your requirement and you know if in future you have change you can directly change their because its your own code now

2) Solution 2 applicable here

If you are working with team and it may possible number of people need to rewrite this method at that time you go with this solution so other who needs to rewrite this method instead of rewriting they can use event-observer.

Or

If you creating an extension then also Solution 2 is good because some other person who bought this extension and need to modify same method they can use your observer.

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.