at the moment I have a function which will refund the invoice on an order. However the shipping is currently also being refunded, which I don't want. Is there a way to set the shipping to zero on an invoice refund?
My code is currently this:
private function createCreditMemoItem($item)
{
$creditmemoItem = $this->creditmemoItemFactory->create();
$creditmemoItem->setOrderItemId($item->getId());
$creditmemoItem->setQty($item->getQtyOrdered());
return $creditmemoItem;
}
private function refundItems($order,$items) {
$refundItems = $items;
$items = [];
foreach ($order->getAllVisibleItems() as $item) {
if (!in_array($item->getId(), $refundItems)) {
continue;
}
// create creditmemo item and add to array
$creditmemoItem = $this->createCreditmemoItem($item);
$items[] = $creditmemoItem;
}
// get the invoice id
$invoiceId = null;
foreach ($order->getInvoiceCollection() as $invoice) {
// this will always point to the latest invoice id
$invoiceId = $invoice->getId();
}
// refund the invoice
$this->refundInvoice->execute(
$invoiceId,
$items,
true, // false = offline / true = online
true // notify customer
);
}
the refundInvoice
uses the Magento\Sales\Api\RefundInvoiceInterface
class.
So just to re-iterate, the partial refund works fine in the sense that, the individual items chosen are refunded but like I say, the shipping amount is also being refunded, which I don't want to happen :)
Any help would be appreciated.
Cheers
Exception #0 (Magento\Sales\Exception\DocumentValidationException): Creditmemo Document Validation Error(s): We can't create creditmemo for the invoice.
, This if I don't use the$ this-> createCreditmemoItem ($ item);
part, but if I include it, thecreateCreditmemoItem
function gives me an error, where does this function come from? How do I add it to my controller?\Magento\Sales\Model\Order\CreditmemoFactory
into your class.Fatal error: Uncaught Error: Call to undefined method Magento\Sales\Model\Order\CreditmemoFactory::create()
It is as if the create method does not exist in the class that I importMagento\Sales\Model\Order\Creditmemo\ItemCreation;
, But now I have another problem, the return is created well, but if I do not return the shipping amount, the order still does not go to the closed state, and if I manually set the state with$order->setState(\Magento\Sales\Model\Order::STATE_CLOSED)->setStatus(\Magento\Sales\Model\Order::STATE_CLOSED);
, Changes the status to closed, but does not return the order