Magento Programming and Class Usage

I took up the Magento study project over this break at the university, and although I very easily understood how to create a theme, I am having problems working on the class structure in Magento.

For what I'm trying to do, I want to calculate the monthly sales figure. From the toolbar, I want to find where $this->getTotals() created, so I can add this picture to the array.

As a starting point, can I redirect me more to the Mage file that it was created, or perhaps even a resource that explains the Magento programming structure, which is not so basic that it belittles and does not make it impossible? (It seems like a challenge these days if you don't know where to look)

+4
source share
3 answers

The structure of Magentos is rather confusing, I would read this documentation as it helps to understand where everything is located.

Now that we find certain methods, I think 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, that sounds like a popular method name, but at least now we can narrow down which one is required. Therefore, I assume that it will be in the sales section, since you seem to be talking about sales, so we get a more manageable list.

 ./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 hunt.

+1
source

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

The Magento view is displayed from the classes of object instances - blocks, which, as a rule, are in the folder folders in the directories of modules, for example. app / code / core / Mage / Adminhtml / Block / - and (optionally) templates, which can be found in the app / design / {area} / {package} / {theme} / template / section.

When debugging a view, the readiness to use get_class($this) calls in template files is often shown by the class. Of course, this means that you need to find a template. In the interface, you can use template template hints - and you can use them in admin with two lines of XML - but I would recommend Fabrizio Branca Advanced template hints for ease of use and its ability to wrap blocks without templates.

+2
source

If you are looking at a .phtml file, the first thing to do is to understand that each .phtml file works in the context of some instance of some Block class. So first find out which block class you are in by looking at comments or by typing / echo / var_dumping / etc the value returned by get_class($this) .

Then you can grep the app / code directory to declare a block class, i.e. grep -irn 'class Mage_Catalog_Block_Product_List_Toolbar' app/code/core

Please note that if you use * nix, if you use an IDE, you can simply tell your IDE to teleport you to the class file / declaration, standing on PHP doc comments, such as those found in the main .phtml files, i.e. @see Mage_Catalog_Block_Product_List_Toolbar , and by clicking any shortcut triggers that work on your IDE.

BTW, note on getter methods:

Almost all Magento classes inherit from the Varien_Object class, which implements __ call () - which, if you recall, is triggered when invoking inaccessible methods on the object - so when you call, say, $this->getBananas() on the object, Magento will not generate a fatal error, but instead checks if the internal $data array of the object contains the "bananas" key and returns its value or NULL if it does not exist.

The consequence of this is that sometimes you will see a call like $this->getSomething() , and then you will look for code looking for function getSomething() , but you will not find it because it is not declared anywhere, it is just a template using behavior of the magic getter Varien_Object .

However, the grepping source of Magento super helpful. In addition, if you use the getter function and you cannot find its declaration, then you know that the code simply accesses the data attribute, which is also useful information.

+1
source

Source: https://habr.com/ru/post/1447686/


All Articles