In Magento there is a functionality where you can define the order of total calculation by specifing before and after which totals a total should be run.
I added a c
Well was stucked at this for years!!!+
Now i know why some projects of the past were so difficult to adjust regarding wee and tax combinations a nightmare i could say, never got to understand why, yesterday i found why, later i found this article, a real shame.. but most of the time i need to know the response to be capable of search the question..
And the obvius solution at least for linux heads without fear, is the code below, basically i make use of the ancient linux command tsort that especifically does a topolocical order in the way we need here..
For the entomological and archeologist souls among us some pointers http://www.gnu.org/software/coreutils/manual/html_node/tsort-invocation.html,I am using authentic 80's technology... yummmm
/**
* Aggregate before/after information from all items and sort totals based on this data
*
* @return array
*/
protected function _getSortedCollectorCodes() {
if (Mage::app()->useCache('config')) {
$cachedData = Mage::app()->loadCache($this->_collectorsCacheKey);
if ($cachedData) {
return unserialize($cachedData);
}
}
$configArray = $this->_modelsConfig;
// invoke simple sorting if the first element contains the "sort_order" key
reset($configArray);
$element = current($configArray);
if (isset($element['sort_order'])) {
uasort($configArray, array($this, '_compareSortOrder'));
$sortedCollectors = array_keys($configArray);
} else {
foreach ($configArray as $code => $data) {
foreach ($data['before'] as $beforeCode) {
if (!isset($configArray[$beforeCode])) {
continue;
}
$configArray[$code]['before'] = array_merge(
$configArray[$code]['before'],
$configArray[$beforeCode]['before']);
$configArray[$code]['before'] = array_unique(
$configArray[$code]['before']);
$configArray[$beforeCode]['after'] = array_merge(
$configArray[$beforeCode]['after'], array($code),
$data['after']);
$configArray[$beforeCode]['after'] = array_unique(
$configArray[$beforeCode]['after']);
}
foreach ($data['after'] as $afterCode) {
if (!isset($configArray[$afterCode])) {
continue;
}
$configArray[$code]['after'] = array_merge(
$configArray[$code]['after'],
$configArray[$afterCode]['after']);
$configArray[$code]['after'] = array_unique(
$configArray[$code]['after']);
$configArray[$afterCode]['before'] = array_merge(
$configArray[$afterCode]['before'], array($code),
$data['before']);
$configArray[$afterCode]['before'] = array_unique(
$configArray[$afterCode]['before']);
}
}
//uasort($configArray, array($this, '_compareTotals'));
$res = "";
foreach ($configArray as $code => $data) {
foreach ($data['before'] as $beforeCode) {
if (!isset($configArray[$beforeCode])) {
continue;
}
$res = $res . "$code $beforeCode\n";
}
foreach ($data['after'] as $afterCode) {
if (!isset($configArray[$afterCode])) {
continue;
}
$res = $res . "$afterCode $code\n";
}
}
file_put_contents(Mage::getBaseDir('tmp')."/graph.txt", $res);
$sortedCollectors=explode("\n",shell_exec('tsort '.Mage::getBaseDir('tmp')."/graph.txt"),-1);
}
if (Mage::app()->useCache('config')) {
Mage::app()
->saveCache(serialize($sortedCollectors),
$this->_collectorsCacheKey,
array(Mage_Core_Model_Config::CACHE_TAG));
}
return $sortedCollectors;
}
I've posted the complete funcion for the sake of completeness, and of course is working like a charm for me at least...