$creditmemo = $this->prepareCreditMemo($amount, $baseGrandTotal, $invoice);
$creditmemo->setPaymentRefundDisallowed(
true
)->setAutomaticallyCreated(
true
)->register()->addComment(
__('The credit memo has been created automatically.')
);
The \Magento\Sales\Model\Order\Creditmemo
class does not have the register()
method in the latest Magento 2 versions (both 2.0.0
and the latest development versions).
The register()
method existed in the 1.0.0-beta
/**
* Register creditmemo
*
* Apply to order, order items etc.
*
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function register()
{
if ($this->getId()) {
throw new LocalizedException(__('We cannot register an existing credit memo.'));
}
foreach ($this->getAllItems() as $item) {
if ($item->getQty() > 0) {
$item->register();
} else {
$item->isDeleted(true);
}
}
$this->setDoTransaction(true);
if ($this->getOfflineRequested()) {
$this->setDoTransaction(false);
}
$this->refund();
if ($this->getDoTransaction()) {
$this->getOrder()->setTotalOnlineRefunded(
$this->getOrder()->getTotalOnlineRefunded() + $this->getGrandTotal()
);
$this->getOrder()->setBaseTotalOnlineRefunded(
$this->getOrder()->getBaseTotalOnlineRefunded() + $this->getBaseGrandTotal()
);
} else {
$this->getOrder()->setTotalOfflineRefunded(
$this->getOrder()->getTotalOfflineRefunded() + $this->getGrandTotal()
);
$this->getOrder()->setBaseTotalOfflineRefunded(
$this->getOrder()->getBaseTotalOfflineRefunded() + $this->getBaseGrandTotal()
);
}
$this->getOrder()->setBaseTotalInvoicedCost(
$this->getOrder()->getBaseTotalInvoicedCost() - $this->getBaseCost()
);
$state = $this->getState();
if (is_null($state)) {
$this->setState(self::STATE_OPEN);
}
return $this;
}