Instead of using the converter directly, it would be better to use the order service model instead:
Mage::app('admin');
$order = Mage::getModel('sales/order')->loadByIncrementId('1117575');
$orderItem = $order->getItemsCollection()->getItemByColumnValue('sku', '100-0055-BRO-S');
$service = Mage::getModel('sales/service_order', $order);
$data = array(
'qtys' => array(
$orderItem->getId() => 1
)
);
$creditMemo = $service->prepareCreditmemo($data)->register()->save();
At least that worked fine for me so far.
I give my answer about order email. Other emails and pdf should works with the same logic.
You have to look at \vendor\magento\module-sales\view\frontend\layout\sales_email_order_items.xml
where the block with class Magento\Sales\Block\Order\Email\Items
has the template \vendor\magento\module-sales\view\frontend\templates\order\items.phtml
In this template the line <?php echo $block->getChildHtml('order_totals') ?>
calls the block with totals (always defined in sales_email_order_items.xml
)
...
<block class="Magento\Sales\Block\Order\Totals" name="order_totals" template="order/totals.phtml">
...
This template starts with
<?php foreach ($block->getTotals() as $_code => $_total): ?>
where $block
is a Magento\Sales\Block\Order\Totals
instance.
Then, to add your custom field to totals, try to write a plugin related to one public method of this class, i think you could choose addTotal()
or addTotalBefore()
, in this way your custom field will be added to $this->_totals
array and it will be printed from the foreach loop.
Best Answer
There are few steps are required for this.
Load order by Order Factory
Inject the factory class
\Magento\Sales\Model\OrderFactory
on__construct
class.for getting order.Then order get by order id
Get Invoice collection from Order object
Get Invoice collection from Order object using getInvoiceCollection()
As you want run delete operation then you should set
$this->registry->register('isSecureArea', true);
Get Shipment collection from Order object
Get Shipment collection from Order object using
getShipmentsCollection()
Get credit memo collection from Order object
Get credit memo collection from Order object using
getCreditmemosCollection()
code: