I've been looking at the core code for the store column renderer in adminhtml sales (Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Store) and the render functions are really confusing me.

The nested foreach loops at the end of the render and renderExport methods appear to suggest that a single order could be attributed to multiple stores, but that makes no sense to me at all. Here is the core code from 1.9

$data = $this->_getStoreModel()->getStoresStructure(false, $origStores);

foreach ($data as $website) {
    $out .= $website['label'] . "\r\n";
    foreach ($website['children'] as $group) {
        $out .= str_repeat(' ', 3) . $group['label'] . "\r\n";
        foreach ($group['children'] as $store) {
            $out .= str_repeat(' ', 6) . $store['label'] . "\r\n";

In what situation would the above code ever generate more than one website, group or store element?

Is it that this renderer can be called in contexts other than relating to a single order? I can see if it were called for a group of orders, then it would made sense to show all the website, groups and store elements.


The mentioned code will always yield a single store and its parent group and website (at most). The reason for this can be found in Mage_Adminhtml_Model_System_Store::getStoresStructure(false, $origStores). Inside that method there's an iteration over all websites, groups and stores. However, the stores are filtered by the given argument $origStores so that only those matching are selected. In our case this is always a single store id derived from the order - see top of rendering method. At the end of each iteration loop in getStoresStructure, the nested array to be returned to the caller will be cleaned up so that all groups without any store and subsequently all websites without any group will be unset. Eventually, the resulting array consists of a single website containing a single group containing a single store which is then returned to the rendering method and assigned to $data.

  • 1
    Thanks. It was the nested foreach loops in the renderer which confused me. It seems odd to me to use loops to extract something which you know is going to be a single value. I guess you could achieve the same result with PHP reset function and an if block. You could make the case that the foreach syntax is more elegant, but I think it makes less readable code. – Dom Nov 5 '16 at 11:29
  • Yup, this looks kinda ugly - each to their own :) – pong Nov 5 '16 at 11:41

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.