Pages

Sunday 22 June 2014

Magento Developper’s Guide (Lesson 9) – Rewrite / modify a Magento Model

This tutorial is the 9th of many , if it’s not already done, I suggest you read to the tutorials starting with the summary of this series.

Here is a small article until the next which is a little longer to write, It subjects will be « the Events and Observers in Magento ».

If you want to help me, you can make a small article about this series of tutorials on your blog, you can tweet on these article, follow my facebook page and share it, talk about this blog, etc …

But please no copy/paste, Google does not like! Thank you.

why and when editing an existing template

Today we will extend a Model, this may be useful sometimes so you need to know how to do it. This can be useful for example, if you want to change the method getName() of a customer.

How do you rewrite a model

Let’s start from the plugin Test you have made in the previous tutorials and extend it model.

<?php
class Easylife_Test_Model_Customer  extends Mage_Customer_Model_Customer
{
  public function getName()
  {
     $name = '';
        if ($this->getPrefix()) {
            $name .= $this->getPrefix() . ' ';
        }
        $name .= $this->getFirstname();
        if ($this->getMiddlename()) {
            $name .= ' ' . $this->getMiddlename();
        }
        $name .=  ' ' . $this->getLastname();
        if ($this->getSuffix()) {
            $name .= ' ' . $this->getSuffix();
        }
        return $name.'88888';
  }
}
Then the same principle, edit config.xml and add :

<models>
  <customer>
    <rewrite>
      <product> Easylife_Test_Model_Customer</product>
    </rewrite>
  </customer>
</models>
Now, if you use getName() method on an Customer object, you will have the string « '88888″ after the name.
This is an example to show you that we can make modification of the model very easily, this example is useless but it is easy to understand ;)

Conclusion:

You now know how to rewrite a model ;)

As usual, feel free to leave your messages and put a links to these tutorials on your websites. It’s always nice for me.
 
Find the summery of this tutorial

No comments:

Post a Comment