Add editable text input to creditmemo totals
-
18-01-2021 - |
Pergunta
How to change the -€5.00 simple text to a text input field (editable) ?
This is how I add the totals:
<?php
namespace My\Discount\Block\Adminhtml\Sales\Order;
class Totals extends \Magento\Framework\View\Element\Template
{
public function initTotals()
{
$parentBlock = $this->getParentBlock();
$order = $parentBlock->getOrder();
$source = $parentBlock->getSource();
$amount = $order->getData("discount_amount"); // custom field
if (isset($amount)) {
$total = new \Magento\Framework\DataObject([
'code' => 'customdiscount',
'value' => -$amount,
'label' => "Custom Discount"
]);
$parentBlock->addTotal($total, 'customdiscount');
}
return $this;
}
}
/view/adminhtml/layout/sales_order_creditmemo_new.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="creditmemo_totals">
<block class="My\Discount\Block\Adminhtml\Sales\Order\Totals"
name="customdiscount" />
</referenceBlock>
</body>
</page>
Solução
Solved by using a block template in this way:
view\adminhtml\layout\sales_order_creditmemo_new.xml
<referenceBlock name="creditmemo_totals">
<block class="Vendor\Module\Block\Adminhtml\Sales\Creditmemo\NewMemo"
name="customdiscount"
template="Vendor_Module::creditmemo.phtml" />
</referenceBlock>
<move element="customdiscount" destination="creditmemo_totals" after="creditmemo_totals" />
Block\Adminhtml\Sales\Creditmemo\NewMemo.php
public function initTotals() {
$total = new \Magento\Framework\DataObject([
'code' => 'customdiscount',
'value' => 0,
'label' => "Discount",
'block_name' => 'customdiscount'
]);
$this->getParentBlock()->addTotal($total, 'customdiscount');
return $this;
}
In this way, the block total is overwritten with the template specified with block_name
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange