4

I've taken on the project of learning Magento over this university break, and while I've figured out how to create a theme rather easily, I am having trouble working with the class structure in Magento.

For what I'm trying to do, I want to calculate a month to date sales figure. Off the Dashboard I wish to find where $this->getTotals() is created so I can add this figure to the array.

As a starting point, can anymore direct me to the Mage file this is created in, or possibly even a resource that explains the programming structure of Magento that is neither so basic it's mind numbing nor so advanced it's impossible? (Seems to be a hard task these days, unless you know where to look)

LSerni
  • 55,617
  • 10
  • 65
  • 107
  • You might want to look at the accepted answer of http://stackoverflow.com/questions/576908/how-to-create-a-simple-hello-world-module-in-magento/585607 – Manos Dilaverakis Nov 22 '12 at 15:26

3 Answers3

2

Magento (blocks and models specifically) makes abundant use of overloading in PHP, so grep can often yield distracting results or nothing at all; ref. Varien_Object::__call() [link].

Magento's view is rendered from an object instance - block classes, which tend to reside in Block folders in module directories e.g. app/code/core/Mage/Adminhtml/Block/ - and (optionally) templates, which can be found under app/design/{area}/{package}/{theme}/template/.

When debugging the view, ready use of get_class($this) calls in template files will often reveal the class involved. Of course, this means that you need to find the template. In the frontend, you can use template path hints - and it's possible to use these in the admin with a couple lines of XML - but I would recommend Fabrizio Branca's Advanced Template Hints for its ease of use and its ability to wrap non-template blocks.

Community
  • 1
  • 1
benmarks
  • 23,384
  • 1
  • 62
  • 84
1

Magentos structure is pretty confusing, I would read through this documentation as it does help understanding where everything is located.

Now as for finding specific methods I find the best way is to use grep.

In your example you want to find getTotals, I run the following grep:

grep -RTi 'function getTotals(' ./app/

And it returns:

./app/code/core/Mage/Sales/Block/Order/Totals.php      :    public function getTotals($area=null)
./app/code/core/Mage/Sales/Model/Quote.php     :    public function getTotals()
./app/code/core/Mage/Sales/Model/Resource/Sale/Collection.php  :    public function getTotals()
./app/code/core/Mage/Sales/Model/Entity/Sale/Collection.php    :    public function getTotals()
./app/code/core/Mage/Sales/Model/Quote/Address.php     :    public function getTotals()
./app/code/core/Mage/Checkout/Block/Onepage/Review/Info.php    :    public function getTotals()
./app/code/core/Mage/Checkout/Block/Cart/Abstract.php  :    public function getTotals()
./app/code/core/Mage/Checkout/Block/Cart/Totals.php    :    public function getTotals()
./app/code/core/Mage/Paypal/Block/Express/Review/Details.php   :    public function getTotals()
./app/code/core/Mage/Paypal/Model/Cart.php     :    public function getTotals($mergeDiscount = false)
./app/code/core/Mage/Adminhtml/Block/Sales/Order/Create/Totals.php     :    public function getTotals()
./app/code/core/Mage/Adminhtml/Block/Sales/Order/Totalbar.php  :    protected function getTotals()
./app/code/core/Mage/Adminhtml/Block/Customer/Edit/Tab/View/Sales.php  :    public function getTotals()
./app/code/core/Mage/Adminhtml/Block/Widget/Grid.php   :    public function getTotals()
./app/code/core/Mage/Adminhtml/Block/Dashboard/Bar.php :    protected function getTotals()

Oh dear, this seems to be a popular method name, but at least now we can narrow down which one is required. So I am guessing it will be in the sales section as you seem to be talking about sales so we end up with a more manageable list of.

./app/code/core/Mage/Sales/Block/Order/Totals.php      :    public function getTotals($area=null)
./app/code/core/Mage/Sales/Model/Quote.php     :    public function getTotals()
./app/code/core/Mage/Sales/Model/Resource/Sale/Collection.php  :    public function getTotals()
./app/code/core/Mage/Sales/Model/Entity/Sale/Collection.php    :    public function getTotals()
./app/code/core/Mage/Sales/Model/Quote/Address.php     :    public function getTotals()

Happy hunting.

jzahedieh
  • 1,570
  • 1
  • 15
  • 27
  • very helpful, is there any way i could narrow this down if i know a phtml file that uses the method? basically the toolbar.phtml file calculates this value with its range selector. If i can replicate this it will solve the problem easy. love that grep thing though (not a linux user but i may boot up my old dev server) – DarkAvernus Nov 22 '12 at 11:32
  • 1
    If you know the .phtml file, set up a debugger like xdebug, set a break point in the .phtml file and step into the function. – Alex Nov 22 '12 at 11:36
  • Also developing on Linux is wonderful, I would highly recommend it, or getting something like [cygwin](http://www.cygwin.com/) on your windows box. – jzahedieh Nov 22 '12 at 11:40
1

If you are looking at the .phtml file, first thing to do is realize that every .phtml file is working in the context of some instance of some Block class. So first find out what block class you're in, either by looking at the comments, or printing/echoing/var_dumping/etc the value returned by get_class($this).

Then you could grep the app/code directory for the class declaration of the block, i.e grep -irn 'class Mage_Catalog_Block_Product_List_Toolbar' app/code/core

Note that, whether or not you're on *nix, if you're using an IDE, you can probably just tell your IDE to teleport you to the class file/declaration by standing on PHP doc comments such as those found in the core's .phtml files, i.e. @see Mage_Catalog_Block_Product_List_Toolbar and pressing whatever shortcut triggers that function on your IDE.

BTW, a note about getter methods:

Pretty much all Magento classes inherit from the Varien_Object class, which implements __call() -- which, if you recall, is triggered when invoking inaccessible methods on an object -- so that when you call, say, $this->getBananas() on an object, Magento won't throw a fatal error, but instead will check if the internal $data array of the object contains a key "bananas", and return its value, or NULL if it doesn't exist.

The implications of this is that sometimes you'll see a call like $this->getSomething() and then you'll grep the code looking for function getSomething(), but you won't find it, because it's not declared anywhere, it's just the template making use of the magic getter behavior of Varien_Object.

Still, grepping the Magento source is super useful. Plus, if you grep a getter function and you don't find its declaration, then you know that the code is just accessing a data attribute, which is perfectly helpful information as well.

Alexander
  • 2,052
  • 1
  • 15
  • 17