I have a template file that resides under app/design/frontend//theme/Magento_Sales/templates/order/email_invoice_totals.phtml
As I understand, the business logic should not reside in those template files. Below is, I have the code for that phtml file.
<?php
use Magento\Framework\Escaper;
use Magento\Sales\Block\Order\Totals;
/** @var Totals $block */
/** @var Escaper $escaper */
$index = 0;
$totalsData = $block->getTotals();
function arrayElementPositionChange($key, $new_index, $array)
{
if($new_index < 0) return;
if($new_index >= count($array)) return;
if(!array_key_exists($key, $array)) return;
$ret = array();
$ind = 0;
foreach($array as $k => $v)
{
if($new_index == $ind)
{
$ret[$key] = $array[$key];
$ind++;
}
if($k != $key)
{
$ret[$k] = $v;
$ind++;
}
}
if($new_index == $ind)
$ret[$key] = $array[$key];
return $ret;
}
$totalsData = arrayElementPositionChange('tax', 1, $totalsData) ? array_move('tax', 1, $totalsData) : $block->getTotals();
?>
<?php foreach ($totalsData as $_code => $_total): ?>
<?php $index = $index+1; ?>
<?php if ($_total->getBlockName()): ?>
<?=
dd($block->getChildHtml($_total->getBlockName()));
$htmlBlock = $block->getTotals($_total->getBlockName(), false);
?>
<?php else:?>
<tr class="<?php if ($_code == "grand_total"): ?> <?= "grand-total"?><?php elseif ($_code === "subtotal_excl"):?> <?= "subtotal-excl" ?> <?php else:?> <?= $escaper->escapeHtmlAttr($_code);?><?php endif; ?>">
<th class=<?= $index == count($block->getTotals()) ? "tax-history" : "row-history"?> <?= /* @noEscape */ $block->getLabelProperties() ?> scope="row">
<?= $escaper->escapeHtml($_total->getLabel()) ?>
</th>
<td class=<?= $index == count($block->getTotals()) ? "total-sum-history" : "price-history"?> <?= /* @noEscape */ $block->getValueProperties() ?> data-th="<?= $escaper->escapeHtmlAttr($_total->getLabel()) ?>">
<span class="total">€ <?= number_format($_total['value'], 2, '.', ''); ?></span>
</td>
</tr>
<?php endif; ?>
<?php endforeach?>
As seen, I have a function called arrayElementPositionChange() but, it should not be inside the template file. So, the question is, where should I add this business logic? and then use it in my phtml file?
One more question is that, I have
$block->getChildHtml($_total->getBlockName(), false);
Here, the child html for is getting fetched. How can I find the child html's phtml file and manipulate it? I want to make some changes inside that html that is returned using getChildHtml method.